1019 数字黑洞 (20)
链接 点我
思想:
主要使用 algorithm 库的自动排序算法
#include <stdio.h> #include <algorithm> using namespace std; int array[4] = {0}; void val_to_array(int val) { int i = 0; while (val != 0) { array[i] = val % 10; val = val / 10; i++; } } bool cmp (int a, int b) { return a > b; } int array_to_val (int * num ) { int result = 0; int i = 0; int pred = 1; for (i = 0; i < 4; i++) { result += array[i] * pred; pred *= 10; } return result; } int main (void) { int val = 0; int result = 0; int Min, Max; scanf ("%d", &val); while (1) { val_to_array(val); sort(array, array + 4); Max = array_to_val(array); sort(array, array + 4, cmp); Min = array_to_val(array); val = Max - Min; printf ("%04d - %04d = %04d ", Max, Min, val); if ((val) == 0 || (val) == 6174) break; } return 0; return 0; }