数字输入,字符串会TE
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int quick(int a, int b)
{
int c = 1;
while(b)
{
if(b & 1)
{
c = a * c;
}
a = a * a;
b >>= 1;
}
return c;
}
int main()
{
int n, n1, n2 ,s;
int a[10], b[10], c[10];
while(cin >> n)
{
n1 = n;
n2 = 0;
for(int i = 3; i >= 0; i--)
{
a[i] = n % 10;
n /= 10;
n2 += a[i] * quick(10, i);
}
if(n1 == n2)
{
printf("%d - %d = 0000\n",n1, n2);
//break;
continue;
}
while(1)
{
sort(a, a + 4);
n1 = n2 = s = 0;
int x = 0;
for(int i = 0; i < 4; i++)
{
b[i] = a[3 - i];
printf("%d", b[i]);
}
printf(" - ");
for(int i = 0; i < 4; i++)
{
printf("%d", a[i]);
}
printf(" = ");
for(int i = 3; i >= 0; i--)
{
if((b[i] - a[i]) >=0)
x = b[i] - a[i];
else
{
x = 10 + b[i] - a[i];
b[i - 1]--;
}
c[i] = x;
//printf("%d", x);
//cout << endl;
//cout << "======="<<s << "===="<< endl;
s += x * quick(10, 3 - i);
}
for(int i = 0; i < 4; i++)
printf("%d", c[i]);
printf("\n");
//cout << s << endl;
if(s == 6174)
break;
for(int i = 0; i < 4; i++)
a[i] = c[i];
n1 = n2 = s = x = 0;
}
}
return 0;
}