甲級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:

1234567899
Sample Output:

Yes
2469135798


思路

本題思路參考了柳婼小姐姐的代碼。
1、將輸入數字按從低位到高位的順序按位讀入,設置flag位記錄每位數乘2所得進位,若相乘結果大於10則flag=1;設置book[]數組記錄,每讀取一位數則對應該數字的book[i]++,每得到一個乘2後的數位則對應book[i]–,如此可知符合條件的數字對應的book[i]=0。
2、設置flag1檢查book數組中是否有非0元素存在,即判定輸入的數字是否符合條件。


代碼

代碼1:

#include <cstdio>
#include <string.h>
using namespace std;

int main()
{
    char s[20];
    scanf("%s",s);
    int len=strlen(s),flag=0,book[10]={0};
    for(int i=len-1;i>=0;i--){
        int t=s[i]-'0';
        book[t]++;
        t=t*2+flag;
        flag=0;
        if(t>=10){
            t=t-10;
            flag=1;
        }
        s[i]=t+'0';
        book[t]--;
    }
    int flag1=0;
    for(int i=0;i<10;i++){
        if(book[i]!=0) flag1=1;
    }
    if(flag==0&&flag1==0) printf("Yes\n");
    else printf("No\n");
    if(flag==1) printf("1");
    printf("%s",s);
}

代碼2:

#include <cstdio>
#include <string.h>
#include <iostream>
using namespace std;

struct bign{
    int d[25];
    int len;
    bign(){
        memset(d,0,sizeof(d));
        len=0;
    }
};

bign change(char str[]){
    bign a;
    a.len=strlen(str);
    for(int i=0;i<a.len;i++){
        a.d[i]=str[a.len-i-1]-'0';
    }
    return a;
}

bign multi(bign a,int b)
{
    bign c;
    int flag=0,temp=0;
    for(int i=0;i<a.len;i++){
        int temp=a.d[i]*b+flag;
        c.d[c.len++]=temp%10;
        flag=temp/10;
        //printf("%d ",c.d[c.len-1]);
    }
    while(flag!=0){
        c.d[c.len++]=flag%10;
        flag/=10;
    }
    return c;
}

bool judge(bign a,bign b){
    if(a.len!=b.len) return false;
    int count[10]={0};
    for(int i=0;i<a.len;i++){
        count[a.d[i]]++;
        count[b.d[i]]--;
    }
    for(int i=0;i<10;i++){
        if(count[i]!=0) return false;
    }
    return true;
}

int main()
{
    char str[25];
    cin.getline(str,25);
    bign a=change(str);
    bign mul=multi(a,2);
    bool res=judge(a,mul);
    if(res==true) printf("Yes\n");
    else printf("No\n");
    for(int i=mul.len-1;i>=0;i--){
        printf("%d",mul.d[i]);
    }
}

備註

1、本題最開始的思路是採用如下的方式進行乘2運算,但是代碼過程中出現問題,有時間再試試。

if(i==len-1) str2[i]=((str1[i]-'0')*2%10)+'0';
else if(i==0) str2[i]=((str1[i]-'0')*2+(str1[i+1]-'0')*2/10)+'0';
else str2[i]=((str1[i+1]-'0')*2/10+(str1[i]-'0')*2%10)+'0';

2、晴神的思路是創建結構體,將字符串轉換爲包含int型數組的結構體,字符串中的數字存放在結構體的int數組中,之後對Int數組進行乘法和比較操作。(代碼2)
3、PAT中的編譯不支持使用gets()函數,輸入字符串時可以使用cin.getlin()函數或scanf進行輸入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章