CSES - Datatähti 2021 alku - Results
Submission details
Task:2021-luvut
Sender:jogr
Submission time:2020-09-30 14:01:00 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'bool is_valid(long long int, int*)':
input/code.cpp:11:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int ci = 0; ci < ns.length(); ci++) {
                      ~~~^~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:32:25: error: 'indices' was not declared in this scope
         if (is_valid(i, indices)) {
                         ^~~~~~~
input/code.cpp:36:36: error: 'indices' was not declared in this scope
         if (cn == n && is_valid(i, indices)) {
                                    ^~~~~~~

Code

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool is_valid(long long num, int* indices) {
char checklist[] = { '2', '0', '2', '1' };
int index = 0;
string ns = to_string(num);
for (int ci = 0; ci < ns.length(); ci++) {
if (index == 4) return true;
if (ns[ci] == checklist[index]) {
indices[index] = ci;
index++;
}
}
if (index == 4) return true;
else return false;
}
int main() {
long long n; // the n:th to be found
cin >> n;
long long cn = 0; // current nth
long long output = 0; // the - output..
for (long long i = 2021; i < pow(10, 12); i++) {
if (is_valid(i, indices)) {
cn++;
//cout <<cn << " : " << i << endl;
}
if (cn == n && is_valid(i, indices)) {
output = i;
break;
}
}
cout << output;
return 0;
}