NLP菜鸡 2019-04-19 18:33 采纳率: 0%
浏览 523

PAT数字黑洞求大神解答

求大神看一下 ,测试了下第一次循环结尾num的值都是正确的 ,但进入第二次循环a和b的值就没了 导致程序就停止了
图片说明

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(char a,char b)
{
    return a>b;
}
int tranfnum(string num)            //字符串转换为数字
{
    int sum = 0;
    int t = 1000;
    for(int i = 0;i < 4;i++)
    {
        int n = (num[i]-'0')*t;
        sum += n;
        t/=10;
    }
    return sum;
}
string tranchar(int num)            //数字转换为字符
{
    string result;
    int t = 1000;
    for(int i = 0;i < 4;i++)
    {
        if(num < t)
        {
            result[i] = '0';
            t/=10;
            continue;
        }
        result[i] = (num/t) + '0';
        num%=t;
        t/=10;
    }
    return result;

}
int main()
{
    std::ios::sync_with_stdio(false);
    string num,a,b;
    cin >> num;
    while(1)
    {
        a = num;b = num;
        sort(a.begin(),a.end());                //升序
        sort(b.begin(),b.end(),cmp);          //降序
        int frist = tranfnum(a);
        int sconed = tranfnum(b);
        int result = sconed - frist;
        if(result == 0)
        {
            cout << b << " - " << a << " = " << 0000 << endl;
            break;
        }
        cout << b << " - " << a << " = ";
        int t = 1000;
        int temp = result;
        while(temp < t)
        {
            cout << 0;
            t/=10;
        }
        cout << result << endl;
        if(result == 6174)
            break;
        num = tranchar(result);
    }
    return 0;
}

  • 写回答

1条回答 默认 最新

  • 杨莉华 2022-02-09 23:50
    关注

    #include
    #include
    #include
    #include
    using namespace std;
    //字符串转数字
    int string_int(string num1)
    {
    int res1;
    stringstream ss;
    ss << num1;
    ss >> res1;
    return res1;
    }
    //数字转字符串
    string int_string(int num)
    {
    string res;
    stringstream ss;
    ss << num;
    ss >> res;
    return res;
    }
    //字符串排序
    bool cmp(char a, char b) { return a > b; }
    int main()
    {
    string str, str1, str2, str3, str4;
    cin >> str;
    while (str.size() < 4)
    {
    str = "0" + str;//在str1的前面加0,补足4位;若在str1后面加0,应写为str1 = str1+“0”;
    }
    str1 = str;
    str2 = str;
    sort(str1.begin(), str1.end());
    sort(str2.begin(), str2.end(), cmp);
    while (str2 != str1 && string_int(str2) - string_int(str1) != 6174)
    {
    str4 = int_string(string_int(str2) - string_int(str1));
    while (str4.size() < 4)
    {
    str4 = "0" + str4;
    }
    cout << str2 << " - " << str1 << " = " << str4 << endl;
    str1 = str4;
    str2 = str4;
    sort(str1.begin(), str1.end());
    sort(str2.begin(), str2.end(), cmp);
    }
    if (str2 == str1)
    cout << str2 << " - " << str1 << " = " << "0000" << endl;
    else
    cout << str2 << " - " << str1 << " = " << 6174 << endl;
    return 0;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题