1 条题解

  • 0
    @ 2022-8-24 11:50:08

    #include <bits/stdc++.h> using namespace std; int w[5]; bool ans = 1; bool cmp(int a, int b) { return a > b; } int main() { int n; cin >> n; while (ans) { int a = n / 1000; int b = n / 100 % 10; int c = n / 10 % 10; int d = n % 10;//各位数字 w[1] = a; w[2] = b; w[3] = c; w[4] = d;//数组存储 sort(w + 1, w + 5, cmp);//最大数字 n = 1000 * w[1] + 100 * w[2] + 10 * w[3] + w[4]; a = n / 1000; b = n / 100 % 10; c = n / 10 % 10; d = n % 10; cout << n << "-" << 1000 * d + 100 * c + 10 * b + a << "=" << n - 1000 * d - 100 * c - 10 * b - a;//输出每一行内容 cout << endl; if (n - 1000 * d - 100 * c - 10 * b - a == 6174) { ans = 0; }//执行前片代码后的判断 n -= (1000 * d + 100 * c + 10 * b + a);将n重新赋值 } return 0; }

    • @ 2022-8-24 11:51:36

      本人方法比较拙劣, 有许多需要改进的地方

    • @ 2024-4-19 21:18:03

      更舒服了

      #include <bits/stdc++.h> 
      using namespace std; 
      int w[5]; 
      bool ans = 1; 
      bool cmp(int a, int b) 
      { 
          return a > b; 
      } 
      int main() 
      { 
          int n; 
          cin >> n; 
          while (ans) { 
              int a = n / 1000; 
              int b = n / 100 % 10; 
              int c = n / 10 % 10;
               int d = n % 10;
              w[1] = a; 
              w[2] = b; 
              w[3] = c; 
              w[4] = d;
              sort(w + 1, w + 5, cmp); 
              n = 1000 * w[1] + 100 * w[2] + 10 * w[3] + w[4]; 
              a = n / 1000;
              b = n / 100 % 10; 
              c = n / 10 % 10; 
              d = n % 10; 
              cout << n << "-" << 1000 * d + 100 * c + 10 * b + a << "=" << n - 1000 * d - 100 * c - 10 * b - a;
              cout << endl; 
              if (n - 1000 * d - 100 * c - 10 * b - a == 6174) { 
                  ans = 0; 
              } 
                  n -= (1000 * d + 100 * c + 10 * b + a) ;
          } 
              return 0; 
      }
      
  • 1

信息

ID
1014
时间
1000ms
内存
128MiB
难度
6
标签
递交数
19
已通过
11
上传者