只剩最后一个测试点没过了,怎么回事?
查看原帖
只剩最后一个测试点没过了,怎么回事?
742265
andywoww楼主2024/8/3 21:16
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <sstream>
#include <map>
#include <vector>
#define ll long long
using namespace std;
stringstream sstr;
string valstr;
inline void mret(int e) {
    // system("pause");
    exit(e);
}
map<ll, ll> mp;
int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll n;
    vector<ll> list;
    while (cin >> n) {
        sstr.clear();
        sstr << n;
        sstr >> valstr;
        ll a, b, c, flag = 1;
        if (valstr.size() == 4) {
            cout << 6174 << endl;
            sstr.clear();
            continue;
        }
        while (1) {
            sort(valstr.begin(), valstr.end());
            string revstr = valstr;
            reverse(revstr.begin(), revstr.end());
            sstr.clear(), sstr << revstr;
            sstr >> a;
            sstr.clear(), sstr << valstr;
            sstr >> b;
            c = a - b;
            while (to_string(c).size() < revstr.size())
                c *= 10;
            if (mp.find(c) != mp.end()) {
                flag = mp[c];
                break;
            }
            list.push_back(c);
            valstr = to_string(c);
            mp[c] = list.size();
        }
        for (ll i = flag - 1; i < list.size(); i++)
            cout << list[i] << ' ';
        cout << endl;
        mp.clear(), list.clear();
        sstr.clear();
    }
    mret(0);
}

到现在还没找出来原因

2024/8/3 21:16
加载中...