基础练习 高精度加法 蓝桥杯

/*
输入两个整数a和b,输出这两个整数的和。a和b都不超过100位。
算法描述
由于a和b都比较大,所以不能直接使用语言中的标准数据类型来存储。对于这种问题,一般使用数组来处理。
定义一个数组A,A[0]用于存储a的个位,A[1]用于存储a的十位,依此类推。同样可以用一个数组B来存储b。
计算c = a + b的时候,首先将A[0]与B[0]相加,如果有进位产生,则把进位(即和的十位数)存入r,把和的个位数存入C[0],
即C[0]等于(A[0]+B[0])%10。然后计算A[1]与B[1]相加,这时还应将低位进上来的值r也加起来,即C[1]应该是A[1]、B[1]和r三个数的和.
如果又有进位产生,则仍可将新的进位存入到r中,和的个位存到C[1]中。依此类推,即可求出C的所有位。
最后将C输出即可。
输入格式
输入包括两行,第一行为一个非负整数a,第二行为一个非负整数b。两个整数都不超过100位,两数的最高位都不是0。
输出格式
输出一行,表示a + b的值。
样例输入
20100122201001221234567890
2010012220100122
样例输出
20100122203011233454668012
*/
#if 1
#include<stdio.h>
void nx(char *);
int q_cd(char *);
void huan(char *,char *);
void shuru(int [],int,char *);
void q_he(int [],int,int [],int,int [],int);
void pd_jw(int [],int,int);
void shuchu(int [],int);
int main(void)
{
 char str1[100 + 1];
 char str2[100 + 1];
 gets(str1);
 gets(str2);
 nx(str1);
 nx(str2);
 int shu1[100] = {0};
 int shu2[100] = {0};
 shuru(shu1,100,str1);
 shuru(shu2,100,str2);
 int he[101] = {0};
 q_he(he,101,shu1,100,shu2,100);
 shuchu(he,101);
 return 0;
}
void shuchu(int a[],int n)
{
 while( a[-- n ] == 0 )
 {
 }
 while( n >= 0 )
 {
  printf("%d", a[n --] );
 }
}
void pd_jw(int a[],int n,int i)
{
 if( a[i] > 9 )
 {
  a[i + 1] += a[i] / 10 ;
  a[i] = a[i] % 10 ;
 }
}
void q_he(int he[],int n,int s1[],int m1,int s2[],int m2)
{
 int i;
 for( i = 0 ; i < m1 ; i ++ )
 {
  he[i] += s1[i] + s2[i];
  pd_jw(he,n,i);
 }
}
void shuru(int a[],int n,char * s)
{
 int i = 0;
 while( * s != '\0' )
 {
  a[i ++] += * s - '0' ;
  s ++ ;
 }
}
void huan(char * p_1,char * p_2)
{
 char tmp = * p_1;
 * p_1 = * p_2 ;
 * p_2 = tmp ;
}
int q_cd(char * s)
{
 int cd = 0;
 while( * s != '\0' )
 {
  cd ++ ;
  s ++ ;
 }
 return cd ;
}
void nx(char * s)
{
 int t = 0 , w = q_cd(s) - 1;
 while( t < w )
 {
  huan(s + t,s + w);
  t ++ ;
  w -- ;
 }
}
#endif
#if 0
#include<stdio.h>
void nx(char *);
int q_cd(char *);
void huan(char *,char *);
void shuchu(int [],int,char *,char *);
int to_shu(char);
void pd_jw(int [],int);
int main(void)
{
 char str1[100 + 1];
 char str2[100 + 1];
 gets(str1);
 gets(str2);
 nx(str1);
 nx(str2);
 int shu1[100] = {0};
 int shu2[100] = {0};
 int he[101] = {0};
 shuchu(he,101,str1,str2);
 return 0;
}
int to_shu(char c)
{
 //printf("shu = %d\n", c - '0');
 return c - '0';
}
void pd_jw(int a[],int i)
{
 if( a[i] > 9 )
 {
  a[i + 1] = a[i] / 10;
  a[i] = a[i] % 10;
 }
}
void shuchu(int a[],int n,char * s1,char * s2)
{
 while( * s1 != '\0' || * s2 != '\0' )
 {
  int i;
  for( i = 0 ; i < n ; i ++ )
  {
   a[i] += to_shu(* s1) + to_shu(* s2);
   pd_jw(a,i);
  }
  s1 ++ ;
  s2 ++ ;
 }
 while( a[ -- n] == 0)
 {
 }
 while( n >= 0 )
 {
  printf("%d", a[n --] );
 }
}
void huan(char * p_1,char * p_2)
{
 char tmp = * p_1;
 * p_1 = * p_2 ;
 * p_2 = tmp ;
}
int q_cd(char * s)
{
 int cd = 0;
 while( * s != '\0' )
 {
  cd ++ ;
  s ++ ;
 }
 return cd ;
}
void nx(char * s)
{
 int t = 0 , w = q_cd(s) - 1;
 while( t < w )
 {
  huan(s + t,s + w);
  t ++ ;
  w -- ;
 }
}
#endif

猜你喜欢

转载自blog.csdn.net/qq_40990854/article/details/80037256