文章

28

粉丝

230

获赞

23

访问

230.3k

头像
利用栈
P1276 上海交通大学机试题
发布于2021年3月26日 21:51
阅读数 7.1k

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
int Reverse(int x);
int main(int argc, char const *argv[])
{
    int a,b;
    while(cin>>a>>b)
    {
        if (Reverse(a+b)==Reverse(a)+Reverse(b))
        {
            printf("%d\n",a+b);
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

int Reverse(int x)
{
    int stack[100];
    int top=-1;
    memset(stack,0,sizeof(stack));
    while(x!=0)
    {
        stack[++top]=x%10;
        x/=10;
    }
    int i=0;
    int Num=0;
    while(top!=-1)
    {
        Num+=stack[top--]*pow(10,i);
        i++;
    }
    return Num;

}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发