迴文數猜想
時間限制:2000 ms | 內存限制:65535 KB
難度:2
-
描述
- 一個正整數,如果從左向右讀(稱之爲正序數)和從右向左讀(稱之爲倒序數)是一樣的,這樣的數就叫回文數。任取一個正整數,如果不是迴文數,將該數與他的倒序數相加,若其和不是迴文數,則重複上述步驟,一直到獲得迴文數爲止。例如:68變成154(68+86),再變成605(154+451),最後變成1111(605+506),而1111是迴文數。於是有數學家提出一個猜想:不論開始是什麼正整數,在經過有限次正序數和倒序數相加的步驟後,都會得到一個迴文數。至今爲止還不知道這個猜想是對還是錯。現在請你編程序驗證之。
-
輸入
- 多組數據。
輸入一個正整數n<10^18 -
輸出
- 輸出2行
第一行輸出 步驟數
第二行輸出 步驟路徑 -
樣例輸入
-
27228
37649
-
樣例輸出
-
3
27228--->109500--->115401--->219912
2
37649--->132322--->355553
-
#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f
#define ll long long
using namespace std;
ll a[10100];
ll ss(ll n){
ll k=0,t;
while(n){
t=n%10;
k=k*10+t;
n/=10;
}
return k;
}
int main(){
ll n,m,t,i;
while(~scanf("%lld",&n)){
t=0;
while(1){
a[t++]=n;
m=ss(n);
if(m==n) break;
n+=m;
}
printf("%d\n",t-1);
for(i=0;i<t-1;i++)
printf("%lld--->",a[i]);
printf("%lld\n",a[i]);
}
return 0;
}