c++大数据加减乘运算

欧拉第13题

大整数相加

计算出以下一百个50位数的和的前十位数字。

#include<stdio.h>
#include<string.h>
using namespace std;
#define max 52
char str[max + 5] = {0};
int ans[max + 5] = {0};
int main() {
	while (~scanf("%s", str)) {
		int len = strlen(str);
		if (len > ans[0]) ans[0] = len;
		//倒过来存
		for (int i = 0; i < len; i++) {
			ans[len - i] += str[i] - '0';
		}
		//核心
		for (int i = 1; i <= ans[0]; i++) {
			if (ans[i] < 10) continue;
			ans[i + 1] += ans[i] / 10;
			ans[i] %= 10;
			ans[0] += (i == ans[0]);
		}
	}
	for (int i = ans[0]; i > ans[0] - 10; i--) {
		printf("%d", ans[i]);
	}
	printf("\n");
	return 0;
}

大数据相乘

输入两个 100 位以内的正整数,输出它们的乘积。
核心点在于:c[i + j] += a[i] * b[j]

#include<iostream>

#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cstring>
using namespace std;
#define max 100
char str1[max + 5], str2[max + 5];
int a[max + 5], b[max + 5], c[2 * max + 5];

int main() {
	memset(c, 0, sizeof(c));
	cin >> str1 >> str2;
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	for (int i = 0; i < len1; i++) 
		a[i] = str1[len1 - i - 1] - '0';
	for (int i = 0; i < len2; i++) 
		b[i] = str2[len2 - i - 1] - '0';
	for (int i = 0; i < len1; i++) {
		for (int j = 0; j < len2; j++) {
			//核心
			c[i + j] += a[i] * b[j];
			if (c[i + j] < 10) continue;
			c[i + j + 1] += c[i + j] / 10;
			c[i + j] %= 10;
		}
	}
	int len3 = len1 + len2;
	while (!c[len3]) len3--;
	for (int i = len3; i >= 0; i--)
		cout << c[i];
	cout << endl;

}

两个100位以内的数字相加

#include<iostream>

#include<vector>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<iomanip>
using namespace std;
#define max 100
char str1[max + 5], str2[max + 5];
int a[max + 5], b[max + 5], c[max + 5] = {0};

int main() {
	cin >> str1 >> str2;
	int len1 = strlen(str1), len2 = strlen(str2);
	for (int i = 0; i < len1; i++)
		a[i] = str1[len1 - 1 - i] - '0';
	for (int i = 0; i < len2; i++)
		b[i] = str2[len2 - 1 - i] - '0';
	int len3 = (len1 > len2 ? len1 : len2);
	for (int i = 0; i < len3; i++) {
		c[i] += a[i] + b[i];
		if (c[i] < 10) continue;
		len3 += (i + 1 == len3);
		c[i + 1] += c[i] / 10;
		c[i] %= 10;
	}
	cout << len3 << endl;
	for (int i = len3 - 1; i >= 0; i--)
		cout << c[i];
}

a^b

输入a, b均为int类型整数, 输出a ^ b为100位以内整数

#include<iostream>

#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
#define max 100
int ans[max + 5] = {0};
int main() {
	int a, b;
	cin >> a >> b;
	ans[0] = 1;
	int len = floor(log10(a)) + 1;
	for (int i = 0; i < b; i++) {
		for (int j = 0; j < len; j++)
			ans[j] *= a;
		for (int j = 0; j < len; j++) {
			if (ans[j] < 10) continue;
			ans[j + 1] += ans[j] / 10;
			ans[j] %= 10;
			len += (j + 1 == len);
		}
	}
	for (int i = len - 1; i >= 0; i--) 
		cout << ans[i];
	
}

计算大整数

定义运算 f(x) 为x!中各位非零值的相乘结果,例如 5!=120 则f(5)=1×2=2

#include<iostream>

#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<iomanip>
#include<stdio.h>
using namespace std;
#define max 10000


int main() {
	int x;
	while (scanf("%d", &x) != EOF) {
		int ans[max + 5] = {0}, sum[max + 5] = {0};
		ans[0] = 1;
		int len = floor(log10(x)) + 1;
		int a = x;
		for (int j = 0; j < x; j++) {			
			for (int i = 0; i < len; i++){
				ans[i] *= a;
			}
			for (int i = 0; i < len; i++) {
				if (ans[i] < 10) continue;
				ans[i + 1] += ans[i] / 10;
				ans[i] %= 10;
				len += (i + 1 == len);
			}
			a--;
		}
		sum[0] = 1;
		int len2 = 1;
		for (int i = 0; i < len; i++) {
			if (ans[i] == 0) continue;
			for (int j = 0; j < len2; j++) {
				sum[j] *= ans[i];
			}
			for (int j = 0; j < len2; j++) {
				if (sum[j] < 10) continue;
				sum[j + 1] += sum[j] / 10;
				sum[j] %= 10;
				len2 += (j == len2 - 1);
			}
		}

		for (int i = len2 - 1; i >= 0; i--)
			cout << sum[i];
		cout << endl;
	}
}

欧拉第20题

阶乘数字和
n! 的意思是 n × (n − 1) × … × 3 × 2 × 1

例如,10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,所以10!的各位数字和是 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27。

求出100!的各位数字和。

#include<iostream>

#include<vector>
#include<math.h>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
#define max 10000
int ans[max + 5] = {0}, sum[max + 5];

int main (){
	ans[1] = 1;
	sum[1] = 1;
	int len = 1;
	int s = 0;
	for (int j = 1; j <= 100; j++) {
		for (int k = 1; k <= len; k++) {
			ans[k] *= j;
		}
		for (int k = 1; k <= len; k++) {
			if (ans[k] < 10) continue;
			ans[k + 1] += ans[k] / 10;
			ans[k] %= 10;
			len += (k == len);
		}
	}
	for (int i = len; i >= 1; i--) {
		cout << ans[i];
		s += ans[i];
	}
	cout << endl;
	cout << s << endl;
}

发布了9 篇原创文章 · 获赞 9 · 访问量 6565

猜你喜欢

转载自blog.csdn.net/qq_43413369/article/details/103666047
今日推荐