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

PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分)

作者头像
glm233
发布2020-09-28 10:52:49
3320
发布2020-09-28 10:52:49
举报

1023 Have Fun with Numbers (20 分)

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

思路:本是一道水的不行的20分题 ,就随便写了一个交了发现一半的测试点错了,后来看数据范围才知道,最多不超过20位,

emmm,longlong可能有些数据不行,

首先我们复习一下整型、长整型的数据表示范围

unsigned int 0~4294967295 int -2147483648~2147483647 unsigned long 0~4294967295 long -2147483648~2147483647 long long的最大值:9223372036854775807 long long的最小值:-9223372036854775808 unsigned long long的最大值:1844674407370955161

__int64的最大值:9223372036854775807 __int64的最小值:-9223372036854775808 unsigned __int64的最大值:18446744073709551615

很明显我们可以看出哪怕是unsigned long long也只有20位,首位还是1,如果极端数据是99999999999999999999呢,那这个时候我们就要用字符串了

这道题目其实是一个很弱的大数相乘,只乘2,如果得数位数和原数不一样的一律输出No,道理很简单,位数都不相同,那组成肯定也不一样~,大体就是写了个乘法函数,然后去求两个数的组成,一样就Yes,否则No

注意输出乘2的得数有可能有进位!

希望对您有所帮助~

代码语言:javascript
复制
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
 char ch = getchar(); ll s = 0, w = 1;
 while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
 while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
 return s * w;
}
inline void write(ll x)
{
 if (x < 0)putchar('-'), x = -x;
 if (x > 9)write(x / 10);
 putchar(x % 10 + 48);
}
char a[25],b[25];
ll c[10],d[10];
int main()
{
    cin>>a;
     for(rg i=0;a[i];i++)
    {
           b[strlen(a)-1-i]=a[i];
    }
    ll temp=0,tot=strlen(b);
    for(rg i=0;b[i];i++)
    {
            ll tep=(b[i]-48)*2+temp;
            temp=tep/10;
            tep%=10;
            b[i]=tep+48;
    }
    while(temp)
    {
        b[tot++]=temp%10+48;
        temp/=10;
    }
    ll sum=0;
    for(rg i=0;i<strlen(a);i++)
    {
        c[a[i]-48]++,d[b[i]-48]++;
    }
     for(rg i=0;i<10;i++)
    {
       sum+=(c[i]==d[i]);
    }
    if(strlen(b)!=strlen(a))
    {
        cout<<"No"<<endl;
        for(rg i=strlen(b)-1;b[i];i--)cout<<b[i];
        return 0;
    }
    sum==10?cout<<"Yes"<<endl:cout<<"No"<<endl;
    for(rg i=strlen(b)-1;b[i];i--)cout<<b[i];
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-11-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档