PAT甲1023. Have Fun with Numbers (20)

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

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

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

bign divide(bign a,int b,int &r)
{
    bign c;
    c.len=a.len;
    for(int i=a.len-1;i>=0;i--)
    {
        r=r*10+a.d[i];
        if(r<b)c.d[i]=0;
        else
        {
            c.d[i]=r/b;
            r=r%b;
        }
    }
    while(c.len>=2&&c.d[c.len-1]==0)
    {
        c.len--;
    }
    return c;
}

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

int main()  
{       
    char a[22];
    bign N;
    scanf("%s",&a);
    N=changetob(a);
    int harsh[11]={0};
    for(int i=0;i<N.len;i++)
    {
        harsh[N.d[i]]++;
    }
    bign result;
    result=multi(N,2);
    for(int i=0;i<result.len;i++)
    {
        harsh[result.d[i]]--;
    }
    int index;
    for(index=0;index<N.len;index++)
    {
        if(harsh[result.d[index]]>0)
            break;
    }
    if(index>=result.len)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
    for(index=0;index<result.len;index++)
    {
        printf("%d",result.d[result.len-index-1]);
    }
    printf("\n");
    system("pause");
    return 0;
}
阅读更多
文章标签: 1023
个人分类: 算法笔记学习 PAT
上一篇PAT甲1096. Consecutive Factors (20)
下一篇PAT甲1024. Palindromic Number (25)
想对作者说点什么? 我来说一句

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

关闭
关闭