Description
While driving the other day, John looked down at his odometer, and it read 100000. John was pretty excited about that. But, just one mile further, the odometer read 100001, and John was REALLY excited! You see, John loves palindromes - things that read the same way forwards and backwards. So, given any odometer reading, what is the least number of miles John must drive before the odometer reading is a palindrome? For John, every odometer digit counts. If the odometer reading was 000121, he wouldn't consider that a palindrome.
Input
There will be several test cases in the input. Each test case will consist of an odometer reading on its own line. Each odometer reading will be from 2 to 9 digits long. The odometer in question has the number of digits given in the input so, if the input is 00456, the odometer has 5 digits. There will be no spaces in the input, and no blank lines between input sets. The input will end with a line with a single 0.
Output
For each test case, output the minimum number of miles John must drive before the odometer reading is a palindrome. This may be 0 if the number is already a palindrome. Output each integer on its own line, with no extra spaces and no blank lines between outputs.
Sample Input
100000 100001 000121 00456 0
Sample Output
1 0 979 44
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 1010 const int inf=0x7fffffff; //无限大 int step; int check(string s) { //cout<<s<<" "<<step<<endl; for(int i=0;i<s.size();i++) { if(s[i]!=s[s.size()-1-i]) return 0; } return 1; } int pingfang(int x,int y) { if(y==0) return 1; int ans=1; for(int i=1;i<=y;i++) { ans*=x; } return ans; } int main() { string s; while(cin>>s) { string s1="0"; if(s==s1) break; ll num=0; step=0; int len=0; while(!check(s)) { if(s[s.size()-1-len]==s[len]) { len++; continue; } else if(s[s.size()-1-len]<s[len]) { step+=((int)(s[len]-'0')-(int)(s[s.size()-1-len]-'0'))*pingfang(10,len); s[s.size()-1-len]=s[len]; len++; } else { step+=((int)(s[len]-'0')+10-(int)(s[s.size()-1-len]-'0'))*pingfang(10,len); s[s.size()-1-len]=s[len]; for(int i=len+1;i<s.size();i++) { if(s[s.size()-1-i]=='9') { s[s.size()-1-i]='0'; } else { s[s.size()-1-i]++; break; } } len=0; } } cout<<step<<endl; } return 0; }