洛谷普及场(高精度算法)

  • P1601 A+B Problem(高精)
#include <cstdio>
#include <cstring>
using namespace std ; 
const int N = 550 ;
int a[N] , b[N] , c[N] ; 
char astr[N] , bstr[N] ; 
int main(){
	scanf ("%s",astr) ;
	scanf ("%s",bstr) ; 
	int alen = strlen(astr) ; 
	int blen = strlen(bstr) ; 
	int clen = alen > blen ? alen : blen ;  	
	for (int i = 0 ; i < alen ; ++i)
		a[i] = astr[alen-i-1] - '0' ;  
	for (int i = 0 ; i < blen ; ++ i)
		b[i] = bstr[blen-i-1] - '0' ;  
	for (int i = 0 ; i < clen ; ++ i){
		c[i] += a[i] + b[i] ;
		if(c[i] >= 10)	{
			c[i+1] += c[i]/10 ; 
			c[i] %= 10 ; 
		}
	}
	while(c[clen] > 0)	clen ++ ; 	
	for (int i = clen-1 ; i >= 0 ; -- i){
		printf ("%d",c[i]) ; 
	}
	printf ("\n") ; 
	return 0 ;  
}
  • P2142 高精度减法
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std ; 
const int N = 10100 ;
int a[N] , b[N] , c[N] ; 
string astr , bstr ; 
int main(){
	cin >> astr ; 
	cin >> bstr ;    
	int alen = astr.length() ; 
	int blen = bstr.length() ; 
	if (blen > alen || (blen == alen && bstr > astr)){
		printf ("-") ;
		swap(alen,blen) ;
		swap(astr,bstr) ; 
	}
	int clen = alen > blen ? alen : blen ;  
	for (int i = 0 ; i < alen ; ++i)
		a[i] = astr[alen-i-1] - '0' ;  
	for (int i = 0 ; i < blen ; ++ i)
		b[i] = bstr[blen-i-1] - '0' ;  
	for (int i = 0 ; i < clen ; ++ i){
		c[i] += a[i] - b[i] ;
		if(c[i] < 0)	{
			c[i] += 10 ; 
			c[i+1] -- ; 
		}
	}
	while(c[clen-1] == 0 && clen > 1)	clen -- ; 	
	for (int i = clen-1 ; i >= 0 ; -- i){
		printf ("%d",c[i]) ; 
	}
	printf ("\n") ; 
	return 0 ;  
}
  • P1303 A*B Problem
    这道应该是A的比较久的吧,要注意处理负数的情况和0的情况(变量比较多经常打错)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std ; 
const int N = 2500 ;
int a[N] , b[N] , c[2*N] ; 
string astr , bstr ; 
int main(){
	cin >> astr ; 
	cin >> bstr ; 
	char x = astr[0] , y = bstr[0] ; 
	int alen = astr.length() , blen = bstr.length() ;  
	int clen = alen + blen - 1 ;
	if (x == '0' || y == '0'){
		printf ("0\n") ; 
		return 0 ; 
	} 
	int xnum = 0 , ynum = 0 ; 
	if (x == '-' && y != '-'){
		printf ("-") ;
		xnum = 1 ; 
		-- clen ;  
	}
	if (x != '-' && y == '-'){
		printf ("-") ;
		ynum = 1 ;
		-- clen ;  
	}
	if (x == '-' && y == '-'){
		xnum = ynum = 1 ; 
		clen -= 2 ; 
	}
	
	for (int i = 0 ; i < alen-xnum ; ++i)
		a[i] = astr[alen-i-1] - '0' ;
	for (int i = 0 ; i < blen-ynum ; ++ i)
		b[i] = bstr[blen-i-1] - '0' ;

	for (int i = 0 ; i < alen-xnum ; ++ i){
		for (int j = 0 ; j < blen-ynum ; ++j){
			c[i+j] += a[i] * b[j] ; 
			if (c[i+j] >= 10){
			    c[i+j+1] += c[i+j]/10 ; 
				c[i+j] %= 10 ; 
			}	
		}
	}
	while(c[clen] > 0)	++clen  ; 
	for (int i = clen-1 ; i >= 0 ; -- i){
		printf ("%d",c[i]) ; 
	}
	printf ("\n") ; 
	return 0 ;  
}
  • p1604 B进制星球

其实就是普通的高精度乘法,就是在位的时候把除于10改成除于进制即可,注意这个是2~36进制的,输入输出的时候要处理大于等于10的字符。

	#include <cstdio>
	#include <iostream>
	#include <string>
	#include <cstring>
	using namespace std ; 
	const int N = 2010 ; 
	string astr , bstr ; 
	int a[N] , b[N] , c[N] ; 
	int main(){
		int n ; 
		scanf ("%d",&n) ;
		getchar() ; 
		cin >> astr ; 
		cin >> bstr ; 
		int alen = astr.length() ; 
		int blen = bstr.length() ; 
		int clen = alen > blen ? alen : blen ;
		for (int i = 0 ; i < alen ; ++ i){
			if (astr[alen-i-1] >= '0' && astr[alen-i-1] <= '9'){
				a[i] = astr[alen-i-1] - '0' ; 
			}
			else {
				a[i] = (astr[alen-i-1]-'A') + 10 ; 
			}
		}
		
		for (int i = 0 ; i < blen ; ++i){
			if (bstr[blen-i-1] >= '0' && bstr[blen-i-1] <= '9'){
				b[i] = bstr[blen-i-1] - '0' ; 
			}
			else{
				b[i] = bstr[blen-i-1] - 'A' + 10 ; 
			}
		}
		for (int i = 0 ; i < clen ; ++ i ){
			c[i] += a[i] + b[i] ; 
			if (c[i] >= n){
				c[i+1] += c[i]/n ; 
				c[i] %= n ; 
			}
		}
		while(c[clen] > 0)	++clen ; 
		for (int i = clen-1 ; i >= 0 ; -- i){
			if (c[i] < 10)
				printf ("%d",c[i]) ;
			else{
				printf ("%c",c[i]-10 + 'A') ; 
			}
		}
		printf ("\n") ; 
		return 0 ; 
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值