【整理】ACM代码整理 BUN 1006

http://www.bnuoj.com/bnuoj/problem_show.php?pid=1006

首先,把输入的数据保存在数组里。接着,按照位置进行加法计算同时判断是否进位(在这里使用计数器:如果有进位就加1)。最后,按照要求的格式输出。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

char a[11],b[11],stack[11];


void opration(char *a,char *b){
	int topa = strlen(a)-1;
	int topb = strlen(b)-1;
//	int len = lena<lenb?lenb:lena;
//	int jinwei = 0;
//	int temp;
//	int op=0;
//	for(int i =len-1;i>=0;i--){
//		temp = (i>lena?0:a[i]-'0') + (i>lenb?0:b[i]-'0') + jinwei;
//		jinwei = temp/10;
//		if(jinwei == 1)op++;
//	}
	int temp = 0;
	int jinwei =0;
	int op = 0;
	while(1){
		if(topa<0 && topb<0)break;
		temp = (topa<0?0:a[topa]-'0') + (topb<0?0:b[topb]-'0') + jinwei;
		jinwei = temp/10;
		if(jinwei == 1)op++;
		topa--;
		topb--; 
	}
	
	
	if(op == 0)printf("No carry operation.\n");
	if(op==1)printf("%d carry operation.\n",op);
	if(op!=0&&op!=1) printf("%d carry operations.\n",op);
}

int main(int argc, char** argv) {
	while(scanf("%s%s",a,b)){
		if(!strcmp(a,"0") && !strcmp(b,"0"))break;
		opration(a,b);
	}
	return 0;
}


你可能感兴趣的:(【整理】ACM代码整理 BUN 1006)