【C++】高精度加减法

前言

高精度,是模拟算法的一种。模拟算法,就是模拟我们人类的思考过程进行运算。那么,高精度加减法便是模拟加减法竖式罢了(乘除法以后再写)。

我第一次听说高精度时,我还刚会用for循环呢。结果下一次遇到高精度时,已经是学校比赛的赛场上了;于是我在赛场上毫无预兆的初次挑战高精度。结果毫无疑问,我只拿了一小半的分——我没有对齐数位,就只拿了两个加数位数相同的数据点的分。后来,就在前天,我第二次挑战高精度,发现其实这一点都不难。不要听别人说高精度很难,就主动放弃它。


程序

程序详解

加法

第二次我是用char数组进行一切的运算,要多处理几个48;我们现在就不给自己找麻烦,用char数组输入,用int数组运算

int main(){
	return 0;
}

  1. 首先我们为了方便输入,所以还是得先借助char数组string

    #include<cstdio>	//scanf()
    char scan_a[1024], scan_b[1024];
    int main(){
    	scanf("%s %s", &scan_a, &scan_b);
    	return 0;
    }
    

  2. 为了更加方便的防止位数不同时数组越界,所以我们让(位数)大的数加上(位数)小的数
    设a为大数,b为小数;我们现在就判断并调整位置。

    #include<cstdio>	//scanf()
    #include<cstring>	//strlen(), strcpy()
    char scan_a[1024], scan_b[1024], t[1024];
    int a_len, b_len;
    int main(){
    	scanf("%s %s", &scan_a, &scan_b);
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	if(a_len<b_len){
    		strcpy(t, scan_a);
    		strcpy(scan_a, scan_b);
    		strcpy(scan_b, t);
    	}
    	return 0;
    }
    

  3. 为了方便操作,我们用int数组进行运算。现在就把数据“移动”到int数组中。

    #include<cstdio>	//scanf()
    #include<cstring>	//strlen(), strcpy()
    char scan_a[1024], scan_b[1024], t[1024];
    int a_len, b_len, a[1024], b[1024];
    int main(){
    	scanf("%s %s", &scan_a, &scan_b);
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	if(a_len<b_len){
    		strcpy(t, scan_a);
    		strcpy(scan_a, scan_b);
    		strcpy(scan_b, t);
    	}
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	for(int i=1; i<=a_len; i++){
    		a[i]=scan_a[i-1]-'0';
    	}
    	for(int i=1; i<=b_len; i++){
    		b[i]=scan_b[i-1]-'0';
    	}
    	return 0;
    }
    

    为了方便后面进位,所以int数组从1开始。
    因为char类型的数据中'0'的值(ASCLL码)为48,('1'=49'2'=50…)所以转为int类型时,要减去48。(这里写减'0'一样的)


  4. 接下来,就可以直接进行加法运算了。(我们先把每一位加起来,后面再单独做进位。)
    因为要防止数组越界,所以我们做加法时,只循环数位短的加数(b)就够了。

    #include<cstdio>	//scanf()
    #include<cstring>	//strlen(), strcpy()
    char scan_a[1024], scan_b[1024], t[1024];
    int a[1024], b[1024];
    int main(){
    	scanf("%s %s", &scan_a, &scan_b);
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	if(a_len<b_len){
    		strcpy(t, scan_a);
    		strcpy(scan_a, scan_b);
    		strcpy(scan_b, t);
    	}
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	for(int i=1; i<=a_len; i++){
    		a[i]=scan_a[i-1]-'0';
    	}
    	for(int i=1; i<=b_len; i++){
    		b[i]=scan_b[i-1]-'0';
    	}
    	for(int i=0; i<=b_len; i++){
    		a[a_len-i]+=b[b_len-i];
    	}
    	return 0;
    }
    

  5. 现在,加法做完了,但是还没有进位。那么我们现在就来做进位。
    进位,逢十进一嘛。我们只需要遍历数组a;判断当前位是否逢十;如果就是,就进一(前一位加一)。

    #include<cstdio>	//scanf()
    #include<cstring>	//strlen(), strcpy()
    char scan_a[1024], scan_b[1024], t[1024];
    int a_len, b_len, a[1024], b[1024];
    int main(){
    	scanf("%s %s", &scan_a, &scan_b);
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	if(a_len<b_len){
    		strcpy(t, scan_a);
    		strcpy(scan_a, scan_b);
    		strcpy(scan_b, t);
    	}
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	for(int i=1; i<=a_len; i++){
    		a[i]=scan_a[i-1]-'0';
    	}
    	for(int i=1; i<=b_len; i++){
    		b[i]=scan_b[i-1]-'0';
    	}
    	for(int i=0; i<=b_len; i++){
    		a[a_len-i]+=b[b_len-i];
    	}
    	for(int i=a_len; i>0; i--){
    		if(a[i]>=10){
    			a[i-1]+=1;
    			a[i]-=10;
    		}
    	}
    	return 0;
    }
    

    1. 好了,上面一顿操作下来,看似很难的高精度加法其实已经被我们轻轻松松的做完了。最后输出即可。
      当然,输出要化简。数字前几位如果是0可就不输出。
    #include<cstdio>	//scanf(), printf()
    #include<cstring>	//strlen(), strcpy()
    char scan_a[1024], scan_b[1024], t[1024];
    int a_len, b_len, a[1024], b[1024];
    bool flag;
    int main(){
    	scanf("%s %s", &scan_a, &scan_b);
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	if(a_len<b_len){
    		strcpy(t, scan_a);
    		strcpy(scan_a, scan_b);
    		strcpy(scan_b, t);
    	}
    	a_len=strlen(scan_a);
    	b_len=strlen(scan_b);
    	for(int i=1; i<=a_len; i++){
    		a[i]=scan_a[i-1]-'0';
    	}
    	for(int i=1; i<=b_len; i++){
    		b[i]=scan_b[i-1]-'0';
    	}
    	for(int i=0; i<=b_len; i++){
    		a[a_len-i]+=b[b_len-i];
    	}
    	for(int i=a_len; i>0; i--){
    		if(a[i]>=10){
    			a[i-1]+=1;
    			a[i]-=10;
    		}
    	}
    	flag=true;
    	for(int i=0; i<=a_len; i++){
    		if(a[i]==0 && flag);
    		else{
    			printf("%d", a[i]);
    			flag=false;
    		}
    	}
    	return 0;
    }
    

减法

减法跟加法是一样的。我们先来找一下高精度加法的核心代码——是逐位相加和进位

for(int i=0; i<=b_len; i++){
	a[a_len-i]+=b[b_len-i];
}
for(int i=a_len; i>0; i--){
	if(a[i]>=10){
		a[i-1]+=1;
		a[i]-=10;
	}
}

那么,减法其他的地方不变,无非就是核心打码变成了逐位相减和退位

for(int i=0; i<=b_len; i++){
	a[a_len-i]-=b[b_len-i];
}
for(int i=a_len; i>0; i--){
	if(a[i]<0){
		a[i-1]-=1;
		a[i]+=10;
	}
}

然后放到完整代码中即可。

#include<cstdio>	//scanf(), printf() 
#include<cstring>	//strlen(), strcpy()
char scan_a[1024], scan_b[1024], t[1024];
int a_len, b_len, a[1024], b[1024];
bool flag;
int main(){
	scanf("%s %s", &scan_a, &scan_b);
	a_len=strlen(scan_a);
	b_len=strlen(scan_b);
	if(a_len<b_len){
		strcpy(t, scan_a);
		strcpy(scan_a, scan_b);
		strcpy(scan_b, t);
	}
	a_len=strlen(scan_a);
	b_len=strlen(scan_b);
	for(int i=1; i<=a_len; i++){
		a[i]=scan_a[i-1]-'0';
	}
	for(int i=1; i<=b_len; i++){
		b[i]=scan_b[i-1]-'0';
	}
	for(int i=0; i<=b_len; i++){
		a[a_len-i]-=b[b_len-i];
	}
	for(int i=a_len; i>0; i--){
		if(a[i]<0){
			a[i-1]-=1;
			a[i]+=10;
		}
	}
	flag=true;
	for(int i=0; i<=a_len; i++){
		if(a[i]==0 && flag){}
		else{
			printf("%d", a[i]);
			flag=false;
		}
	}
	return 0;
}

完整代码

其实前面都已经放出来了。

高精度加法

#include<cstdio>
#include<cstring>
char scan_a[1024], scan_b[1024], t[1024];
int a_len, b_len, a[1024], b[1024];
bool flag;
int main(){
	scanf("%s %s", &scan_a, &scan_b);
	a_len=strlen(scan_a);
	b_len=strlen(scan_b);
	if(a_len<b_len){
		strcpy(t, scan_a);
		strcpy(scan_a, scan_b);
		strcpy(scan_b, t);
	}
	a_len=strlen(scan_a);
	b_len=strlen(scan_b);
	for(int i=1; i<=a_len; i++){
		a[i]=scan_a[i-1]-'0';
	}
	for(int i=1; i<=b_len; i++){
		b[i]=scan_b[i-1]-'0';
	}
	for(int i=0; i<=b_len; i++){
		a[a_len-i]+=b[b_len-i];
	}
	for(int i=a_len; i>0; i--){
		if(a[i]>=10){
			a[i-1]+=1;
			a[i]-=10;
		}
	}
	flag=true;
	for(int i=0; i<=a_len; i++){
		if(a[i]==0 && flag);
		else{
			printf("%d", a[i]);
			flag=false;
		}
	}
	return 0;
}

高精度减法

#include<cstdio>	//scanf(), printf() 
#include<cstring>	//strlen(), strcpy()
char scan_a[1024], scan_b[1024], t[1024];
int a_len, b_len, a[1024], b[1024];
bool flag;
int main(){
	scanf("%s %s", &scan_a, &scan_b);
	a_len=strlen(scan_a);
	b_len=strlen(scan_b);
	if(a_len<b_len){
		strcpy(t, scan_a);
		strcpy(scan_a, scan_b);
		strcpy(scan_b, t);
	}
	a_len=strlen(scan_a);
	b_len=strlen(scan_b);
	for(int i=1; i<=a_len; i++){
		a[i]=scan_a[i-1]-'0';
	}
	for(int i=1; i<=b_len; i++){
		b[i]=scan_b[i-1]-'0';
	}
	for(int i=0; i<=b_len; i++){
		a[a_len-i]-=b[b_len-i];
	}
	for(int i=a_len; i>0; i--){
		if(a[i]<0){
			a[i-1]-=1;
			a[i]+=10;
		}
	}
	flag=true;
	for(int i=0; i<=a_len; i++){
		if(a[i]==0 && flag){}
		else{
			printf("%d", a[i]);
			flag=false;
		}
	}
	return 0;
}

尾声

其实,细看每一步都很简单;泛看全局跟我们的计算方法也基本一样(我这个版本基本一样,别人写的可能就是完全一样了)。所以,我们不用怕听似很难的题,把编程看的自然一点,想想我们最平常的思考的思路,就可以很简单的把题目做出来。这就是模拟算法


补:高精度乘法

高精度乘法https://blog.csdn.net/Happynetizens/article/details/136407617


  • 29
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值