加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

OJ笔记:大数运算

发布时间:2020-12-14 03:52:03 所属栏目:大数据 来源:网络整理
导读:题目:http://ac.jobdu.com/problem.php?pid=1208 #include stdio.h#include cstringint a[1005];int b[4000];int c[1005];char s[1500];void add(int x){if (1 == x) {for (int i = 0; i 1000; i++) {a[i] = a[i] + c[i];a[i+1] += (a[i] / 10);a[i] = a[i]

题目:http://ac.jobdu.com/problem.php?pid=1208

#include <stdio.h>
#include <cstring>

int a[1005];
int b[4000];
int c[1005];
char s[1500];

void add(int x)
{
	if (1 == x) {
		for (int i = 0; i < 1000; i++) {
			a[i] = a[i] + c[i];
			a[i+1] += (a[i] / 10);
			a[i] = a[i] % 10;
		}
	}
}

/* c 乘2 */
void bigPower()
{
	for (int i = 0; i < 1000; i++) {
		c[i] = c[i] + c[i];
	}
	for (int i = 0; i < 1000; i++) {
		c[i+1] += (c[i] / 10);
		c[i] = c[i] % 10;
	}
}

int main()
{
	while (scanf("%s",s) != EOF) {
		memset(a,sizeof(a));
		memset(b,sizeof(b));
		memset(c,sizeof(c));
		int len = strlen(s);
		for (int i = len - 1,j = 0; i >= 0; i--,j++) {
			a[j] = s[i] - '0';
		}
		int k = 0;
		while (len > 0) {
			for (int i = len - 1; i >= 1; i--) {
				int t = a[i] / 2;
				a[i-1] += (a[i] % 2) * 10;
				a[i] = t;
			}
			b[k] = a[0] % 2;
			++k;
			a[0] = a[0] / 2;
			if (a[len-1] == 0) {
				--len;
			}
		}
		c[0] = 1;
		for (int i = k-1; i >= 0; i--) {
			add(b[i]);
			bigPower();
		}
		int i;
		for (i = 1000; a[i] == 0; i--) {
		}
		if (i < 0) {
			printf("0");
		} else {
			for (; i >= 0; i--) {
				printf("%d",a[i]);
			}
		}
		printf("n");
	}

	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读