UVA 10018: Reverse and Add

Jasonlin posted @ 2011年3月27日 15:40 in UVA , 2578 阅读

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=12&problem=959&mosmsg=Submission+received+with+ID+8683636

题目大意:

      一个数加上它反过来的数的和再做同样操作,直到这个和是一个回文数。

解题思路:

      按照题目意思做,但是要注意数据大小,题目说是结果不大于4,294,967,295,所以用unsigned就行。

解题代码:

#include<iostream>
using namespace std;
unsigned reverse(unsigned n){
    unsigned t=0;
    while(n){
        t=t*10+n%10;
        n/=10;
    }
    return t;
}
int main(){
    int n;
    cin>>n;
    while(n--){
        unsigned t,c=0;
        cin>>t;
        while(t!=reverse(t)){
            t+=reverse(t);
            c++;
        }
        cout<<c<<' '<<t<<endl;
    }
}

  扩展知识:http://en.wikipedia.org/wiki/Palindromic_number

  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter