前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 甲级 1023 Have Fun with Numbers

PAT 甲级 1023 Have Fun with Numbers

作者头像
ShenduCC
发布2018-04-27 10:35:42
6430
发布2018-04-27 10:35:42
举报
文章被收录于专栏:算法修养算法修养

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:

代码语言:javascript
复制
1234567899

Sample Output:

代码语言:javascript
复制
Yes
2469135798
代码语言:javascript
复制
用数组表示,进行加法
代码语言:javascript
复制
#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;
}

提交代码

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1023. Have Fun with Numbers (20)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档