poj 2562 Primary Arithmetic

简介:

这道题太阴险了。。居然还有单复数。。。

1的时候和多个的时候不一样。。。。。。。


题意:给你两个数,问如果有两个数用笔算相加,有多少次进位。

 

思路:简单模拟。但情况要考虑全,而且注意单复数的输出形式不同。可能WA的数据有:

      0 5

      999999 8

      901 99


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

char a[15],b[15];
int aInt[15],bInt[15];

void inverse()
{
	int i;
	//转a
	for(i=0;i<strlen(a);i++)
		aInt[strlen(a)-1-i]=a[i]-48;

	//转b
	for(i=0;i<strlen(b);i++)
		bInt[strlen(b)-1-i]=b[i]-48;

	//test
	/*for(i=0;i<strlen(a);i++)
		printf("%d ",aInt[i]);
	printf("\n");

	for(i=0;i<strlen(b);i++)
		printf("%d ",bInt[i]);
	printf("\n");*/
}

int main()
{
	int i;
	int count;
	while(scanf("%s%s",&a,&b))
	{
		if(strcmp(a,"0")==0 && strcmp(b,"0")==0)
			break;

		//init
		count=0;
		memset(aInt,0,sizeof(aInt));
		memset(bInt,0,sizeof(bInt));

		//将a[],b[]中的数转为int型,且低位在1位
		inverse();

		for(i=0;i<=strlen(a) || i<=strlen(b);i++)
			if(aInt[i]+bInt[i]>=10)
			{
				count++;
				aInt[i+1]++;
			}

		if(count==0)
			printf("No carry operation.\n");
		else if(count==1)
			printf("1 carry operation.\n");
		else
			printf("%d carry operations.\n",count);
	}

	return 0;
}




相关文章
uva10035 Primary Arithmetic
uva10035 Primary Arithmetic
37 0
BNUOJ 1006 Primary Arithmetic
Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你把两个数由右至左按位加起来。
836 0
|
人工智能 移动开发 vr&ar
|
算法
poj 1679 The Unique MST
点击打开链接poj 1679 思路:最小生成树+次小生成树 分析:判断最小生成树是否是唯一的,那就可以去判断次小生成树是否大于最小生成树。这里就涉及到了次小生成树的求法 求解次小生成树: step 1.
859 0
|
机器学习/深度学习 CDN
POJ 2840 Big Clock
POJ 2840 Big Clock
119 0
POJ 2840 Big Clock
Description Our vicar raised money to have the church clock repaired for several weeks. The big clock, which used to strike the hours days...
943 0
|
机器学习/深度学习 存储 人工智能

热门文章

最新文章