Gravatar
李星昊
积分:139
提交:68 / 148

Pro37  增强的加法问题

#include<bits/stdc++.h>
using namespace std;
 
string s1,s2;
int a[250],b[250],c[500];
int len; 
int main(){
	freopen("add.in","r",stdin);
	freopen("add.out","w",stdout);
	cin >> s1 >> s2;
 
	
	for (int i = 0; i < s1.size(); i++) {
		a[s1.size() - i - 1] = s1[i] - '0';
	}
	for (int i = 0; i < s2.size(); i++) {
		b[s2.size() - i - 1] = s2[i] - '0';
	}
	
	len = s1.size();
	
	if (s1.size() < s2.size()) {
		len = s2.size();
	}
	
	for (int i = 0; i < len; i++) {
		c[i] = a[i] + b[i];
	}
	
	for (int i = 0; i < len; i++) {
		if (c[i] >= 10) {
			c[i + 1] += c[i] / 10;
			c[i] %= 10;
		}
	}
	
	if (c[len] != 0) {
		len++;
	}
	for (int i = len - 1; i >= 0; i--) {
		cout << c[i];
	}
}
 
//第一步:定义
//第二步:输入
//第三步:将字符串转换成数组
//第四步:相加(中间有进位,详见代码)
//第四步:输出
 

2022-11-07 10:01:57    
我有话要说
暂无人分享评论!