U2yyy 2022-05-09 15:44 采纳率: 71.4%
浏览 42
已结题

Have Fun with Numbers

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

#include<stdio.h>
int length(long long a);
void bits(int a[],long long b);
void judgement(int a[],int b[],int len);
int main(){
    int j,len;

    long long num;

    int numl[10];

    for(j=0;j<10;j++){
        numl[j] = 0;
    }

    scanf("%lld",&num);

    len = length(num);

    int arr[len];

    bits(arr,num);

    judgement(arr,numl,len);

    num *=2;

    len = length(num);

    int arr_[len];

    bits(arr_,num);

    int rel = 1;

    for(j=0;j<len;j++){
        int n;
        for(n=0;n<10;n++){
            if(numl[n] == 0){
                if(arr_[j] == n){
                    rel = 0;
                }
            }
        }
    }

    if(rel){
        printf("Yes\n");
        printf("%lld",num);
    }else{
        printf("No\n");
        printf("%lld",num);
    }

    return 0;
}
int length(long long a){
    int cnt = 0;
    for( ; ; ){
        a /= 10;
        cnt++;
        if(a == 0)
        break;
    }
    return cnt;
}

void bits(int a[],long long b){
    int i = 0;
    for(i=0; ;i++){
        a[i] = b%10;
        b /= 10;
        if(b == 0)
        break;
    }
}

void judgement(int a[],int b[],int len){
    int i = 0;
    for(i=0;i<len;i++){
        switch(a[i]){
            case 0:
            b[0] = 1;
            break;
            case 1:
            b[1] = 1;
            break;
            case 2:
            b[2] = 1;
            break;
            case 3:
            b[3] = 1;
            break;
            case 4:
            b[4] = 1;
            break;
            case 5:
            b[5] = 1;
            break;
            case 6:
            b[6] = 1;
            break;
            case 7:
            b[7] = 1;
            break;
            case 8:
            b[8] = 1;
            break;
            case 9:
            b[9] = 1;
            break;
        }
    }
}


超过unsigned long,输入中有数字不在结果中,但结果数字都在输入里
最长串,10个数字全出现
最长串全是9
这三种情况显示答案错误,目前自己没有找出来问题

  • 写回答

2条回答 默认 最新

  • 藏猊 2022-05-09 15:54
    关注

    意思是k位数字翻倍后还由1-k组成?
    案例里不超过20位数字,很明显longlong存不下,这里用字符串存会比较好

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 创建了问题 5月9日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大