Skip to content

Commit 10cb887

Browse files
authored
GH-42: Solve codeforces 489C (#84)
1 parent caf1e9e commit 10cb887

File tree

17 files changed

+233
-0
lines changed

17 files changed

+233
-0
lines changed

problems/codeforces489C/.gitginore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
solution

problems/codeforces489C/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run src/main:solution < tests/data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```

problems/codeforces489C/run_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for f in $(cd tests/; ls *.in); do
2+
test_id=$(echo $f | cut -d'.' -f1)
3+
./solution < tests/$test_id.in | diff -aur tests/$test_id.out -
4+
done
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <iostream>
4+
#include <map>
5+
#include <string>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
void GenerateSmallest(int m, int s) {
11+
vector<int> a;
12+
int tm = m;
13+
while (m > 0) {
14+
if (s > 9) {
15+
a.insert(a.begin(), 9);
16+
s -= 9;
17+
} else if (s > 1) {
18+
if (m > 1){
19+
a.insert(a.begin(), s - 1);
20+
s = 1;
21+
} else {
22+
a.insert(a.begin(), s);
23+
}
24+
} else {
25+
if (m > 1) {
26+
a.insert(a.begin(), 0);
27+
} else {
28+
a.insert(a.begin(), 1);
29+
}
30+
}
31+
m--;
32+
}
33+
for (int i = 0; i < tm; i++) {
34+
cout << a[i];
35+
}
36+
}
37+
38+
void GenerateBiggest(int m, int s) {
39+
vector<int> a;
40+
int tm = m;
41+
while (m > 0) {
42+
if (s > 9) {
43+
a.push_back(9);
44+
s -= 9;
45+
} else {
46+
a.push_back(s);
47+
s = 0;
48+
}
49+
m--;
50+
}
51+
for (int i = 0; i < tm; i++) {
52+
cout << a[i];
53+
}
54+
}
55+
56+
void solve(int m, int s) {
57+
if (s > m * 9) {
58+
cout << -1 << " " << -1 << endl;
59+
return;
60+
}
61+
if (s == 0){
62+
if(m > 1){
63+
cout << -1 << " " << -1 << endl;
64+
return;
65+
} else {
66+
cout << 0 << " " << 0 << endl;
67+
return;
68+
}
69+
}
70+
GenerateSmallest(m, s);
71+
cout << " ";
72+
GenerateBiggest(m, s);
73+
cout << endl;
74+
return;
75+
}
76+
77+
int main() {
78+
ios::sync_with_stdio(false);
79+
cin.tie(0);
80+
int m, s;
81+
cin >> m >> s;
82+
solve(m, s);
83+
return 0;
84+
}

problems/codeforces489C/tests/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforces489C/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2 15
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
69 96
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-1 -1

0 commit comments

Comments
 (0)