蓝桥杯 1530: [蓝桥杯][算法提高VIP]数字黑洞

基本思想:

和PAT数字黑洞类似,但是比哪个简单,没有输出和判零情况;

关键点:

无;

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector> 
#include<string>
#include<math.h>
#include<algorithm>
#include<cstring>
using namespace std;

vector<int>vec;

int to_number() {
    int n = 0;
    for (int i = 0; i < 4; i++) {
        n = n * 10 + vec[i];
    }
    return n;
}

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

int cnt_fun() {
    sort(vec.begin(), vec.end(), cmp);
    int a = to_number();
    sort(vec.begin(), vec.end());
    int b = to_number();
    return a - b;
}

void vector_fun(int n) {
    vec.resize(0);
    for (int i = 0; i < 4; i++) {
        vec.push_back(n % 10);
        n /= 10;
    }
}

int main(){
    int n;
    cin >> n;
    vector_fun(n);
    int cnt = 0;
    while (n!=6174){
        cnt++;
        n = cnt_fun();
        vector_fun(n);
        //cout << n<<" ";
    }
    cout << cnt;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12291171.html