1079. 延迟的回文数 (20)
生活随笔
收集整理的這篇文章主要介紹了
1079. 延迟的回文数 (20)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個 k+1 位的正整數 N,寫成 ak...a1a0 的形式,其中對所有 i 有 0 <= ai < 10 且 ak > 0。N 被稱為一個回文數,當且僅當對所有 i 有 ai = ak-i。零也被定義為一個回文數。
非回文數也可以通過一系列操作變出回文數。首先將該數字逆轉,再將逆轉數與該數相加,如果和還不是一個回文數,就重復這個逆轉再相加的操作,直到一個回文數出現。如果一個非回文數可以變出回文數,就稱這個數為延遲的回文數。(定義翻譯自 https://en.wikipedia.org/wiki/Palindromic_number)
給定任意一個正整數,本題要求你找到其變出的那個回文數。
輸入格式:
輸入在一行中給出一個不超過1000位的正整數。
輸出格式:
對給定的整數,一行一行輸出其變出回文數的過程。每行格式如下
A + B = C其中A是原始的數字,B是A的逆轉數,C是它們的和。A從輸入的整數開始。重復操作直到C在10步以內變成回文數,這時在一行中輸出“C is a palindromic number.”;或者如果10步都沒能得到回文數,最后就在一行中輸出“Not found in 10 iterations.”。
輸入樣例 1: 97152 輸出樣例 1: 97152 + 25179 = 122331 122331 + 133221 = 255552 255552 is a palindromic number. 輸入樣例 2: 196 輸出樣例 2: 196 + 691 = 887 887 + 788 = 1675 1675 + 5761 = 7436 7436 + 6347 = 13783 13783 + 38731 = 52514 52514 + 41525 = 94039 94039 + 93049 = 187088 187088 + 880781 = 1067869 1067869 + 9687601 = 10755470 10755470 + 07455701 = 18211171 Not found in 10 iterations.代碼: #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std;int main() {char s[1000],t[1000];int c = 0;cin>>s;reverse_copy(s,s + strlen(s),t);int len = strlen(s);t[len] = '\0';//結尾的0好像是不會復制過去的,所以記得結尾0while(strcmp(s,t) && c < 10){c ++;cout<<s<<" + "<<t<<" = ";int d = 0;for(int i = 0;i < len;i ++){d += s[i] - '0' + t[i] - '0';t[i] = d % 10 + '0';d /= 10;}if(d)t[len ++] = '0' + d;t[len] = '\0';reverse_copy(t,t + len,s);s[len] = '\0';cout<<s<<endl;}if(c == 10)cout<<"Not found in 10 iterations."<<endl;else cout<<s<<" is a palindromic number."<<endl; }
?
轉載于:https://www.cnblogs.com/8023spz/p/8082008.html
總結
以上是生活随笔為你收集整理的1079. 延迟的回文数 (20)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件测试白皮书-判定表法
- 下一篇: js中document.referrer