zoukankan      html  css  js  c++  java
  • 自测-4 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 kk 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.

    Sample Input:

    1234567899
    

    Sample Output:

    Yes
    2469135798

    说明:最开始使用的思路是大数计算的方式,存入字符串数组,然后运算后与原数组进行对比,但存在wa点,现在没找到。先放置于此:
    #include <stdio.h>
    #include<string.h>
    int main()
    {
        // N为长度,M为进位
        int N = 0,M = 0, i = 0, num = 0,loop = 0, j = 0;
        char str[100];
        char tmp[100];
        scanf("%s",str);
        N = strlen(str);
    
        for(i = N - 1; i >= 0; i--){
            num = (str[i] - '0')*2 + M;
            M = 0;
            if(num > 9){
                num = num % 10;
                M++;
            }
            tmp[i] = num + '0';
        }
        for(i = 0; i < N; i++){
            for(j = 0; j < N; j++){
                if(tmp[i] == str[j]){
                    str[j] = 'a';
                    break;
                }
            }
        }
        for(i = 0; i < N; i++){
            if(str[i] != 'a'){
                loop = 1;
                break;
            }
        }
        if(M == 1){
            printf("No
    1");
            for(i = 0; i < N; i++){
                printf("%d",tmp[i] - '0');
            }
            return 0;
        }
        if(loop == 1){
            for(i = 0; i < N; i++){
                printf("%d",tmp[i] - '0');
            }
        }else{
            printf("Yes
    ");
            for(i = 0; i < N; i++){
                printf("%d",tmp[i] - '0');
            }
        }
        return 0;
    }

    后改变思路,只记录0~9十个数组出现的次数,仅需要两个长度为10的一维数组即可。使用别人写的C++代码,引用链接在最后

     1 #include <cstdio>  
     2 #include <string>  
     3 #include <iostream>  
     4 using namespace std;  
     5 int cnt1[10] = { 0 }, cnt2[10] = {0};  
     6   
     7 int main(){  
     8     string s;  
     9     cin >> s;  
    10     string s2 = s;  
    11     for (int i = 0; i < s.size(); i++){  
    12         cnt1[s[i] - '0']++;  
    13     }  
    14     int carry = 0;  
    15     for (int i = s.size() - 1; i >= 0; i--){  
    16         s2[i] = ((s[i] - '0') * 2 + carry )% 10 + '0';  
    17         carry = ((s[i] - '0') * 2 + carry) / 10;  
    18     }  
    19     if (carry){  
    20         char c = carry + '0';  
    21         s2 = c + s2;  
    22         cout << "No
    " << s2 << endl;  
    23         return 0;  
    24     }  
    25     for (int i = 0; i < s2.size(); i++){  
    26         cnt2[s2[i] - '0']++;  
    27     }  
    28     bool flag = true;  
    29     for (int i = 0; i < 10; i++){  
    30         if (cnt1[i] != cnt2[i]){  
    31             flag = false;  
    32             break;  
    33         }  
    34     }  
    35     if (flag) cout << "Yes" << "
    " << s2 << endl;  
    36     else cout << "No
    " << s2;  
    37     return 0;  
    38 }  

    http://blog.csdn.net/xtzmm1215/article/details/38837203

  • 相关阅读:
    ffmpeg视频操作记录
    frida定义线程写图片文件
    frida创建静态域
    frida创建字符串
    pyppeteer_stealth
    python ast
    最小的js编译器
    excel加双引号和逗号
    JUnit 单元测试方法中启用子线程的问题
    ctrip 开源 DAL 框架相关问题总结
  • 原文地址:https://www.cnblogs.com/baichangfu/p/7147352.html
Copyright © 2011-2022 走看看