#includeint main(void) { int num, count, time, max, min; int str[4]; scanf("%d", &num); do{ str[0] = num / 1000; str[1] = (num - str[0] * 1000) / 100; str[2] = (num - str[0] * 1000 - str[1] * 100) / 10; str[3] = num % 10; if (str[0] == str[1] && str[0] == str[2] && str[0] == str[3]) { printf("%4d - %4d = %04d", num, num, 0); break; } for (count = 0; count < 4; count ++) { for (time = 0; time < 3 - count;time ++) { if (str[time] >= str[time+1]) { int temp = 0; temp = str[time]; str[time] = str[time + 1]; str[time + 1] = temp; } } } max = (str[3] * 1000 + str[2] * 100 + str[1] * 10 + str[0]); min = (str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3]); num = max - min; printf("%04d - %04d = %04dn", max, min, num); }while (num != 6174); return 0; } /*总结: 由于用一个数组直接分别去存储用户输入的四位数,不能满足循环后仍然可以这么使用,所以再循环里用用数组分别存储数值。 然后就是将一个数组中的元素合并成一个数值,可以考虑通过乘10的一定倍数后相加 再者,在子函数中改变数组的值,相应的子函数的形参也要写成相应的格式
practice makes perfect!
加油



