当前位置:编程学习 > C/C++ >>

题目1471: 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 
 
提示 [+]
*** 提示已隐藏,点击上方 [+] 可显示 ***
 
 
 
来源
2013年浙江大学复试机试模拟题
 
 
 
 
 
 
 
 
[cpp] 
/********************************* 
*   日期:2013-3-25 
*   作者:SJF0115 
*   题号: 题目1471: A+B without carry 
*   来源:http://acmclub.com/problem.php?id=1471 
*   结果:AC 
*   来源:2013年浙江大学复试机试模拟题 
*   总结: 
**********************************/  
#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];  
    //freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin);   
    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;  
}  
 
/*********************************
*   日期:2013-3-25
*   作者:SJF0115
*   题号: 题目1471: A+B without carry
*   来源:http://acmclub.com/problem.php?id=1471
*   结果:AC
*   来源:2013年浙江大学复试机试模拟题
*   总结:
**********************************/
#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];
//freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin);
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;
}
 
 
 
 
 
 
 
 
 
[cpp] 
/********************************* 
*   日期:2013-3-25 
*   作者:SJF0115 
*   题号: 题目1471: A+B without carry 
*   来源:http://acmclub.com/problem.php?id=1471 
*   结果:AC 
*   来源:2013年浙江大学复试机试模拟题 
*   总结: 
**********************************/  
#include<stdio.h>   
#include<string.h>   
#include<stdlib.h>   
  
int main()  
{  
    int a,b,index;  
    int c[12];  
    //freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin);   
    while(scanf("%d %d",&a,&b)!=EOF){  
        //正确答案   
        printf("%d ",a + b);  
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,