【CodeForces】[75A]Life Without Zeros

这里写图片描述

题意:
正常运算的话101+102=203
当去除式子中所有的0时变为
11+12=23
发现等式仍然成立
而105+106=211去除所有0变为
15+16=211很显然不成立
所以给出两个数字
判断它们相加的式子去除所有0后是否成立

解题过程:
因为题目表示去除所有的0
那么可以通过

Int tc=0;
    while(c) {
        if(c%10)
            tc*=10;
        tc+=c%10;
        c/=10;
    }
c=0;
    while(tc) {
        c=c*10+tc%10;
        tc/=10;
    }

来去除数字c中的0
最后判断去除0后的两个数相加
是否等于相加后去除0即可

另外需要注意数据范围
int有可能会溢出

#include<stdio.h>
int main() {
    __int64 a,b;
    while(scanf("%I64d",&a)!=EOF) {
        scanf("%I64d",&b);
        __int64 c=a+b;
        __int64 tc=0;
        while(c) {
            if(c%10)
                tc*=10;
            tc+=c%10;
            c/=10;
        }
        c=0;
        while(tc) {
            c=c*10+tc%10;
            tc/=10;
        }
        __int64 ta=0;
        while(a) {
            if(a%10)
                ta*=10;
            ta+=a%10;
            a/=10;
        }
        a=0;
        while(ta) {
            a=a*10+ta%10;
            ta/=10;
        }
        __int64 tb=0;
        while(b) {
            if(b%10)
                tb*=10;
            tb+=b%10;
            b/=10;
        }
        b=0;
        while(tb) {
            b=b*10+tb%10;
            tb/=10;
        }
        printf("%s\n",c==a+b?"YES":"NO");
    }

    return 0;
}

题目地址:【CodeForces】[75A]Life Without Zeros

posted @ 2016-05-18 17:13  BoilTask  阅读(34)  评论(0)    收藏  举报