本文主要是介绍PAT 19/95 乙级 数字黑洞,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include<stdio.h>
#include<stdlib.h>
int cmp1( const void *a,const void *b)
{return (*(int*)a) > (*(int*)b) ? 1 : -1;
}
int cmp2(const void *a, const void *b)
{return (*(int*)a) <(*(int*)b) ? 1 : -1;
}
int main()
{int a,a1,sum3;int b[5];int d[5];scanf("%d",&a);a1 = a;for (int i=0;i<4;i++){b[i] = a % 10;a /= 10;}for (int i = 0; i<4; i++){d[i] = a1 % 10;a1 /= 10;}qsort(b,4,sizeof(int),cmp1);qsort(d,4, sizeof(int), cmp2);int sum1 = b[0] * 1000 + b[1] * 100 + b[2] * 10 + b[3] ;int sum2 = d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3];if (sum1 == sum2){sum3 = sum1 - sum2;printf("%d - %d = %04d",sum1,sum2,sum3);}else{sum3 = sum2 - sum1;printf("%d - %04d = %d\n", sum2, sum1, sum3);while (sum3 != 6174){a = sum3;a1 = sum3;for (int i = 0; i < 4; i++){b[i] = a % 10;a /= 10;}for (int i = 0; i < 4; i++){d[i] = a1 % 10;a1 /= 10;}qsort(b, 4, sizeof(int), cmp1);qsort(d, 4, sizeof(int), cmp2);int sum1 = b[0] * 1000 + b[1] * 100 + b[2] * 10 + b[3];int sum2 = d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3];sum3 = sum2 - sum1;if(sum3==6174)printf("%d - %04d = %d", sum2, sum1, sum3);elseprintf("%d - %04d = %d\n", sum2, sum1, sum3);}}return 0;
}
1.四位格式输出 0补位 不熟
这篇关于PAT 19/95 乙级 数字黑洞的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!