• PAT1019数字黑洞


    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <sstream>

    using namespace std;

    string int2string(int ival)
    {
    stringstream stream; //创建一个stringstream, 即可以用来输入,也可用来输出数据
    stream << ival; //将int型值传给stringstream,
    return stream.str(); //stringstream的成员函数str() 返回一个相应的字符串
    }
    int STOI(string s)
    {
    stringstream stream;
    stream << s;
    int t;
    stream >> t;
    return t;
    }
    int main()
    {
    string num_str;
    cin >> num_str;
    for (int i = num_str.size(); i < 4; i++)
    num_str += '0';


    while (true)
    {
    // 先检查数字字符是否全部一样
    if (num_str.find_first_not_of(num_str[0]) == string::npos)
    {
    cout << num_str << " - " << num_str << " = " << "0000" << endl;
    break; // 结束循环
    }
    // 数字字符升序排序
    sort(num_str.begin(), num_str.end());
    int num_ascend = STOI(num_str);
    // 数字字符降序排序
    reverse(num_str.begin(), num_str.end());
    int num_descent = STOI(num_str);
    if (num_ascend != num_descent) // 四位数字不全相同情况
    {
    if (num_descent - num_ascend == 6174)
    {
    cout << num_descent << " - " << num_ascend << " = "
    << 6174 << endl;
    break; // 结束循环
    }
    else //差不为6174
    {
    string num_ascend_str = int2string(num_ascend);
    if (num_ascend_str.size() != 4) // 控制输出的格式,必须有4位填充满
    num_ascend_str.insert(0, 4 - num_ascend_str.size(), '0');
    cout << num_descent << " - " << num_ascend_str << " = "
    << num_descent - num_ascend << ' ';
    num_str = int2string(num_descent - num_ascend);
    }
    }
    }
    getchar();
    getchar();
    return 0;
    }

  • 相关阅读:
    java自定义注解教程
    java8 LocalDateTime时间格式化
    java8新特性Stream用法详解
    java将数组转换成list集合
    elestaticsearch原生写法创建mapping
    springboot-mybatis-plus生成器
    jQuery.bind() 函数详解
    CSS3 中的 rem 值与 px 之间的换算
    console.log的应用
    JQuery中$(document)是什么意思有什么作用
  • 原文地址:https://www.cnblogs.com/zxzmnh/p/11627056.html
Copyright © 2020-2023  润新知