关闭

PAT 甲级 1023 Have Fun with Numbers

标签: PAT 1023Have Fun with Number
361人阅读 评论(0) 收藏 举报
分类:

1023. Have Fun with Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
用数组表示,进行加法
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
int a[25];
int c[25];
char b[25];
int tag[10];
int len;
int add()
{
    int i=0;
    int num=0;
    while(i<len||num!=0)
    {
        a[i]+=(a[i]+num);
        num=a[i]/10;
        a[i]%=10;
        i++;
    }
    return i;
}
int main()
{
    scanf("%s",b);
    len=strlen(b);
    memset(tag,0,sizeof(tag));
    memset(a,0,sizeof(a));
    for(int i=0;i<len;i++)
    {
        a[len-1-i]=b[i]-'0';
        tag[b[i]-'0']++;
    }
    int cnt=add();
   
    for(int i=0;i<cnt;i++)
    {
        tag[a[i]]--;
    }
    bool ans=true;
    for(int i=0;i<=9;i++)
    {
        if(tag[i]!=0)
            ans=false;
    }
    if(!ans)
        printf("No\n");
    else
        printf("Yes\n");
    for(int i=cnt-1;i>=0;i--)
            printf("%d",a[i]);
        printf("\n");

    return 0;
}



提交代码
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103080次
    • 积分:4360
    • 等级:
    • 排名:第7628名
    • 原创:344篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    最新评论