码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-18 23:14:49      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:clu   ios   define   ice   out   需要   specific   last   sts   

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("\n");
}

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函数会在结尾增加‘\n‘。

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

标签:clu   ios   define   ice   out   需要   specific   last   sts   

原文地址:https://www.cnblogs.com/yoyo-sincerely/p/9326425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!