压位高精模板(c++)

zZ的blog http://blog.csdn.net/ssssssay/article/details/52102173

/*
 ~~~~~~~~~~~~~~wine~~~~~~~~~~~~~~~
 压位高精模板ver 1.1,不滋瓷负数
 欢迎指出bug
 蒟蒻不会fft和牛顿迭代,效率不高
 2017-4-10
 eg :
	BUGNUM a = 233,b(0),t; LL x,p;
	t.init(); t.out();
	
	t = t+1000; t = a+b; t = a-b; t = a/b; t = a%b;
	t = a.sqrt(); t = q.pow(x); t = q.pow_mod(x,p);
	bool cmp = a>b;  //......
	
	p = a._mod(x); p = a._div(x); p = a._add(x);p = a._mul(11);
	p = a._add(-23); //减成负数就zZ了
	
	a.div(233); a.add(23); a.mul(2333)
	 即 a = a/233; a = a+233; a = a*2333;
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
#define ll "lld"

typedef long long LL;

const int BASE = 100000000,MAXLEN = 10330;          //高精度 压8位
char s[MAXLEN<<3];

struct BIGNUM {
   
	int len; LL bt[MAXLEN];

	void upzero(){
   while(!bt[len] && len!=1)--len;} //去前导0
	void upbit() {
                                    //进位
		LL x(0);
		for (int i = 1; i <= len; i++) {
   
			if(bt[i] < 0) {
   bt[i] += BASE; --bt[i+1];}
			bt[i] += x; x = bt[i]/BASE; bt[i] %= BASE;
		}
		while(x) bt[++len] = x % BASE,x /= BASE;
	}
	LL to_long(){
   return len<3?bt[2]*BASE+bt[1]:-1;}
	BIGNUM(LL num):len(1) {
   memset(bt,0,sizeof bt);bt[1]=num;upbit();}  //构造函数
    BIGNUM():len(1){
   memset(bt,0,sizeof bt);}
	BIGNUM operator = (LL x) {
   len = 1;memset(bt,0,sizeof<
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
高精度数的计算器一般使用字符串表示数字,然后模拟手工计算的过程进行计算。下面是一个简单的高精度数的加法算法代码示例(使用C语言实现): ```c #include <stdio.h> #include <string.h> #define MAXN 1005 int a[MAXN], b[MAXN], c[MAXN]; void reverse(char s[]) { int len = strlen(s); for (int i = 0; i < len / 2; i++) { char temp = s[i]; s[i] = s[len - 1 - i]; s[len - 1 - i] = temp; } } void str_to_int(char s[], int num[]) { reverse(s); int len = strlen(s); for (int i = 0; i < len; i++) { num[i] = s[i] - '0'; } } void int_to_str(int num[], char s[]) { int len = 0; for (int i = MAXN - 1; i >= 0; i--) { if (num[i] != 0) { len = i + 1; break; } } for (int i = 0; i < len; i++) { s[i] = num[len - 1 - i] + '0'; } s[len] = '\0'; reverse(s); } void add(int a[], int b[], int c[]) { int carry = 0; for (int i = 0; i < MAXN; i++) { c[i] = a[i] + b[i] + carry; carry = c[i] / 10; c[i] %= 10; } } int main() { char sa[MAXN], sb[MAXN]; scanf("%s%s", sa, sb); str_to_int(sa, a); str_to_int(sb, b); add(a, b, c); char sc[MAXN]; int_to_str(c, sc); printf("%s\n", sc); return 0; } ``` 这个算法实现了两个字符串表示的数字的加法操作,其中 `MAXN` 是表示数字的最大位数,本示例中设置为1005,可以根据需要适当调整。 这个算法的实现过程比较简单,首先将字符串表示的数字转换成整型数组表示的数字,然后进行加法操作,最后将得到的结果转换回字符串表示的数字。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值