PAT (Advanced Level) 1023. Have Fun with Numbers (20)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1023. Have Fun with Numbers (20)相关的知识,希望对你有一定的参考价值。

手动模拟一下高精度加法。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
using namespace std;

char s[30],t[30],c[30];
int lens,lenc;

void f(char *x)
{
    int len=strlen(x);
    for(int i=0;i<len/2;i++) swap(x[i],x[len-i-1]);
}

void add()
{
    int k=0;
    int len=strlen(s);
    int num1,num2;

    for(int i=0;i<=len;i++)
    {
        if(s[i]==0) num1=num2=0;
        else num1=num2=s[i]-0;
        c[i]=(num1+num2+k)%10+0;
        k=(num1+num2+k)/10;
    }
    if(c[len]==0) c[len]=0;
    f(c);
  //  printf("%s\n",c);
}

bool check()
{
    char tmps[30],tmpc[30];
    if(lens!=lenc) return 0;
    strcpy(tmps,s); strcpy(tmpc,c);
    sort(tmps,tmps+lens); sort(tmpc,tmpc+lenc);
    for(int i=0;s[i];i++) if(tmps[i]!=tmpc[i]) return 0;
    return 1;
}

int main()
{
    memset(s,0,sizeof s);memset(t,0,sizeof t);memset(c,0,sizeof c);
    scanf("%s",s); strcpy(t,s);
    f(s); f(t); add();  f(s);
    lens=strlen(s);lenc=strlen(c);
    if(check()) printf("Yes\n");
    else printf("No\n"); 
    printf("%s\n",c);
    return 0;
}

 

以上是关于PAT (Advanced Level) 1023. Have Fun with Numbers (20)的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced 1023 Have Fun with Numbers (20) [?整数运算]

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079

PAT Advanced Level 1095