题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968
思路理清以后就很简单了,主要一开始各部分的功能顺序没有想好,这题应该是必须要用数组的把
代码
#include<algorithm>#include<vector>#include<iostream>using namespace std;int convert(int &num){vector<int> list(4);for(int i = 0; i < 4; i++){list[i] = num % 10;num /= 10;}sort(list.begin(),list.end());int temp = list[0]*1000 + list[1]*100 + list[2]*10 + list[3];num = list[3]*1000 + list[2]*100 + list[1]*10 + list[0];return temp;}int main(){int n1, n2, result = -1;scanf("%d",&n1);while(result != 0&&result!=6174){n2 = convert(n1);result = n1 - n2;printf("%04d - %04d = %04d\n", n1, n2, result);n1 = result;}}
