USACO Section 1.2: Dual Palindromes

 1 /*

 2 ID: leetcod3

 3 PROG: dualpal

 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 ("dualpal.out");

26 ifstream fin ("dualpal.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 N, S;

48     fin >> N >> S;

49     int account = 0;

50     while (account < N) {

51         S++;

52         int cur = 0;

53         for (int i = 2; i <= 10; i++) {

54             if (palin(tobase(S, i))) cur++;

55         }

56         if (cur >= 2) {

57             fout << S << endl;

58             account++;

59         }

60     }

61     return 0;

62 }

 

你可能感兴趣的:(USACO)