#python def zuida(n): gw = n % 10 sw = (n // 10) % 10 bw = n // 100 a = [gw, sw, bw] a.sort() max=a[2]*100+a[1]*10+a[0] min=a[2]+a[1]*10+a[0]*100 cha = max - min return cha,max,min n=int(input('请输入')) cha,max,min=zuida(n) count=1 while cha!=495: cha, max, min = zuida(n) print(f'{count}:{max}-{min}={cha}') n = cha count+=1
#c语言
第一种:
#include<stdio.h>
int zuida(int n)
{
int gw, sw, bw;
int max;
gw = n % 10;
sw = (n / 10) % 10;
bw = n / 100;
int a[3] = { gw,sw,bw };
int t, i, j;
for (i = 0; i < 3 - 1; i++)
{
for (j = 0; j < 3 - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
max = a[2] * 100 + a[1] * 10 + a[0];
return max;
}
int zuixiao(int n)
{
int min;
int gw, sw, bw;
gw = n % 10;
sw = (n / 10) % 10;
bw = n / 100;
int a[3] = { gw,sw,bw };
int t, i, j;
for (i = 0; i < 3 - 1; i++)
{
for (j = 0; j < 3 - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
min = a[0] * 100 + a[1] * 10 + a[2];
return min;
}
int main()
{
int n = 0, count = 1;
scanf_s("%d", &n);
do {
printf("%d:%d-%d=%d\n", count, zuida(n), zuixiao(n), zuida(n) - zuixiao(n));
n = zuida(n) - zuixiao(n);
count++;
} while (n != 495);
}
第二种(简单)
#include<stdio.h>
void da_xiao(int n,int b[])
{
int gw, sw, bw;
int max;
gw = n % 10;
sw = (n / 10) % 10;
bw = n / 100;
int a[3] = { gw,sw,bw };
int t, i, j;
for (i = 0; i <3-1; i++)
{
for (j = 0; j < 3-1 - i; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
b[0] = a[2] * 100 + a[1] * 10 + a[0];
b[1] = a[0] * 100 + a[1] * 10 + a[2];
}
int main()
{
int n,count=1;
scanf_s("%d", &n);
int c[2];
da_xiao(n, c);
do {
da_xiao(n, c);
printf("%d:%d-%d=%d\n",count,c[0],c[1],c[0]-c[1]);
n = c[0] - c[1];
count++;
} while (n != 495);
}