Precision adder
Problem Description
input two integers a and b, and outputs of the two integers. a and b are not more than 100.
Algorithmic descriptions
since a and b are relatively large, can not be used directly in the standard data types of stored language. For this problem, generally use an array to handle.
Define an array A, A [0] is used to store a bit, A [1] for storing a ten, and so on. B can also be used to store an array b.
C = a + b is calculated when the first A [0] and B [0] are added, if there is carry, carry put (i.e., the tens and) into r, and the existence of the single-digit the C [0], i.e., C [0] is equal to (A [0] + B [ 0])% 10. Then calculates A [1] and B [1] are added, then the feed should also be low also add up the value r, i.e., C [1] should be A [1], B [1] and three number r and. If there carry generation, you may still carry the new stored into the r, and stored into the bit C [1] in. And so on, you can find all the bits C.
Finally, the C output can be.
Input format
input comprises two rows, a first row of non-negative integers a, a second line non-negative integer b. Two integers no more than 100, the highest two-bit number is not zero.
Output format
output line, represents the value of a + b.
Sample input
20100122201001221234567890
2010012220100122
Sample Output
20100122203011233454668012
problem analysis:
对两个数字的加法来说,如果有一个更长,需要注意的有:加法结束后是否有进位;剩余数字的处理;
步骤:
1,将字符串转化成数组数据(ASCII的处理;存放的数据顺序)
2,找出更长的数组
3,进行加减
4,加法结束后判断是否有进位,以及剩余数字的赋值处理
5,倒序输出
Code shows (Verified):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 101
#define max(x,y) (x>y ? x:y)
#define min(x,y) (x<y ? x:y)
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
int i;
int len_a, len_b;
char a[M], b[M];
char a2[M] = { 0 }, b2[M] = { 0 }; //存放颠倒顺序后的数字串
int c[1 + M] = { 0 };
gets(a);
gets(b);
len_a = strlen(a);
len_b = strlen(b);
int la = len_a - 1;
int lb = len_b - 1;
// 首尾调换顺序 存储在a2[] b2[] 中 方便相加
for (i = 0; i < len_a; i++)
{
a2[la] = a[i]-48; // 将ASCII 码值 减去48(48-57) 所剩数字 为 0-9
la -= 1;
}
for (i = 0; i < len_b; i++)
{
b2[lb] = b[i]-48;
lb -= 1;
}
int k=0; // k 保存进位数据
int j = 0; // 数组c[] 的长度
int m = max(len_a, len_b); // 两个数组最长的一个的长度 以此来进行 所有数字的相加
int n = min(len_a, len_b); // 两个数字与最短的一个的长度 以此进行加法的及时转换
for(i=0;i<n;i++) // 两个数组相加
{
c[j] = (a2[i] + b2[i]) + k;
k = c[j]/10;
c[j]=c[j]%10;
j++;
if (i == n - 1 && k>0) // 最后一个进位
{
c[j] = k;
}
}
// 将短数相加之后 剩下的数据直接赋值给c[]
for (i = n; i<m; i++)
{
if (len_a>len_b) // 与剩余的长的数 相加
{
if (k > 0)
{
c[j] = a2[i] + k; // 两数相加之后还有进位 如果是9的话 这个地方没有进位 有问题
if(c[j]==10) // 加上判定条件后就ok 了
{
c[j]%=10;
k=1;
}
else
{
k=0;
}
}
else
c[j] = a2[i]; // 两数相加之后无进位
j++;
}
else
{
if (k > 0)
{
c[j] = b2[i] + k;
if(c[j]==10)
{
c[j]%=10;
k=1;
}
else
{
k=0;
}
}
else
c[j] = b2[i];
j++;
}
}
if(len_a == len_b && k>0)
printf("1");
for (i = j-1; i>=0; i--) // 两个数组相加之后的和的数据输出 i=j-1 从后往前输出
{
printf("%d", c[i]);
}
return 0;
}
Bubble:
ah, nothing special This question is difficult, still need to pay attention to deal with some of the details. ok, call it a day.