example1
- https://pintia.cn/problem-sets/994805342720868352/problems/994805400954585088
#include <iostream>
#include <algorithm>
using namespace std;
void intToArray(int n,int arr[]){
for(int i=0;i<4;++i){
arr[i] = n%10;
n /= 10;
}
}
void arrayToInt(int arr[],int &n){
int multiplicand = 1;
n=0;
for(int i=0;i<4;++i){
n += arr[i]*multiplicand;
multiplicand*=10;
}
}
void arrayToIntAndReverse(int arr[],int &n){
int multiplicand = 1;
n=0;
for(int i=3;i>=0;--i){
n += arr[i]*multiplicand;
multiplicand*=10;
}
}
int main()
{
int inputFigure;
cin>>inputFigure;
int inputArr[4];
do{
intToArray(inputFigure,inputArr);
int flag;
for(flag = 1;flag<4;++flag){
if(inputArr[flag] != inputArr[0]){
break;
}
}
if(flag == 4){
//each of these four digits is the same
printf("%04d - %04d = 0000\n",inputFigure,inputFigure);
return 0;
}
sort(inputArr,inputArr+4);
int smallFigure,bigFigure;
arrayToInt(inputArr,bigFigure);
arrayToIntAndReverse(inputArr,smallFigure);
inputFigure = bigFigure - smallFigure;
printf("%04d - %04d = %04d\n",bigFigure,smallFigure,inputFigure);
}while(inputFigure!=6174);
return 0;
}