poj 1350 Cabric Number Problem

来源:互联网 发布:c#网络编程 pdf 编辑:程序博客网 时间:2024/04/29 18:19
//这一题的陷阱多多,贡献了2次OLE,一次WA!哎!陷阱:需要保证输入的数必须为4位,不能大于或小于4位,要不就OLE! 输出的是No,Ok而不是no ,ok #include <iostream>#include <string>#include <sstream>#include <algorithm>using namespace std;bool cmp(char a, char b){     return a > b;}int main(){    int n, c, temp1, temp2, ans;    string str, str1, str2;    bool flag, flag1, flag2;    while (cin >> n && n != -1)    {          cout << "N=" << n << ":" << endl;          c = 0;          flag = flag1 = flag2 = false;          while (1)          {              stringstream stream;              stream << n;              stream >> str;              if (!flag2)//对输入的数判断是否为4位数,第二次就可以跳过,不用再判断!               {                  flag2 = true;                  if (str.length() != 4)                  {                      flag = true;                      break;                  }              }              str1 = str2 = str;              sort(str1.begin(), str1.end());              sort(str2.begin(), str2.end(), cmp);              if (!flag1)              {                  if (str1 == str2)                   {                      flag = true;                      break;                  }              }              stringstream stream1;              stream1 << str1;              stream1 >> temp1;              stringstream stream2;              stream2 << str2;              stream2 >> temp2;              ans = temp2 - temp1;              c++;              flag1 = true;              cout << temp2 << "-" << temp1 << "=" << ans << endl;              if (ans == 6174 || ans == 0) break;              else n = ans;          }          if (flag)             cout << "No!!" << endl;          else             cout << "Ok!! " << c << " times" << endl;    }        system("pause");}

原创粉丝点击