hdu_1282 回文数猜想

2014-11-24 12:24:33 · 作者: · 浏览: 0

分析:

题目理解起来还是简单的,基本上有两种思路:1) 将int转为string来实现; 2)直接用int做(回文串判断,相加)

第二中思路比较直接,将一个数倒置得到新的数,然后判断是否是回文数(两个数值相等);不过鄙人采用了第一种方法,因为字符串的操作不太熟练,需要联系来着。

字符串操作:

string --> int

int a;

string str=123;

a=atoi(str.c_str()); //头文件 stdlib.h

int --> string

int a;

string str;

stringstream ss; //头文件 sstream.h

ss<

ss>>str;

字符串转置

string str=123;

reverse( str.begin(), str.end() ); // algorithm.h

cout<

代码:

//hdu 1282
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          using namespace std; #define MAXN 10000 int cnt; string s[MAXN]; bool palindrome(string e) { for(int i=0,j=e.length()-1;i
         
          ; cout<
          
           >x; if(palindrome(x)) { s[cnt++]=x; break; } } show(); } int main() { freopen(in.txt,r,stdin); string str; while(cin>>str){ if(palindrome(str)){ cout<<0<