PAT甲1023 Have Fun with Numbers (20)(20 分)

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

void doubleup(char a[],char b[])
{
    int c=0;
    int len=0;
    for(int i=strlen(a)-1;i>=0;i--)
    {
        b[len++]=((a[i]-'0')*2+c)%10+'0';
        c=(a[i]-'0')*2/10;
    }
    if(c!=0)b[len++]=c+'0';
    b[len++]='\0';
}

int harsh[10];

int main()
{
    char A[110],B[110];
    scanf("%s",&A);
    doubleup(A,B);
    for(int i=0;i<strlen(A);i++)
    {
        harsh[A[i]-'0']++;
    }
    for(int i=0;i<strlen(B);i++)
    {
        harsh[B[i]-'0']--;
    }
    bool flag=true;
    for(int i=0;i<=9;i++)
    {
        if(harsh[i]!=0)
        {
            printf("No\n");
            flag=false;
            break;
        }
    }
    if(flag)
    {
        printf("Yes\n");
    }
    for(int i=strlen(B)-1;i>=0;i--)
    {
        printf("%c",B[i]);
    }
    return 0;
}

(附翻译)Have Fun with Numbers (20)的问题

07-22

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!rnrnNow 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.rnrnInput Specification:rnrnEach input file contains one test case. Each case contains one positive integer with no more than 20 digits.rnrnOutput Specification:rnrnFor 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.rnrnSample Input:rn1234567899rnSample Output:rnYesrn2469135798rnrn【题目大意】:有一类像123456789的数,将其乘2后的数位组成是一样的(246913578)。同样的数还有1234567899等等rn【疑问】这里我想问的是,先不考虑任何数据结构的应用,只是存粹的C程序设计的话,下面的代码为何不能通过题目的检测点rn[code=c]rn#includern#includernint *devideNumber(long long int );//这个函数负责将数各位分解,无特别 rnrnstatic int cnt=0;rnrnint main()rn long long int x,b[20];//将所给数作为long long int型存储 rn int i,NotMatch=0,mark;rn scanf("%d",&x);rn int *a=devideNumber(x);//将分解后的数字放进malloc的空间内,无特别 rnrn while(cnt)rn b[cnt-1]=*a++;//将分解后的数字倒着装入数组b中(因为分解是倒着分的) rn cnt--;rn rn x=2*x;rn a=devideNumber(x);//将翻倍后的x再次分解,无特别 rn while(1)rn for(i=0,mark=0;i0;i++)rn a[i]=x%10;rn x=x/10;rn cnt++;rn rn return a;rnrn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭