文章

9

粉丝

0

获赞

40

访问

2.1k

头像
a+b - 华科 题解:C
P1398 华中科技大学
发布于2026年3月20日 10:58
阅读数 234

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
	char a[1005], b[1005];
	while (scanf("%s %s", a, b) != EOF) {
		int len_a = strlen(a), len_b = strlen(b);
		char res[1005];
		
		int idx = 0, idx_a = len_a - 1, idx_b = len_b -1;
		int carry = 0;
		while (idx_a >= 0 || idx_b >= 0 || carry > 0) {
			int val_a = (idx_a >= 0) ? (a[idx_a--] - '0') : 0;
			int val_b = (idx_b >= 0) ? (b[idx_b--] - '0') : 0;
			int sum = val_a + val_b + carry;
			res[idx++] = sum % 10 + '0';
			carry = sum / 10;
		}
		
		for (int k = idx - 1; k >= 0; k--) {
			printf("%c", res[k]);
		}
		printf("\n");
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发