题目57

来源:互联网 发布:淘宝买什么东西比较好 编辑:程序博客网 时间:2024/04/28 23:08
#include<stdio.h>#include<math.h>#include<stdlib.h>void swap1(int *p, int *q);void swap2(int *p, int *q);int main(){int x, m, i, y, s, z;int a, b, c, d;scanf("%d", &m);getchar();while(m--){scanf("%d", &x);getchar();i = 0;if(x == 6174){printf("1\n");getchar();continue; } while(1){a = x%10;b = (x%100-a)/10;c = (x%1000-b*10-a)/100;d = (x-c*100-b*10-a)/1000;swap1(&a, &b);swap1(&a, &c);swap1(&a, &d);swap1(&b, &c);swap1(&b, &d);swap1(&c, &d);y = 1000*a+100*b+10*c+d;swap2(&a, &b);swap2(&a, &c);swap2(&a, &d);swap2(&b, &c);swap2(&b, &d);swap2(&c, &d);z = 1000*a+100*b+10*c+d;x = y - z;i++;if(x == 6174){break; } }printf("%d\n", i+1);}return 0; }void swap1(int *p, int *q){int t;if(*p < *q){t  = *p;*p = *q;*q = t;}}void swap2(int *p, int *q){int t;if(*p > *q){t  = *p;*p = *q;*q = t;}}

0 0
原创粉丝点击