「已注销」 2021-08-31 10:46 采纳率: 50%
浏览 49
已结题

想问一下,为什么PAT乙级1019测试点2通不过,说明我的代码哪里有问题,修改后能通过测试点2的,优先采纳

PAT 乙级 1019 千名教师建设,万道高质量题目,百万用户拼题的程序设计实验辅助教学平台 https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968

img

img

img

我的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    int a, res[10];
    cin >> a;
    
    if (a == 6174) printf("7641 - 1467 = 6174\n");
    
    int z = a;
    int j = 0;
    while (z)
    {
        res[j ++ ] = z % 10;
        z /= 10;
    }
    
    bool flag = false;
    for (int n = 0; n < j; n ++ )
        if (res[n] != res[0])
        {
            flag = true;
            break;
        }
    
    memset(res, 0, sizeof res);
            
    
    if (!flag) printf("%d - %d = 0000", a, a);
    else
    {
        while (a != 6174)
        {
            int i = 0, x, y;
            while (a)
            {
                res[i ++ ] = a % 10;
                a /= 10;
            }
            sort(res, res + 4, cmp);
            x = res[0] * 1000 + res[1] * 100 + res[2] * 10 + res[3];
            y = res[3] * 1000 + res[2] * 100 + res[1] * 10 + res[0];
            
            a = x - y;
            
            printf("%04d - %04d = %04d\n", x, y, a);
            memset(res, 0, sizeof res);
        }
    }
    
    return 0;
}

  • 写回答

1条回答 默认 最新

  • Admini$trat0r .net领域新星创作者 2021-08-31 10:50
    关注
    
    #include<iostream>
    #include<string.h>  //string
    #include<algorithm>  //sort
    #include<math.h>  //pow
    using namespace std;
     
    int main()
    {
      string str;
      int a[4]={0,0,0,0};
      int d=0,x=0,cha=0;  //较大值 较小值初始化  输入值
      cin>>str;
      int len=str.length();
      for(int i=0;i<len;i++){
        a[i]=str[i]-'0';
      }
      
      if (a[1]==a[2]&&a[2]==a[3]&&a[3]==a[0])  {
          cout<<str<<" - "<<str<<" = "<<"0000";     //这样写如果0000不带引号的话,第一个测试点错误   
         // printf("%04d - %04d = %04d\n",a[0]*1111,a[0]*1111,0);
          
         }
      else{
        while(cha!=6174)
        {
          sort(a,a+4,greater<int>());
        
          
          
            for(int i=0;i<4;i++){
              d=10*d+a[i];
              x=10*x+a[3-i];   //注意789  应该表示为0789
            }
            cha= d-x;
           // cout<<d<<" - "<<x<<" = "<<cha<<endl;   //可能是3210-123   格式错误
            printf("%04d - %04d = %04d\n",d,x,cha);
            
            a[3]=cha%10;
            a[2]=(cha/10)%10;
            a[1]=(cha/100)%10;
            a[0]=cha/1000;
            d=0;
            x=0;
          
        }
      }
      
      return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月31日
  • 修改了问题 8月31日
  • 创建了问题 8月31日

悬赏问题

  • ¥15 大二软件工程基础大题
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥60 如何批量获取json的url
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数