「已注销」 2021-08-31 11:42 采纳率: 50%
浏览 60
已结题

想问一下,测试点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;
}
  • 写回答

2条回答 默认 最新

  • aabyte 2021-08-31 12:45
    关注

    改好了,有帮助望采纳,谢谢!

    #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");
        if(a<10) a*=1000; //改的地方一 
        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("%04d - %04d = 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;
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月8日
  • 已采纳回答 8月31日
  • 修改了问题 8月31日
  • 创建了问题 8月31日

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?