1113: 高精度加法之一
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 499 Solved: 168
[Submit][Status][Web Board]
Description
计算a+b的值,a,b 皆不超过300位。
Input
两个数 每行一个
Output
一个数
Sample Input
12
23
Sample Output
35
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 1000 + 10;
char s[N], s1[N], e[N], e1[N];
int main(){int len, len1, maxL, k;scanf("%s%s", s, s1);len = strlen(s);len1 = strlen(s1);memset(e, 0, sizeof(e));memset(e1, 0, sizeof(e1));k = 1;//把字符串反转,再变为数字 for(int i = len - 1; i >= 0; i--){e[k] = s[i] - '0';k++;}k = 1;for(int i = len1 - 1; i >= 0; i--){e1[k] = s1[i] - '0';k++;} maxL = max(len, len1);for(int i = 1; i <= maxL; i++){e[i] += e1[i];if(e[i] >= 10){e[i + 1] += e[i] / 10;e[i] %= 10;} }bool flag = true;for(int i = N; i >= 1; i--){if(e[i] != 0){flag = false;}if(!flag){printf("%d", e[i]);}}return 0;
}