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

  • 相关阅读:
    leetcode Convert Sorted List to Binary Search Tree
    leetcode Convert Sorted Array to Binary Search Tree
    leetcode Binary Tree Level Order Traversal II
    leetcode Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
    证明中序遍历O(n)
    leetcode Maximum Depth of Binary Tree
    限制 button 在 3 秒内不可重复点击
    HTML 和 CSS 画三角形和画多边行基本原理及实践
    在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较
  • 原文地址:https://www.cnblogs.com/baichangfu/p/7147352.html
Copyright © 2011-2022 走看看