A + B Problem II hd 1002
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:
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
想法:因為大數在int或者__int64當中加減仍存在誤差,所以,我們將大數存在字串中,然後讓另一個陣列倒序儲存。然後將兩個陣列依次相加,如果大於9,進位。最後需要判斷最後一位是否為0,如果是0,不輸出最後一位,然後倒序輸出,如果是0,就直接倒序輸出。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int max(int x,int y)
{
int z;
z=x>y?x:y;//比較兩個值大小
return z;
}
int main()
{
int lena,lenb,i,n,t=0,z,numa[1100],numb[1100];
char a[1100],b[1100];
scanf("%d",&n);
while(n--)
{
memset(numa,0,sizeof(numa));
memset(numb,0,sizeof(numb));//int陣列清零
scanf("%s%s",a,b);
lena=strlen(a);
lenb=strlen(b);
for(i=0;i<lena;i++)
numa[lena-1-i]=a[i]-'0';
for(i=0;i<lenb;i++)
numb[lenb-1-i]=b[i]-'0';
z=max(lena,lenb);
for(i=0;i<z;i++)//此處i值的範圍就是兩個字串長度的最大者
{
numa[i]=numa[i]+numb[i];
if(numa[i]>9)//處理相加大於十的情況
{
numa[i]=numa[i]-10;
numa[i+1]++;
}
}
t++;
printf("Case %d:\n",t);
printf("%s + %s = ",a,b);
if(numa[z]==0)// 判斷陣列第z位是否為零
{
for(i=z-1;i>=0;i--)
printf("%d",numa[i]);
printf("\n");
}
else
{
for(i=z;i>=0;i--)
printf("%d",numa[i]);
printf("\n");
}
if(n!=0)//每一次的輸入遇上一次的輸出之間空一行 ,n=0是輸出的是最後一組資料,即不用空出一行
printf("\n");
}
return 0;
}
相關文章
- HDU 1002 A + B Problem II
- hduoj1002 A + B Problem II (大數相加 字串模擬)字串
- ESTR1002 Problem Solving by Programming
- The 2024 ICPC Asia EC Regionals Online Contest (II) - Problem B. Mountain BookingAI
- [PAT B] 1002 寫出這個數
- P1865 A % B Problem
- QOJ6836 A Plus B Problem
- [Algorithm] 1. A+B ProblemGo
- B1002 寫出這個數 (20分)
- Online Judge——1000.A+B Problem(c++)C++
- CF741B:https://codeforces.com/problemset/problem/741/BHTTP
- 1002
- 題解:AT_arc181_b [ARC181B] Annoying String Problem
- The 2024 ICPC Asia EC Regionals Online Contest (II) - Problem H. Points Selection
- PAT-B 1002 寫出這個數 【字串+遞迴列印】字串遞迴
- CF1934B Yet Another Coin Problem 題解
- 2020 ICPC上海 B.Mine Sweeper II
- 【簡單菊花圖】Codeforce 1583Problem - B.md
- 1002 Running Median
- XYD1002CSPS
- 1002模擬賽
- P1001 A+B Problem(整活-dijstra堆最佳化)JS
- 1002,概統,積分
- 題解:P9938 [USACO21OPEN] Acowdemia II B
- Mathematical Problem
- Sum Problem
- 題解:P9953 [USACO20OPEN] Social Distancing II B
- 2020ICPC上海 B.Mine Sweeper II(思維,亂搞)
- Yet Another Problem
- Nanami and the Constructive ProblemNaNStruct
- Prime Ring Problem
- 2019 MCM Problem A
- Picture hd 2052
- E. Not a Nim Problem
- Nanami and the House Protecting ProblemNaN
- HDU - 6182 A Math Problem
- Problem A. Ascending Rating
- Fixed "There was a problem with the editor 'vi'"