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

2020-09-28 10:52:49 浏览数 (2)

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

0 人点赞