zoukankan      html  css  js  c++  java
  • hdu1002 A + B Problem II

    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. 

    InputThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000. 
    OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases. 
    Sample Input

    2
    1 2
    112233445566778899 998877665544332211

    Sample Output

    Case 1:
    1 + 2 = 3
    
    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110




    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char a[1001],b[1001];
    	int c[1001],d[1001],e[1001],n,i;
    	scanf("%d",&n);
    	int x = 0;
    	while(n--){
    		x++;
    		scanf("%s%s",a,b);
    		memset(c,0,sizeof(c));
    		memset(d,0,sizeof(d));
    		memset(e,0,sizeof(e));
    		int a1 = strlen(a);
    		int a2 = strlen(b);
                   //将字符串倒序,将字符转化为数字才能进行加法
    		int a3 = a1-1,a4 = a2-1;
    		for(i = 0; i<a1; i++){
    			c[a3] = a[i]-48;//减个'0'就可以转化了
    			a3--;
    		}
    		for(i = 0; i<a2; i++){
    			d[a4] = b[i]-48;
    			a4--; 
    		}
    		int l;
    		if(a1>a2){
    			l = a1;
    		}
    		else{
    			l = a2;
    		}
    		for(i = 0; i<l; i++){
    			e[i] += c[i]+d[i];//对两组字符串中的数逐位相加,存于e[i]
    			if(e[i]>=10){// 得到的结果进行进制位处理
    				e[i+1]++;
    				e[i] = e[i]%10;
    			}
    		}
    		printf("Case %d:
    ",x);
            printf("%s + %s = ",a,b);
    		if(e[l] != 0){
    			printf("%d",e[l]);
    		}
    		for(i = l-1; i>=0; i--){
    			printf("%d",e[i]);
    		}
    		printf("
    ");
    		if(n){
    			printf("
    ");
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    java Servlet小结
    Java 自定义客户端与服务器
    JAVA IO流总结
    java udp与tcp
    tomcat作为服务器的配置
    Linux
    Git -- 如何删除本地仓库
    ASP.NET Core 基础 Startup 类
    ASP.NET Core解说之Middleware(中间件)
    一、Redis安装 Redis学习记录
  • 原文地址:https://www.cnblogs.com/clb123/p/10168900.html
Copyright © 2011-2022 走看看