USACO Section 1.2: Palindromic Squares

 1 /*

 2 ID: leetcod3

 3 PROG: palsquare

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 #include <limits>

21 #include <stack>

22 

23 using namespace std;

24 

25 ofstream fout ("palsquare.out");

26 ifstream fin ("palsquare.in");

27 

28 string tobase(int x, int base) {

29     string ans = "";

30     while (x) {

31         int tmp = x % base;

32         if (tmp >= 10) ans.insert(0, 1, 'A'+tmp-10);

33         else ans.insert(0, 1, '0'+tmp);

34         x /= base;

35     }

36     return ans;

37 }

38 

39 bool palin(string s) {

40     for (int i = 0; i < s.size() / 2; i++) {

41         if (s[i] != s[s.size()-1-i]) return false;

42     }

43     return true;

44 }

45 

46 int main() {

47     int base;

48     fin >> base;

49     for (int i = 1; i <= 300; i++) {

50         if (palin(tobase(i*i, base))) fout << tobase(i, base) << " " << tobase(i*i, base) << endl;

51     }

52     return 0;

53 }

 

你可能感兴趣的:(USACO)