Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
输入少于或者大于4位都要No!!#include<string> #include<iostream> #include<algorithm> #include<cmath> using namespace std; bool cmp_1(char a, char b) { return a>b; } string tostring(int x) { string ans = ""; while (x) { ans= (char)('0' + x % 10)+ans; x /= 10; } return ans; } int toint(string x) { int ans = 0,i=0; for (int k = x.length() - 1; k >= 0; k--) { ans += (int)(x[k] - '0')*pow(10.0, i++); } return ans; } int main() { string in; while (cin >> in) { if (in == "-1") break; cout << "N=" << in << ":" << endl; if ((in.length()!=4)||in[0] == in[1] && in[1] == in[2] && in[2] == in[3]) { cout << "No!!" << endl; continue; } sort(in.begin(), in.end(), cmp_1); string re = in; reverse(re.begin(), re.end()); int t = 0; while (true) { t++; int ans = toint(in) - toint(re); cout << toint(in) << "-" << toint(re) << "=" << ans << endl; if (ans == 0 || ans == 6174) { break; } else{ in = tostring(ans); sort(in.begin(), in.end(),cmp_1); re = in; reverse(re.begin(), re.end()); } } cout << "Ok!! " << t << " times" << endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator