信息学奥赛一本通(C++版)第二部分 基础算法 第一章 高精度计算

第一章 高精度计算

模板在最后。

T1307 : 高精度乘法

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1308 : 高精除

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1309 : 回文数

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1168 : 大整数加法

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1169 : 大整数减法

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1170 : 计算2的N次方

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1171 : 大整数的因子

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1172 : 求10000以内n的阶乘

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1173 : 阶乘和

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1174 : 大整数乘法

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


T1175 : 除以13

时间限制: 1000 ms 内存限制: 65536 KB

【题目描述】

  

【输入】

  

【输出】

  

【输入样例】


【输出样例】


【答案&代码】


(未完待续)

#include
#include
#include
#include
#include
using std::string;
using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::endl;
using std::max;
#define MAX_LENGTH 2005
struct BIG_NUMBER{
	int len,s[MAX_LENGTH];
	bool sign;
	
	BIG_NUMBER(void);
	BIG_NUMBER(const char*);
	BIG_NUMBER(int);
	~BIG_NUMBER(void);
	void clean(void);
	string toStr(void)const;
	friend istream& operator>>(istream&,BIG_NUMBER&);
	friend ostream& operator<<(ostream&,BIG_NUMBER&);
	BIG_NUMBER operator=(const char*);
	BIG_NUMBER operator=(int);
	BIG_NUMBER operator=(const string);
	bool operator>(const BIG_NUMBER&)const;
	bool operator>=(const BIG_NUMBER&)const;
	bool operator<(const BIG_NUMBER&)const;
	bool operator<=(const BIG_NUMBER&)const;
	bool operator==(const BIG_NUMBER&)const;
	bool operator!=(const BIG_NUMBER&)const;
	BIG_NUMBER operator+(const BIG_NUMBER&)const;
	BIG_NUMBER operator++(void);
	BIG_NUMBER operator++(int);
	BIG_NUMBER operator+=(const BIG_NUMBER&);
	BIG_NUMBER operator-(const BIG_NUMBER &) const;
	BIG_NUMBER operator--(void);
	BIG_NUMBER operator--(int);
	BIG_NUMBER operator-=(const BIG_NUMBER&);
	BIG_NUMBER operator*(const BIG_NUMBER &)const;
	BIG_NUMBER operator*(const int num)const;
	BIG_NUMBER operator*=(const BIG_NUMBER&);
	BIG_NUMBER operator/(const BIG_NUMBER&)const;
	BIG_NUMBER operator/=(const BIG_NUMBER&);
	BIG_NUMBER operator%(const BIG_NUMBER&)const;
	BIG_NUMBER factorial(void)const;
	BIG_NUMBER sqrt(void)const;
	BIG_NUMBER pow(const BIG_NUMBER&)const;
};
BIG_NUMBER::BIG_NUMBER(void){
	memset(s, 0, sizeof(s));
	len = 1;
	sign = 1;
}
BIG_NUMBER::BIG_NUMBER(const char*num){
	*this = num;
}
BIG_NUMBER::BIG_NUMBER(int num){
	*this = num;
}
string BIG_NUMBER::toStr(void)const{
	string res;
	res = "";
	for (int i = 0; i < len; i++)
		res = (char)(s[i] + '0') + res;
	if (res == "")
		res = "0";
	if (!sign&&res != "0")
		res = "-" + res;
	return res;
}
istream &operator>>(istream &in,BIG_NUMBER&num){
	string str;
	in>>str;
	num=str;
	return in;
}
ostream &operator<<(ostream &out,BIG_NUMBER&num){
	out<<num.toStr();
	return out;
}
BIG_NUMBER BIG_NUMBER::operator=(const char*num){
	memset(s, 0, sizeof(s));
	char a[MAX_LENGTH] = "";
	if (num[0] != '-')
		strcpy(a, num);
	else
		for (int i = 1; i < strlen(num); i++)
			a[i - 1] = num[i];
	sign = !(num[0] == '-');
	len = strlen(a);
	for (int i = 0; i < strlen(a); i++)
		s[i] = a[len - i - 1] - 48;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator=(int num){
	char temp[MAX_LENGTH];
	sprintf(temp, "%d", num);
	*this = temp;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator=(const string num){
	const char *tmp;
	tmp = num.c_str();
	*this = tmp;
	return *this;
}
bool BIG_NUMBER::operator<(const BIG_NUMBER&num)const{
	if (sign^num.sign)
		return num.sign;
	if (len != num.len)
		return len < num.len;
	for (int i = len - 1; i >= 0; i--)
		if (s[i] != num.s[i])
			return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
	return !sign;
}
bool BIG_NUMBER::operator>(const BIG_NUMBER&num)const{
	return num < *this;
}
bool BIG_NUMBER::operator<=(const BIG_NUMBER&num)const{
	return !(*this>num);
}
bool BIG_NUMBER::operator>=(const BIG_NUMBER&num)const{
	return !(*this<num);
}
bool BIG_NUMBER::operator!=(const BIG_NUMBER&num)const{
	return *this > num || *this < num;
}
bool BIG_NUMBER::operator==(const BIG_NUMBER&num)const{
	return !(num != *this);
}
BIG_NUMBER BIG_NUMBER::operator+(const BIG_NUMBER &num)const{
	if (sign^num.sign) {
		BIG_NUMBER tmp = sign ? num : *this;
		tmp.sign = 1;
		return sign ? *this - tmp : num - tmp;
	}
	BIG_NUMBER result;
	result.len = 0;
	int temp = 0;
	for (int i = 0; temp || i < (max(len, num.len)); i++) {
		int t = s[i] + num.s[i] + temp;
		result.s[result.len++] = t % 10;
		temp = t / 10;
	}
	result.sign = sign;
	return result;
}
BIG_NUMBER BIG_NUMBER::operator++(void){
	*this = *this + 1;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator++(int){
	BIG_NUMBER old = *this;
	++(*this);
	return old;
}
BIG_NUMBER BIG_NUMBER::operator+=(const BIG_NUMBER &num){
	*this = *this + num;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator-(const BIG_NUMBER &num) const{
	BIG_NUMBER b=num,a=*this;
	if (!num.sign && !sign) {
		b.sign=1;
		a.sign=1;
		return b-a;
	}
	if (!b.sign) {
		b.sign=1;
		return a+b;
	}
	if (!a.sign) {
		a.sign=1;
		b=BIG_NUMBER(0)-(a+b);
		return b;
	}
	if (a<b) {
		BIG_NUMBER c=(b-a);
		c.sign=false;
		return c;
	}
	BIG_NUMBER result;
	result.len = 0;
	for (int i = 0, g = 0; i < a.len; i++) {
		int x = a.s[i] - g;
		if (i < b.len) x -= b.s[i];
		if (x >= 0) g = 0;
		else {
			g = 1;
			x += 10;
		}
		result.s[result.len++] = x;
	}
	result.clean();
	return result;
}
BIG_NUMBER BIG_NUMBER::operator*(const BIG_NUMBER &num)const{
	BIG_NUMBER result;
	result.len = len + num.len;
 
	for (int i = 0; i < len; i++)
		for (int j = 0; j < num.len; j++)
			result.s[i + j] += s[i] * num.s[j];
 
	for (int i = 0; i < result.len; i++) {
		result.s[i + 1] += result.s[i] / 10;
		result.s[i] %= 10;
	}
	result.clean();
	result.sign = !(sign^num.sign);
	return result;
}
BIG_NUMBER BIG_NUMBER::operator*(const int num)const{
	BIG_NUMBER x = num;
	BIG_NUMBER z = *this;
	return x*z;
}
BIG_NUMBER BIG_NUMBER::operator*=(const BIG_NUMBER&num){
	*this = *this * num;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator/(const BIG_NUMBER&num)const{
	BIG_NUMBER ans;
	ans.len = len - num.len + 1;
	if (ans.len < 0) {
		ans.len = 1;
		return ans;
	}
 
	BIG_NUMBER divisor = *this, divid = num;
	divisor.sign = divid.sign = 1;
	int k = ans.len - 1;
	int j = len - 1;
	while (k >= 0) {
		while (divisor.s[j] == 0) j--;
		if (k > j) k = j;
		char z[MAX_LENGTH];
		memset(z, 0, sizeof(z));
		for (int i = j; i >= k; i--)
			z[j - i] = divisor.s[i] + '0';
		BIG_NUMBER dividend = z;
		if (dividend < divid) {
			k--;
			continue;
		}
		int key = 0;
		while (divid*key <= dividend) key++;
		key--;
		ans.s[k] = key;
		BIG_NUMBER temp = divid*key;
		for (int i = 0; i < k; i++)
			temp = temp * 10;
		divisor = divisor - temp;
		k--;
	}
	ans.clean();
	ans.sign = !(sign^num.sign);
	return ans;
}
BIG_NUMBER BIG_NUMBER::operator/=(const BIG_NUMBER&num){
	*this = *this / num;
	return *this;
}
BIG_NUMBER BIG_NUMBER::operator%(const BIG_NUMBER& num)const{
	BIG_NUMBER a = *this, b = num;
	a.sign = b.sign = 1;
	BIG_NUMBER result, temp = a / b*b;
	result = a - temp;
	result.sign = sign;
	return result;
}
BIG_NUMBER BIG_NUMBER::pow(const BIG_NUMBER& num)const{
	BIG_NUMBER result = 1;
	for (BIG_NUMBER i = 0; i < num; i++)
		result = result*(*this);
	return result;
}
BIG_NUMBER BIG_NUMBER::factorial(void)const{
	BIG_NUMBER result = 1;
	for (BIG_NUMBER i = 1; i <= *this; i++)
		result *= i;
	return result;
}
void BIG_NUMBER::clean(void){
	if (len == 0) len++;
	while (len > 1 && s[len - 1] == '\0')
		len--;
}
BIG_NUMBER BIG_NUMBER::sqrt(void)const{
	if(*this<0)return -1;
	if(*this<=1)return *this;
	BIG_NUMBER l=0,r=*this,mid;
	while(r-l>1) {
		mid=(l+r)/2;
		if(mid*mid>*this)
			r=mid;
		else
			l=mid;
	}
	return l;
}
BIG_NUMBER::~BIG_NUMBER(void){
}

你可能感兴趣的:(基础算法)