C language black hole trap

Target:

The program inputs an integer less than 1000 and the three numbers are not all equal, please output the rearrangement and difference process of entering the black hole. 495 is a very magical number, known as the number of black holes or traps.

hint: 

Given any positive integer less than 1000 , a three-digit number can be obtained by adding 0 to the front (two-digit numbers are filled with one 0, and one-digit numbers are filled with two 0s). If the three digits of this three-digit number are not all equal , then after a finite number of "rearrangement and difference" operations (the largest number after rearrangement of the numbers that make up the number minus the smallest number after rearrangement), 495 will always be obtained.

For example, for the integer 80, 080 can be obtained after padding the front digit with 0, and 800,008 can be obtained after rearranging. At this time, the maximum number that can be obtained is 800, and the minimum number is 008 (that is, 8). Then you only need 4 rearrangements to find the difference to get 495, the process is as follows:

1:800-8=792 //第一次

2:972-279=693 //第二次,将第一次的结果进行重排求差

3:963-369=594 //第三次,将第二次的结果进行重排求差

4:954-459=495 //第四次以此类推

Program implementation:

verbose output:

#include <stdio.h>
int main()
{
    int n;
    printf("请输入一个三位数:\n");
    scanf("%d",&n);
    int a[3],b[6];
    int s=1,max,min;
    do{
    	a[0]=n%10;n=n/10;
        a[1]=n%10;n=n/10;
        a[2]=n;
    
        b[0]=a[0]+a[1]*10+a[2]*100;
        b[1]=a[0]+a[2]*10+a[1]*100;
        b[2]=a[1]+a[0]*10+a[2]*100;
        b[3]=a[1]+a[2]*10+a[0]*100;
        b[4]=a[2]+a[0]*10+a[1]*100;
        b[5]=a[2]+a[1]*10+a[0]*100;
        
        printf("第%d次 可以排成如下几种形式:\n",s);
        s++;
        for(int i=0;i<6;i++)     //输出排好的数字 
        {
        	printf("%03d\n",b[i]);
		}

        max=b[0];
        min=b[0];
        for(int i=0;i<6;++i) //最大值 
		{
            if(b[i] > max)   
			{ 
                max=b[i];
            }
        }
        
        for(int i=0;i<6;++i)  //最小值 
		{
            if(b[i] < min)   
			{ 
                min=b[i];
            }
        }
        n=max-min;    //差值 
        printf("max=%d\n",max);
        printf("min=%d\n",min);
        printf("%d-%d=%d\n\n",max,min,max-min);
    }while(n!=495);
    return 0;
}
请输入一个三位数:
8
第1次 可以排成如下几种形式:
008
008
080
800
080
800
max=800
min=8
800-8=792

第2次 可以排成如下几种形式:
792
972
729
279
927
297
max=972
min=279
972-279=693

第3次 可以排成如下几种形式:
693
963
639
369
936
396
max=963
min=369
963-369=594

第4次 可以排成如下几种形式:
594
954
549
459
945
495
max=954
min=459
954-459=495

Profile output:

#include <stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a[3],b[6];
    int s=1,max,min;
    do{
    	a[0]=n%10;n=n/10;
        a[1]=n%10;n=n/10;
        a[2]=n;
    
        b[0]=a[0]+a[1]*10+a[2]*100;
        b[1]=a[0]+a[2]*10+a[1]*100;
        b[2]=a[1]+a[0]*10+a[2]*100;
        b[3]=a[1]+a[2]*10+a[0]*100;
        b[4]=a[2]+a[0]*10+a[1]*100;
        b[5]=a[2]+a[1]*10+a[0]*100;
        
        max=b[0];
        min=b[0];
        for(int i=0;i<6;++i) //最大值 
		{
            if(b[i] > max)   
			{ 
                max=b[i];
            }
        }
        
        for(int i=0;i<6;++i)  //最小值 
		{
            if(b[i] < min)   
			{ 
                min=b[i];
            }
        }
        n=max-min;    //差值 
        printf("%d:%d-%d=%d\n",s,max,min,max-min);
        s++;
    }while(n!=495);
    return 0;
}
输入:123 
输出: 
1:321-123=198 
2:981-189=792 
3:972-279=693 
4:963-369=594 
5:954-459=495

输入:18 
输出: 
1:810-18=792 
2:972-279=693 
3:963-369=594 
4:954-459=495

Guess you like

Origin blog.csdn.net/m0_66411584/article/details/127569289