问题 Z: 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.

样例输入 Copy

15 16
1 999
31 71

样例输出 Copy

31 21
1000 990
102 2

代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	int a,b,len1,len2,index,i,j;
	char str1[11],str2[11],c[11];
	
	while(scanf("%s %s",str1,str2)!=EOF){
		len1 = strlen(str1);
		len2 = strlen(str2);
		index = 0;
		//计算过程
		for(i = len1-1,j = len2-1;i >= 0 && j >= 0;i--,j--){
			int sum = str1[i] - '0' + str2[j] - '0';
			//忽略进位
			if(sum > 9){
				sum -= 10;
			}
			c[index++] = sum + '0';
		}
		while(i >= 0){
			c[index++] = str1[i];
			i--;
		}
		while(j >= 0){
			c[index++] = str2[j];
			j--;
		}
		//去掉前导0
		index = index -1;
		while(c[index] == '0' && index > 0){
			index--;
		}
		//正确答案
		printf("%d ",atoi(str1) + atoi(str2));
		//答案
		for(i = index;i >= 0;i--){
			printf("%c",c[i]);
		}
		printf("\n");
 
 
	}
	return 0;
}
#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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值