高精度计算

本文详细介绍了大整数数据的接收、存储方法,以及高精度计算中的加法、减法、竖式除法、模拟减法等运算,包括阶乘计算、因子查找和大整数乘法示例,以及如何计算大整数除以13的商和余数。
摘要由CSDN通过智能技术生成

主页:(*´∇`*) 咦,又好了~ xiaocr_blog


(1)数据的接收方法和存储方法:

当输入的数据很长的时候,可采取字符串方式输入,这样可以输入位数很长的数,利用字符串函数和操作运算,将每一位数取出,存入数组中。

(2)高精度数位数确定:

位数确定:接收时往往是用字符串的,所以它的位数就等于字符串长度。

(3)高精度除法分为竖式除法还有模拟减法两种

//加法进位 c[i] = a[i] + b[i]
if(c[i]>=10){c[i] %= 10;++c[i+1];}
//减法借位
if(a[i]<b[i]){--a[i+1];a[i]+=10;
     c[i] = a[i] - b[i]  }
//高除低除法储存无需倒置,竖式除法
//高除高为减法模拟除法

#include<bits/stdc++.h>
using namespace std;
int main() {
	char a1[100], b1[100];
	int a[100], b[100], c[100];
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	gets_s(a1);
	gets_s(b1);
	int lena = strlen(a1);
	int lenb = strlen(b1);
	for (int i = 0; i <= lena - 1; i++) {
		a[lena - i] = a1[i] - '0';
	}
	for (int i = 0; i <= lenb - 1; i++) {
		b[lenb - i] = b1[i] - '0';
	}
	int lenc = 1;
	int x = 0;
	while (lenc <= lena || lenc <= lenb) {
		c[lenc] = a[lenc] + b[lenc]+x;
		x = c[lenc] / 10;
		c[lenc] %= 10;
		lenc++;
	}
	c[lenc] = x;
	if (c[lenc] == 0) {//处理最高位
		lenc--;
	}
	for (int i = lenc; i >= 1; i--) {
		cout << c[i];
	}
	return 0;
}
#include<bits\stdc++.h>
using namespace std;
int main() {
	char a1[100], b1[100],q[100];
	int a[100], b[100], c[100];
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	gets(a1);gets(b1); 
	if (strlen(a1) < strlen(b1) || (strlen(a1) == strlen(b1)&&strcmp(a1, b1) < 0)) {
		strcpy(q, a1);
		strcpy(a1, b1);
		strcpy(b1, q);
		cout << "-";
	}
	int lena = strlen(a1);
	int lenb = strlen(b1);
	for (int i = 0; i <= lena - 1; i++) {
		a[lena - i] = a1[i] - '0';
	}
	for (int i = 0; i <= lenb - 1; i++) {
		b[lenb - i] = b1[i] - '0';
	}
	int lenc = 1;
	while (lenc <= lena || lenc <= lenb) {
		if (a[lenc] < b[lenc]) {
		 a[lenc] += 10;	
		 a[lenc + 1]--;
		}
			c[lenc] = a[lenc] - b[lenc];
		lenc++;
	}
	lenc--;
	while ((c[lenc] == 0) && (lenc > 1)) {
		lenc--;
	}
	for (int i = lenc; i >= 1; i--) {
		cout << c[i];
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
	char a1[101],b1[101];
	int a[101],b[101],c[101];
	int lena,lenb,lenc;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	gets(a1),gets(b1); 
	lena = strlen(a1);lenb = strlen(b1);
	for(int i =0;i<=lena-1;i++){
		a[lena-i] = a1[i] - '0';
	}
	for(int i =0;i<=lenb-1;i++){
		b[lenb-i] = b1[i] - '0';
	}
	for(int i =1;i<=lena;i++){
		for(int j =1;j<=lenb;j++){
			c[i+j-1] += a[i]*b[j];
			c[i+j] += c[i+j-1]/10;
			c[i+j-1]%=10;
		}
	}
	lenc = lena + lenb;
	if(c[lenc]==0&&lenc>=1){
		lenc--;
	}
	for(int i =lenc;i>=1;i--){
		cout<<c[i];
	}
	return 0;
} 
#include<bits/stdc++.h>
using namespace std;
char a1[101];
int a[101],c[101],x;
long long b;
int main(){
	cin>>a1>>b;
	int lena = strlen(a1);
	for(int i =1;i<=lena;i++)
	{
		a[i] = a1[i-1] - '0';
	}
	for(int i=1;i<=lena;i++){
		c[i] = (10*x+a[i])/b;
		x = (10*x+a[i])%b;
	}
	int lenc =1;
	while(c[lenc]==0&&lenc<lena){
		lenc++;
	}
	for(int i = lenc;i<=lena;i++){
		cout<<c[i];
	}
	
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
int a[101], b[101], c[101];
void init(int a[]) {
	char st[101];
	cin >> st;
	a[0] = strlen(st);
	for (int i = 1; i <= a[0]; i++) {
		a[i] = st[a[0]-i] - '0';
	}
}
void output(int a[]) {
	if (a[0] == 0) {
		cout << 0;
		return;
	}
	for (int i = a[0]; i >= 1; i--) {
		cout << a[i];
	}
}
int compare(int a[], int b[]) {
	if (a[0] > b[0]) return 1;
	if (a[0] < b[0]) return -1;
	for (int i = a[0]; i >= 1; i--) {
		if (a[i] > b[i]) return 1;
		if (a[i] < b[i]) return -1;
	}
	return 0;
}
void minus_(int a[], int b[]) {
	int flag;
	flag = compare(a, b);
	if (flag == 0) {
		a[0] = 0;
		return;
	}
	if (flag == 1) {
		for (int i = 1; i <= a[0]; i++) {
			if (a[i] < b[i]) {
				a[i + 1]--; a[i] += 10;
			}
			a[i] -= b[i];
		}
		while (a[a[0]] == 0 && a[0] > 0) {
			a[0]--;
		}
		return;
	}
}
void numcpy(int p[], int q[], int det) {
	for (int i = 1; i <= p[0]; i++) {
		q[i + det - 1] = p[i];
		q[0] = p[0] + det - 1;
	}
}
void chuGao(int a[], int b[], int c[]) {
	int tem[101];
	c[0] = a[0] - b[0] + 1;

	for (int i = c[0]; i >= 1; i--) {
		memset(tem, 0, sizeof(tem));
		numcpy(b, tem, i);
		while (compare(a, tem) > 0) {
			c[i]++;
			minus_(a, tem);
		}

	}
	while (c[0] > 0 && c[c[0]] == 0) {
		c[0]--;
	}
	return;
}
int main() {
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	init(a), init(b);
	chuGao(a, b, c);
	output(c);

	return 0;
}

1.大整数的加法

#include<bits/stdc++.h>
using namespace std;
char a1[101], b1[101];
int a[101], b[101], c[101];
int lena, lenb, lenc;
int main() {
	cin >> a1 >> b1;
	lena = strlen(a1); lenb = strlen(b1);
	for (int i = 0; i <= lena - 1; i++) {
		a[lena - i] = a1[i] - '0';
	}
	for (int i = 0; i <= lenb - 1; i++) {
		b[lenb - i] = b1[i] - '0';
	}
	lenc = 1;
	while (lenc <= lena || lenc <= lena) {
		lenc++;
	}
	int x = 0;
	for (int i = 1; i <= lenc; i++) {
		c[i] = a[i] + b[i] + x;
		x = c[i] / 10;
		c[i] %= 10;
	}
	while (c[lenc] == 0 && lenc > 0) {
		lenc--;
	}
	for (int i = lenc; i >=1; i--) {
		cout << c[i];
	}

	return 0;
}

2.给定一个整数,计算2的N次方

#include<bits/stdc++.h>
using namespace std;
int a[1001];
int N;
int main() {
	a[1] = 1;
	cin >> N;
	int k = 1,x=0;
	for (int j = 1; j <= N; j++) {
		 x = 0;
		for (int i = 1; i <= k; i++) {
			a[i] = a[i] * 2 + x;
			x = a[i] / 10;
			a[i] %= 10;
			if (x != 0 && i == k) {
				k++;
				//伸缩
			}
		}
	}
	for (int i = k; i >= 1; i--) {
		cout << a[i];
	}
	return 0;
}

3.大整数的因子

#include<bits/stdc++.h>
using namespace std;
char a1[101];
int a[101], c[101], lena,b, x;
int main() {
	cin >> a1;
	lena = strlen(a1);
	for (int i = 1; i <= lena; i++) {
		a[i] = a1[i - 1] - '0';
	}
	for (int i = 2; i <= 9; i++) {
		x = 0;
		for (int j = 1; j <= lena; j++) {
			c[j] = (a[j] + x * 10) / i;
			x = (10 * x + a[j]) % i;
		}
		if (x % i == 0) {
			cout << i << " ";
		}
	}


	return 0;
}

4.*10000以内n的阶乘

#include<bits/stdc++.h>
using namespace std;
int a[10001],b[10001];
int main(){
	int n,fac,k=1;
	cin>>n;
	a[1] = 1;
	for(int i =1;i<=n;i++){
		for(int j =1;j<=k;j++){
			a[j] = a[j]*i;
			a[j]= a[j-1]/10+a[j];
			a[j-1] = a[j-1]%10;
			if(a[j]>=10&&j>=k){
				k++;
			}
		}
	}
	for(int i =k;i>=1;i--){
		cout<<a[i];
	}
	return 0;
}


5*.高精度计算之阶乘和

#include<bits/stdc++.h>
using namespace std;
int a[10001], sum[10001], n;
void getsum(int a[], int b[]) {
	int tem[10001] = { 0 };
	int x = 0;
	for (int i = 1; i <= 10001; i++) {
		tem[i] = a[i] + b[i] + x;
		x = tem[i] / 10;
		tem[i] %= 10;
	}
	for (int i = 1; i <= 10001; i++) {
		b[i] = tem[i];
	}
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		memset(sum, 0, sizeof(sum));
		a[1] = 1; int k = 1;
		for (int j = 1; j <= i; j++) {
			for (int p = 1; p <= k; p++) {
				a[p] = a[p] * j;
				a[p] = a[p - 1] / 10 + a[p];
				a[p - 1] = a[p - 1] % 10;
				if (a[p] >= 10 && p >= k) {
					k++;
				}
			}
		}
		getsum(a, sum);
	}
	int t = 10001;
	while (sum[t] == 0) {
		t--;
	}
	for (int i = t; i >= 1; i--) {
		cout << sum[i];
	}

	return 0;
}
#include<stdio.h>
#define N 10000 
int main()
{
	int i, j, x, k, n, b[N] = { 0 };
	scanf("%d", &n);
	for (k = 1; k <= n; k++)
	{
		int a[N] = { 0 };
		a[9999] = 1;
		for (i = 1, x = 0; i <= k; i++) {
			for (j = 9999; j >= 0; j--)
			{
				a[j] = a[j] * i + x;
				x = a[j] / 10;
				a[j] = a[j] % 10;
			}
		}
		for (j = 9999; j >= 0; j--)
		{
			b[j] = b[j] + a[j];
			if (b[j] >= 10)
			{
				b[j - 1] = b[j - 1] + 1;
				b[j] = b[j] - 10;
		}
	}
    }
	for (i = 0; i < N; i++)
		if (b[i] != 0) break; 
	for (x = i; x < N; x++)
		printf("%d", b[x]);
	return 0;
}


6.大整数乘法

#include<bits/stdc++.h>
using namespace std;
char a1[10001], b1[10001];
int a[10001], b[10001], c[10001];
int main() {
	int lena, lenb, lenc;
	cin >> a1 >> b1;
	lena = strlen(a1);
	lenb = strlen(b1);
	for (int i = 0; i <= lena-1; i++) {
		a[lena - i] = a1[i] - '0';
	}
	for (int i = 0; i <= lenb-1; i++) {
		b[lenb - i] = b1[i] - '0';
	}
	lenc = lena + lenb;
	for (int i = 1; i <= lena; i++) {
		for (int j = 1; j <= lenb; j++) {
			c[i + j - 1] += b[j] * a[i];
			c[i + j] += c[i + j - 1] / 10;
			c[i+j-1]%= 10;

		}
	}
	lenc = lena + lenb;
	while(c[lenc] == 0 && lenc > 0) {
		lenc--;
	}
	for (int i = lenc; i >= 1; i--) {
		cout << c[i];
	}
	return 0;
}

7.大整数除以13,输出商和余数

#include<bits/stdc++.h>
using namespace std;
char a1[10001];
int a[10001],b[101];
const int n = 13;
int x;
int main() {
	cin >> a1;
	int lena = strlen(a1);
	for (int i = 1; i <= lena; i++) {
		a[i] = a1[i - 1]-'0';
	}
	for (int i = 1, x = 0; i <= lena; i++) {
		b[i] = (a[i] + x * 10) / 13;;
		x = (a[i] + x * 10) % 13;
	}
	int len = 1;
	while (b[len] == 0&&len<lena) {
		len++;
	}
	for (int i = len; i <= lena; i++) {
		cout << b[i];
	}
	cout << endl;
	cout << x;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cr不是铬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值