首页
学习
活动
专区
圈层
工具
发布
29 篇文章
1
PAT (Basic Level) Practice (中文)1047 编程团体赛
2
PAT (Basic Level) Practice (中文)1083 是否存在相等的差
3
PAT (Basic Level) Practice (中文)1082 射击比赛
4
PAT (Basic Level) Practice (中文)1081 检查密码
5
PAT (Basic Level) Practice (中文)1077 互评成绩计算
6
PAT (Basic Level) Practice (中文)1076 Wifi密码
7
PAT (Basic Level) Practice (中文)1064 朋友数
8
PAT (Basic Level) Practice (中文)1063 计算谱半径
9
PAT (Basic Level) Practice (中文)1057 数零壹
10
PAT (Basic Level) Practice (中文)1056 组合数的和
11
PAT (Basic Level) Practice (中文)1042 字符统计
12
PAT (Basic Level) Practice (中文)1041 考试座位号
13
PAT (Basic Level) Practice (中文)1023 组个最小数
14
PAT (Basic Level) Practice (中文)1022 D进制的A+B
15
PAT (Basic Level) Practice (中文)1019 数字黑洞
16
PAT (Basic Level) Practice (中文)1007 素数对猜想
17
PAT (Basic Level) Practice (中文)1091 N-自守数
18
PAT (Basic Level) Practice (中文)1026 程序运行时间
19
PAT (Basic Level) Practice (中文)1061 判断题
20
PAT (Basic Level) Practice (中文)1086 就不告诉你
21
PAT (Basic Level) Practice (中文)1016 部分A+B
22
PAT (Basic Level) Practice (中文)1012 数字分类
23
PAT (Basic Level) Practice (中文)1013 数素数
24
PAT (Basic Level) Practice (中文)1011 A+B 和 C
25
PAT (Basic Level) Practice (中文)1009 说反话
26
PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题
27
PAT (Basic Level) Practice (中文)1006 换个格式输出整数
28
PAT (Basic Level) Practice (中文)1004 成绩排名
29
PAT (Basic Level) Practice (中文)1002 写出这个数

PAT (Basic Level) Practice (中文)1019 数字黑洞

1019 数字黑洞

给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。

例如,我们从6767开始,将得到

7766 - 6677 = 1089 9810 - 0189 = 9621 9621 - 1269 = 8352 8532 - 2358 =6174 7641 - 1467 = 6174 … …

现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。

输入格式:

输入给出一个 (0,10​4) 区间内的正整数 N。

输出格式:

如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。

输入样例 1:

6767

输出样例 1:

7766 - 6677 = 1089 9810 - 0189 = 9621 9621 - 1269 = 8352 8532 - 2358 = 6174

输入样例 2:

2222

输出样例 2:

2222 - 2222 = 0000

代码:

代码语言:javascript
复制
#include<stdio.h>

int fun1(char *str)
{
    int i,j;
    int index;
    char temp;
    int sum=0;
    for(i=0;i<4-1;i++)
    {
        index=i;
        temp=str[i];
        for(j=i+1;j<4;j++)
        {
            if(str[j]-'0'>=str[index]-'0') index=j;
        }
        str[i]=str[index];
        str[index]=temp;
    }
    for(i=0;i<4;i++)
    {
        printf("%c",str[i]);
        sum=sum*10+(str[i]-'0');
    }
    printf(" - ");
    return sum;
}

int fun2(char *str)
{
    int i,j;
    int index;
    char temp;
    int sum=0;
    for(i=0;i<4-1;i++)
    {
        index=i;
        temp=str[i];
        for(j=i+1;j<4;j++)
        {
            if(str[j]-'0'<=str[index]-'0') index=j;
        }
        str[i]=str[index];
        str[index]=temp;
    }
    for(i=0;i<4;i++)
    {
        printf("%c",str[i]);
        sum=sum*10+(str[i]-'0');
    }
    printf(" = ");
    return sum;
}

int fun3(int sum1,int sum2)
{
    int temp=sum1-sum2;
    if(temp>999) printf("%d\n",temp);
    if(temp>99&&temp<=999) printf("0%d\n",temp);
    if(temp>9&&temp<=99) printf("00%d\n",temp);
    if(temp>0&&temp<=9) printf("000%d\n",temp);
    if(temp==0) printf("0000\n");
    return temp;
}

void fun4(char *str,int n)
{
    int j=3;
    str[0]='0';
    str[1]='0';
    str[2]='0';
    str[3]='0';
    while(n)
    {
        str[j--]=(n%10)+'0';
        n/=10;
    }
}
int main()
{
    char str[50];
    str[0]='0';str[1]='0';str[2]='0';str[3]='0';
    char str1[50];
    gets(str1);
    int i;
    int len=strlen(str1);
    for(i=len-1;i>=0;i--)
    {
        str[3-i]=str1[i];
    }

    if(str[0]==str[1]&&str[1]==str[2]&&str[2]==str[3]) printf("%s - %s = 0000\n",str,str);
    else
    {
        while(1)
        {
            int sum1=fun1(str);
            int sum2=fun2(str);
            int temp=fun3(sum1,sum2);
            if(temp==6174) break;
            else
            {
               fun4(&str,temp);
            }
        }
    }
    return 0;
}
下一篇
举报
领券