-
solving USACO dualpal problem in c++ALGORITHM/USACO 2019. 8. 12. 14:43728x90
각 base마다 한번씩 체크해주기만 하자.
체크한 횟수가 2회일경우 그것은 dualpal.
#include<fstream> #include<algorithm> #include <string> #include <stdlib.h> using namespace std; char toChar(int a) { if (a > 10) { return 'A' + a - 10; } return a + '0'; } string toBase(int target, int base) { string ret; while (target > 0) { int leftover = target % base; target /= base; ret += toChar(leftover); } return ret; } int main() { ofstream fout("dualpal.out"); ifstream fin("dualpal.in"); int N, S; fin >> N >> S; int check = 0; for (int i = S + 1;; i++) { int checkout = 0; for (int base = 2; base <= 10; base++) { string a; a = toBase(i, base); string dummy = a; reverse(dummy.begin(), dummy.end()); if (a == dummy) { checkout++; if (checkout == 2) { fout << i << endl; check++; break; } } } if (check == N) { break; } } }
'ALGORITHM > USACO' 카테고리의 다른 글
Solving USACO palsquare problem in c++ (0) 2019.08.12 Solving USACO Name That Number problem in c++ (0) 2019.08.11 Solving the usaco transform problem in c++ (0) 2019.08.11 Milking Cows /c++ (0) 2019.08.11 Broken Necklace /c++ (0) 2019.08.11