C++_USACO_Dual Palindromes

/*
PROB:dualpal
LANG:C++
*/
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
stack<char> rem_stack; 
string transtoN(int num_ten,int base){
    stringstream ss;
    string str_n="";
    int irem=0;
    char crem=' ';
    if(base==10){
        ss<<num_ten;
        str_n=ss.str();
    }
    else{  
        while(num_ten!=0){
            irem=num_ten%base;
            if(irem<10)
                crem=(char)(irem+48);
            else
                crem=irem-10+'A';
            rem_stack.push(crem);
            num_ten=num_ten/base;
        }
        while(!rem_stack.empty()){
            str_n.append(1,rem_stack.top());
            rem_stack.pop();
        }
    }
    return str_n;
}

int main(){
    ifstream fin("dualpal.in");
    ofstream fout("dualpal.out");
    int n,s;
    int count_base=0;
    int count_n=0;
    string str_n="";    
    bool ispals=true;
    fin>>n>>s;
    for(int i=s+1;i<65536;i++){
        count_base=0;
        for(int k=2;k<=10;k++){
            str_n=(transtoN(i,k));
            if(str_n[0]=='0' || str_n[str_n.size()-1]=='0')
                continue;
            for(int j=0;j<str_n.size()/2;j++){
                if(str_n[j]!=str_n[str_n.size()-1-j]){
                    ispals=false;
                    break;
                }
            }
            if(ispals)
                count_base++;
            if(count_base==2){
                count_n++;
                fout<<i<<endl;
                break;
            }
            ispals=true;
        }
        if(count_n==n)
            break;
    }
    return 0;
}

 

posted @ 2013-08-02 16:44  开心成长  阅读(190)  评论(0编辑  收藏  举报