-
Solving USACO palsquare problem in c++ALGORITHM/USACO 2019. 8. 12. 14:15728x90
firt. You should make function wich is change number according to base
second. it just simple by using for statement to check all square numbers
#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("palsquare.out"); ifstream fin("palsquare.in"); int B; fin >> B; string answer1; string answer2; for (int i = 1; i <= 300; i++) { answer2 = toBase(i * i, B); answer1 = toBase(i, B); string dummy = answer2; reverse(dummy.begin(), dummy.end()); reverse(answer1.begin(), answer1.end()); if (answer2 == dummy) { fout << answer1 << " " << dummy << endl; } } }
'ALGORITHM > USACO' 카테고리의 다른 글
solving USACO dualpal 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