大整数类java和c++实现

转载:https://www.cnblogs.com/Leonard-/p/7636639.html和https://blog.csdn.net/dvt777/article/details/48897225

在处理大整数类的时候使用java比c++合适,所以我们有时需要看题来选择编译方式,当然也可以使用c++进行编写

一、JAVA实现

大数相加:

复制代码
 1 //JAVA 大数相加 
 2 import java.math.BigInteger;
 3 import java.util.*;
 4 import java.io.*;
 5 
 6 public class Main
 7 {
 8     public static void main(String args[])
 9     {
10         Scanner cin = new Scanner(System.in);
11             BigInteger a = cin.nextBigInteger();
12             BigInteger b = cin.nextBigInteger();
13             BigInteger ans = a.add(b);
14             System.out.println(ans);
15     }
16 }
复制代码

 

大数相乘:

复制代码
 1 //JAVA 大数相乘 
 2 import java.math.BigInteger;
 3 import java.util.*;
 4 import java.io.*;
 5 
 6 public class Main
 7 {
 8     public static void main(String args[])
 9     {
10         Scanner cin = new Scanner(System.in);
11             BigInteger a = cin.nextBigInteger();
12             BigInteger b = cin.nextBigInteger();
13             BigInteger ans = a.multiply(b);
14             System.out.println(ans);
15     }
16 }
复制代码

 

大数相减:public BigInteger subtract(BigInteger val)

大数相除:public BigInteger divide(BigInteger val)

 

大数阶乘:

复制代码
 1 //JAVA 大数阶乘 
 2 import java.io.*;
 3 import java.math.BigInteger;
 4 import java.util.*;
 5 
 6 public class Main
 7 {
 8     public static void main(String args[])
 9     {
10         Scanner cin = new Scanner(System.in);    
11         int n = cin.nextInt(); //输入int型 
12         BigInteger ans = BigInteger.ONE;
13         for(int i = 1; i <= n; i++)
14         ans = ans.multiply(BigInteger.valueOf(i));
15         System.out.println(ans);
16     }
17 }
复制代码

 

大数比较大小

复制代码
 1 //JAVA 大数比较大小 
 2 import java.io.*;
 3 import java.math.BigInteger;
 4 import java.util.*;
 5 
 6 public class Main
 7 {
 8     public static void main(String args[])
 9     {
10         Scanner cin = new Scanner(System.in);    
11         while(cin.hasNext())
12         {
13             BigInteger a = cin.nextBigInteger();
14             BigInteger b = cin.nextBigInteger();
15             if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break;
16             int flag = a.compareTo(b);
17             if(flag == -1)
18                 System.out.println("a<b");
19             else if(flag == 0)
20                 System.out.println("a==b");
21             else
22                 System.out.println("a>b");
23         }
24     }
25 }
复制代码

 

高精度幂

复制代码
 1 //JAVA 高精度幂 
 2 import java.io.*;
 3 import java.math.BigDecimal;
 4 import java.util.*;
 5 
 6 public class Main
 7 {
 8     public static void main(String args[])
 9     {
10         Scanner cin = new Scanner(System.in);    
11         while(cin.hasNext())
12         {
13             BigDecimal ans = cin.nextBigDecimal();
14             int n = cin.nextInt();
15             String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉后面的0和小数点 
16             if(res.startsWith("0")) //去掉前导0 
17             {
18                 res = res.substring(1);
19             }
20             System.out.println(res);
21         }
22     }
23 }
复制代码


二、c++实现

用vector实现

#include<iostream>
#include<vector>
#include<stack>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
class BigInteger  
{
public:
	static const int base=100000000;
	static const int width=8;
	vector<int>s;
	BigInteger(long long num=0){*this=num;}    //构造函数
	BigInteger operator=(long long num)
	{
		s.clear();
		do{
			s.push_back(num%base);
			num=num/base;
		}while(num>0);
		return *this;
	}
	BigInteger operator=(const string &str)      //重载=号
	{
		s.clear();
		int x,len=(str.length()-1)/width+1;
		for(int i=0;i<len;i++){
			int end=str.length()-i*width;
			int start=max(0,end-width);
			sscanf(str.substr(start,end-start).c_str(),"%d",&x);    //格式符%d是读入十进制整数
			                                                    //string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址
			s.push_back(x);
		}
		return *this;
	}
	friend ostream & operator<<(ostream &out,const BigInteger& x)   //重载输出号
	{
	out<<x.s.back();
	for(int i=x.s.size()-2;i>=0;i--){
		char buf[20];
		sprintf(buf,"%08d",x.s[i]);
		for(int j=0;j<int(strlen(buf));j++)
			out<<buf[j];
	}
	return out;
	}
	friend istream & operator>>(istream &in,BigInteger& x)   //重载输入号
	{
	string s;
	if(!(in>>s)) return in;
	x=s;
	return in;
	}
	BigInteger operator+(const BigInteger& b)const   //重载加号
	{
		BigInteger c;
		c.s.clear();
		for(int i=0,g=0;;i++){
			if(g==0&&i>=s.size()&&i>=b.s.size()) break;
			int x=g;
			if(i<s.size()) x+=s[i];
			if(i<b.s.size()) x+=b.s[i];
			c.s.push_back(x%base);
			g=x/base;
		}
		return c;
	}
	BigInteger operator-(const BigInteger& b)   //重载减号,默认前面大于后面
	{
		BigInteger c;
		c.s.clear();
		if(*this>b){
			int i,g;
		for(i=0,g=0;;i++){
			if(g==0&&i>=b.s.size()) break;
			int x=g;
			if(s[i]<b.s[i]){
				s[i+1]-=1;
				s[i]=s[i]+base;
			}
			if(i<s.size()) x+=s[i];
			if(i<b.s.size()) x-=b.s[i];
			c.s.push_back(x%base);
			g=x/base;
		}
		int x=0;
		for(;i<s.size();i++){
			x+=s[i];
			c.s.push_back(x%base);
			x=x/base;
		}
		}
		return c;
	}
	bool operator<(const BigInteger& b)const   //重载小于号
	{
		if(s.size()!=b.s.size()) return s.size()<b.s.size();
		for(int i=s.size()-1;i>=0;i--)
			if(s[i]!=b.s[i]) return s[i]<b.s[i];
		return false;
	}
	bool operator>(const BigInteger& b)const   //重载大于号
	{
		return b<*this;
	}
	bool operator<=(const BigInteger& b)const
	{
		return !(b<*this);
	}
	bool operator>=(const BigInteger& b)const
	{
		return !(*this<b);
	}
	bool operator==(const BigInteger& b)const  //重载等于号
	{
		return !(b<*this)&&!(*this<b);
	}
	BigInteger operator+=(const BigInteger& b)
	{
		*this=(*this+b);
		return *this;
	}
	BigInteger operator-=(const BigInteger& b)
	{
		*this=(*this-b);
		return *this;
	}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值