1669 - A+B without carry

Xiao Ming always tends to ignore the carry when he does decimal addition with paper and pencil.For example,15+17,Xiao Ming will answer 22,because he ignores the carry from the single digits.5+7=12,and the digit 1 is the carry. 

题目输入

The input will consist of a series of pairs of integers a and b(both less than 1000000000),separated by a space, one pair of integers per line.

题目输出

For each pair of input integers a and b you should output the correct answer of the sum of a and b,a space character and Xiao Ming's answer of the sum of a and b in one line,and with one line of output for each line in input.If Xiao Ming's answer begins with zero,don't output unnecessary zero.

输入/输出样例

题目输入

15 16
1 999
31 71

题目输出

31 21
1000 990
102 2

C语言解答

#include <stdio.h>
#include <math.h>

void compute(int a,int b)
{
  int m,n,i,j,p=1;
  i=a+b;
  j=i;
  while((a/10!=0)||(b/10!=0))
  {
    m=a%10;n=b%10;
    if(m+n>=10)
      j=j-(int)pow(10,p);
    a=a/10;b=b/10;
    p++;
  }
  if((a%10)+(b%10)>=10)
    j=j-(int)pow(10,p);
  printf("%d ",i);
  printf("%d\n",j);
}
int main()
{
  int a,b;
  while(scanf("%d %d",&a,&b)!=EOF)
      compute(a,b);
  return 0;
}

C++解答

#include <stdio.h>
int a,b;
void run()
{
	int c,k;
	c=a+b;
	printf("%d ",c);//这个程序是由正常的和来反求不进位的和(或者直接按位相加也是可以的 )。 
	k=1;
	while(k<=a||k<=b)
	{
		if(a/k%10+b/k%10>9)
			c-=k*10;//在某一位上的进位被忽略,相当于总和减小了这一位权值的十倍,个位进位被忽略,总和就少了10。 
		k*=10;
	}
	printf("%d\n",c);
}
int main()
{
	while(scanf("%d%d",&a,&b)!=EOF)
		run();
	return 0;
} 
时间限制 1 秒
内存限制 32 MB
讨论 统计
上一题 下一题