USACO SEC.1.2 No.5 Dual Palindromes
题意:输入一个N和S,在[S + 1, 10000]范围以内找到N个至少在[2,10]两种进制上面都是回文数的数字
核心:仍然是转换十进制到任意进制的算法
注意G++下要包含<cstring>才能找到strcpy等相关的函数
/* ID: lsswxr1 PROG: dualpal LANG: C++ */ #include <iostream> #include <vector> #include <map> #include <list> #include <set> #include <deque> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <cctype> #include <cstdio> #include <iomanip> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <fstream> using namespace std; ///宏定义 const int INF = 1000000000; const int MAXN = 15; const int maxn = MAXN; ///全局变量 和 函数 #define USACO #ifdef USACO #define cin fin #define cout fout #endif ////////////////////////////////////////////////////////////////////////// int N, S; char str[maxn]; void numtrans(char* tar, int jingzhi, int num) { if (num == 0) { strcpy(tar, ""); return; } numtrans(tar, jingzhi, num / jingzhi); int len = strlen(tar); tar[len] = '0' + num % jingzhi; tar[len + 1] = '\0'; } bool isPalindromes(char* src) { int len = strlen(src); for (int i = 0; i < len / 2; i++) { if (src[i] != src[len - 1 - i]) return false; } return true; } int main() { #ifdef USACO ofstream fout ("dualpal.out"); ifstream fin ("dualpal.in"); #endif ///变量定义 while (cin >> N >> S) { int cnt = 0; int cnttrans; for (int i = S + 1; S < 10000; i++) { cnttrans = 0; for (int j = 2; j <= 10; j++) { numtrans(str, j, i); if (isPalindromes(str)) { cnttrans++; if (cnttrans >= 2) break; } } if (cnttrans >= 2) { cnt++; cout << i << endl; if (cnt == N) { break;; } } } } ///结束 return 0; }