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

不用反转,用字符串直接相加就行了

Posted by low3182063 at 2010-06-27 16:35:53 on Problem 1504
char* a[M],b[M],sum[M];存储输入的两个正整数,然后去除a,b中的前导0和后缀0,下标由0开始由低到高带进位相加存放在sum中,再去除sum中的前导0和后缀0,然后输出就行了。因为  反转(反转(a))=a,所以这里就是改变了一下加法的规则。
贴个代码验证了一下,很繁锁:

#include <stdio.h>
#include <string.h>
#define M 100
char a[M],b[M];

void del(char* a){
	int len, i, j;
	len = strlen(a);
	i=j=0;
	while(a[i]=='0')i++;
	while(a[len-1]=='0')len--;
	while(i<len){
		a[j]=a[i];
		j++; i++;
	}
	a[j]='\0';
}

void add(char* a, char* b){
	int t=0,c=0,i=0,as,bs,max;
	as = strlen(a);
	bs = strlen(b);
	max = (as>bs)?as:bs;

	if(as!=bs){
		if(max == as)
			while(bs<max)b[bs++]='0';
		else
			while(as<max)a[as++]='0';
	}
	while(i<max){
		t = a[i]+b[i]-'0'-'0'+c;
		if(t>9){
			t -= 10;
			c = 1;
		}else
			c = 0;
		a[i] = t + '0';
		i++;
	}
	if(c) a[max++] = '1';
	a[max] = '\0';
}

int main(int argc, char *argv[])
{
	int n;
	scanf("%d", &n);
	while(n--){
		scanf("%s %s", a, b);
		del(a);
		del(b);
		add(a,b);
		del(a);
		printf("%s\n",a);
	}
	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