牛客网PAT练兵场-数字黑洞

题解:循环即可

题目地址:https://www.nowcoder.com/questionTerminal/2e6a898974064e72ba09d05a60349c9e

 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Ycute
 5 * Date : 2019-10-31-16.37.32
 6 * Description : 模拟输出
 7 */
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cmath>
11 #include<cstring>
12 #include<algorithm>
13 using namespace std;
14 
15 //降序
16 int cmp0(int a,int b){
17     return a>b;
18 }
19 //升序
20 bool cmp1(int a,int b){
21     return a<b;
22 }
23 int main(){
24     int in[4];
25     int temp[4];
26     int p;
27     scanf("%d",&p);
28     //scanf("%1d%1d%1d%1d",&in[0],&in[1],&in[2],&in[3]);
29     temp[0]=in[0]=p/1000;
30     temp[1]=in[1]=p/100%10;
31     temp[2]=in[2]=p%100/10;
32     temp[3]=in[3]=p%10;
33     while(1){
34         if(in[0]==in[1]&&in[1]==in[2]&&in[2]==in[3]){
35             printf("N - N = 0000\n");
36             break;
37         }
38         int t1[4];
39         int t2[4];
40         t1[0]=temp[0];
41         t1[1]=temp[1];
42         t1[2]=temp[2];
43         t1[3]=temp[3];
44         t2[0]=temp[0];
45         t2[1]=temp[1];
46         t2[2]=temp[2];
47         t2[3]=temp[3];
48         sort(t1,t1+4,cmp0);
49         sort(t2,t2+4,cmp1);
50         int num0=t1[0]*1000+t1[1]*100+t1[2]*10+t1[3];
51         int num1=t2[0]*1000+t2[1]*100+t2[2]*10+t2[3];
52         int num2=num0-num1;
53         temp[0]=num2/1000;
54         temp[1]=num2/100%10;
55         temp[2]=num2%100/10;
56         temp[3]=num2%10;
57         printf("%d%d%d%d - %d%d%d%d = %d%d%d%d\n",t1[0],t1[1],t1[2],t1[3],t2[0],t2[1],t2[2],t2[3],temp[0],temp[1],temp[2],temp[3]);
58         if(temp[0]==6&&temp[1]==1&&temp[2]==7&&temp[3]==4) break ;
59     }
60     return 0;
61 }

猜你喜欢

转载自www.cnblogs.com/cutelife/p/11772791.html