USACO 1.2 Dual Palindromes (回文,进制转换)

废话不哆嗦,贴代码:

/*
  ID:twd30651
  PROG:dualpal
  LANG:C++
*/
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
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;
}


你可能感兴趣的:(USACO 1.2 Dual Palindromes (回文,进制转换))