HDU1002 A + B Problem II 大数问题

时间:2023-02-13 12:21:50

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 409136    Accepted Submission(s): 79277

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 
Input
The 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.
 
Output
For 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

 
思路:
采用字符数组储存两个加数,模拟小学的加法竖式计算
注意点:
1.俩个加数长度不等的时候,长度短的加数前面加0,0的个数为二者长度相减的绝对值
2.输出格式问题,只有输出最后一组数据的结果的时候,一个回车,其余都是两个回车
代码如下:
#include<bits/stdc++.h>
int main()
{
int n;
scanf("%d",&n);
int y=;
while(y<=n)
{
char a[]= {''},b[]= {''},C[],A[],B[];
getchar();
scanf("%s %s",a,b);
int l1=strlen(a);
int l2=strlen(b);
if(l1>l2)
{
int k=l1-l2;
char d[k];
for(int i=; i<k; i++)
d[i]='';
d[k]='\0';
B[]='\0';
strcat(B,d);
strcat(B,b);
A[]='\0';
strcat(A,a);
}
else if(l1<l2)
{
int k=l2-l1;
char d[k];
for(int i=; i<k; i++)
d[i]='';
d[k]='\0';
A[]='\0';
strcat(A,d);
strcat(A,a);
B[]='\0';
strcat(B,b);
}
else if(l2==l1)
{
A[]='\0';
B[]='\0';
strcat(A,a);
strcat(B,b);
}
l1=strlen(A);
l2=strlen(B);
int k=,cc=;
for(int i=l1-,j=l2-; i>=&&j>=; i--,j--)
{
int t=A[i]-''+B[j]-''+cc;
if(t>=)
{
cc=;
t=t-;
}
else
{
cc=;
}
C[k]=t+'';
k++;
}
if(cc==)
{
C[k]='';
C[k+]='\0';
}
else
{
C[k]='\0';
}
int l3=strlen(C);
printf("Case %d:\n",y);
printf("%s + %s = ",a,b);
for(int i=l3-; i>=; i--)
{
printf("%c",C[i]);
}
printf("\n");
if(y!=n)
printf("\n");
y++;
}
return ;
}

HDU1002 A + B Problem II 大数问题的更多相关文章

  1. hdu1002 A &plus; B Problem II&lpar;大数题&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...

  2. HDU1002 -A &plus; B Problem II&lpar;大数a&plus;b&rpar;

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. hdu1002 A &plus; B Problem II&lbrack;大数加法&rsqb;

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1002 题干 代码和解释 由题意这是一个涉及到大数的加法问题.去看了一眼大数加法的方法感觉头很大,然后突然发现Java可以流氓解决大数问题,毅 ...

  4. 杭电ACM(1002) -- A &plus; B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  5. hdu1002 A &plus; B Problem II&lpar;高精度加法&rpar; 2016-05-19 12&colon;00 106人阅读 评论&lpar;0&rpar; 收藏

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. &lbrack;HDU1002&rsqb; A &plus; B Problem II

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  7. A &plus; B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

  8. A &plus; B Problem II&lpar;大数加法)

    一直格式错误,不想改了,没A #include <iostream> #include <stdio.h> #include <string.h> #include ...

  9. HDU 1023 Train Problem II 大数打表Catalan数

    一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...

随机推荐

  1. jQuery 中的事件冒泡和阻止默认行为

    1.事件冒泡 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...

  2. python 五子棋

    http://www.skywind.me/blog/archives/1029 http://blog.csdn.net/skywind/article/details/8164713 https: ...

  3. win 7 下Maven环境的搭建

    Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具. Maven是什么? 比较正式的定义:Maven是一个项目管理工具,它包含了: 一个项目对象模型 (Project O ...

  4. 自增锁预分配ID

    http://www.cnblogs.com/xpchild/p/3825309.html mysql> show create table pp; CREATE TABLE `pp` ( `i ...

  5. 收起虚拟键盘的各种方法 -- IOS

    使用虚拟键盘来输入资讯,是 iOS 的重要互动方式之一,虚拟键盘通常会自动出现在可以编辑的 UITextField 或是 UITextView 的编辑事件中,叫出键盘固然容易,但是要把它收起来,可就没 ...

  6. POJ1159Palindrome

    http://poj.org/problem?id=1159 题意 : 给定一个字符串,问最少插入多少字符,使该字符串变成回文串 思路 : 设原字符串序为X,逆序列为Y,则最少需要补充的字母数 = X ...

  7. javascript中字符串的trim功能表达式

    string.replace(/(^\s*)|(\s*$)/g, "") 用来删除行首行尾的空白字符(包括空格.制表符.换页符等等)

  8. 《Go并发编程实战》读书笔记-初识Go语言

    <Go并发编程实战>读书笔记-初识Go语言 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在讲解怎样用Go语言之前,我们先介绍Go语言的特性,基础概念和标准命令. 一. ...

  9. Docker 容器状态查看 - 五

    1.top stats 查看 docker 容器的状态信息 查看容器状态: docker stats nginx1 查看进程信息: docker top nginx1 2.inspect 使用 doc ...

  10. &lbrack;Shell&rsqb;Bash变量:自定义变量 &amp&semi; 环境变量 &amp&semi; 位置参数变量 &amp&semi; 预定义变量

    --------------------------------------------------------------------------------- 变量是计算机内存的单元,其中存放的值 ...