加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数加法模板

发布时间:2020-12-14 02:12:45 所属栏目:大数据 来源:网络整理
导读:/*大数加法模板 */#includeiostream#includecstdio#includecstring#includecstdlib#includecmath#includealgorithm#includestackusing namespace std;int T;char s1[1001],s2[1001];int ca,up;stackint sta;int main(){while(cinT){ca=1;while(T--){scanf("%
/*
大数加法模板 
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std;

int T;
char s1[1001],s2[1001];
int ca,up;
stack<int> sta;

int main()
{
	while(cin>>T)
	{
		ca=1;
		while(T--)
		{
			scanf("%s%s",s1,s2);
			
			int len1=strlen(s1)-1,len2=strlen(s2)-1;
			
			
			up=0;
			int temp;
			for(;len1>=0 && len2>=0;len1--,len2--)
			{
				temp = (s1[len1] + s2[len2] - '0' - '0' + up);
				if(temp >= 10)
				{
					up=1;
				}
				
				else up=0;
				
				if(len1 || len2)
				sta.push(temp%10);
				
				else 
				sta.push(temp);
			}
			
			for(int i=len1;i>=0;i--)
			{
				temp = (s1[i] - '0' + up);
				
				if(temp >= 10)
				{
					up=1;
				}
				
				else up=0;
				
				if(i)
				sta.push(temp%10);
				else
				sta.push(temp);
			}
			
			for(int i=len2;i>=0;i--)
			{
				temp = (s2[i] - '0' + up);
				
				if(temp >= 10)
				{
					up=1;
				}
				
				else up=0;
				
				if(i)
				sta.push(temp%10);
				else
				sta.push(temp);
			}
			
			
			cout<<"Case "<<ca++<<":"<<endl;
			cout<<s1<<" + "<<s2<<" = ";
			while(!sta.empty())
			{
				printf("%d",sta.top());
				sta.pop();
			}
			
			printf("n");
			if(T) printf("n");
			 
		}
	}
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读