Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

shuiyige

Posted by KatrineYang at 2016-11-10 12:06:57 on Problem 1504 and last updated at 2016-11-10 12:07:25
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

void add(char *a, char *b, int la, int lb, char *res){
	int carry = 0;
	int i;
	for(i = 0; i < la || i < lb; i++){
		if(i<la) carry += (a[i]-'0');
		if(i<lb) carry += (b[i]-'0');
		res[i] = (carry%10) + '0';
		carry /= 10;
	}
	while(carry > 0){
		res[i] = (carry%10)+'0';
		carry /= 10;
		i++;
	}
}

int main() {
	int n;
	scanf("%d", &n);
	while(n--){
		char a[1000], b[1000];
		scanf("%s%s",a,b);
		int la = strlen(a), lb = strlen(b);
		char res[2000] = {'\0'};
		add(a,b,la,lb,res);
		char *st = res;
		while(st[0] == '0') st++;
		int lr = strlen(res);
		int pos = lr-1;
		while(res[pos] == '0'){
			res[pos] = '\0';
			pos--;
		}
		printf("%s\n", st);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator