1023 Have Fun with Numbers (20)(20 point(s))

Posted yoyo-sincerely

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1023 Have Fun with Numbers (20)(20 point(s))相关的知识,希望对你有一定的参考价值。

problem

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 file 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

tip

answer


#include<iostream>
#include<set>
#include<cstring>
#include<algorithm>

#define LL long long
using namespace std;

string a, da;
int na[22], nda[22];

void GetNum(string t, int *n){
    if(t == "0") {
        n[0]++;
        return ;
    }
    for(int i = 0; i < t.size(); i++){
//      s.insert(t[i]-‘0‘);
        n[t[i]-‘0‘]++;
    }
    return ;
}

string Double(string t){
    string tt = "";
    reverse(t.begin(), t.end());
    int last = 0, th = 0;
    for(int i = 0; i < t.size(); i++){
        th = 2*(t[i]-‘0‘) + last;
        tt.push_back(th%10 + ‘0‘);
        last = th/10;
    }
    if(last != 0) tt.push_back(last+‘0‘);
//  cout<<tt<<endl;
    return tt;
}

void PrintStatus(int *a){
    for(int i = 0; i < 10; i++){
        printf("%d ", a[i]);
    }
    printf("
");
}

int main(){
//  freopen("test.txt", "r", stdin);
    cin>>a;
    da = Double(a);
    memset(na, 0, sizeof(na));
    memset(nda, 0, sizeof(nda));
    
    GetNum(a, na);
    GetNum(da, nda);
    
//  PrintStatus(na);
//  PrintStatus(nda);
    
    bool flag = true;
    for(int i = 0; i < 10; i++){
        if(na[i] != nda[i]) flag = false;
    }
    
    if(flag) puts("Yes");
    else puts("No");
    reverse(da.begin(), da.end());
    cout<<da;
    return 0;
}

exprience

  • 英语单词
    • permutation 排列
  • puts 与cout<<endl 差别
    • Cout是istream类的预定义对象,puts是预定义函数(库函数)。
    • cout是一个对象,它使用重载插入(<<)运算符函数来打印数据。 但是put是完整的函数,它不使用重载的概念。
    • cout可以打印数字和字符串。 而puts只能打印字符串。
    • cout在内部使用flush而puts并没有,为了刷新stdout,我们必须明确地使用fflush函数。
    • 要使用puts,我们需要包含stdio.h头文件。 在使用cout时我们需要包含iostream.h头文件。
    • puts函数会在结尾增加‘ ‘。

以上是关于1023 Have Fun with Numbers (20)(20 point(s))的主要内容,如果未能解决你的问题,请参考以下文章

1023 Have Fun with Numbers

1023 Have Fun with Numbers

PAT 1023 Have Fun with Numbers

PAT 甲级 1023 Have Fun with Numbers

1023 Have Fun with Numbers (20)

1023 Have Fun with Numbers (20)(20 point(s))