廢話不哆嗦,貼代碼:
/* ID:twd30651 PROG:dualpal LANG:C++ */ #include#include #include #include using namespace std; char s[100]; int N,S; void gs(int num,int BASE) { int index=0; while(num/BASE) { s[index++]="0123456789ABCDEF"[num%BASE];//用了一個在C和指針上學得寫法,簡明易懂,就是浪費了一個17字節的字符串常量 num=num/BASE; } s[index++]="0123456789ABCDEF"[num%BASE]; s[index]='\0'; } bool isPalindromes() { size_t len=strlen(s); if(s[0]=='0'&&s[len-1]=='0')return false; for(size_t i=0;i<=len/2;++i) { if(s[i]!=s[len-i-1])return false; } return true; } int main(int argc,char *argv[]) { freopen("dualpal.in","r",stdin); freopen("dualpal.out","w",stdout); scanf("%d %d",&N,&S); int count=0; int fn=0; for(int i=S+1;;++i) { fn=0; for(int j=2;j<=10;++j) { if(fn<2) { memset(s,0,sizeof(s)); gs(i,j); if(isPalindromes()) { fn++; if(fn==2) { printf("%d\n",i); count++; break; } } } } if(count==N) break; } return 0; }