Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请注意以下数据,代码附上

Posted by 2007011268 at 2012-07-11 21:57:07 on Problem 1001
1. 10.000 2
2. 10000. 2
3. 0.0000 2
4. 100000 2
我在做的时候,忽略了第一种情况,结果总是wa。代码奉上,如下:

#include <iostream>
#include <string>
#include <sstream>
#define Max_Digit_Number 150
#define Max_Arr_Number 20
#define Len_Every 8       //The length of each small integer in each position of the ivec;
#define Modular_Number 100000000          //The int number stored in every array contains 8 digits
typedef unsigned int uint;
using namespace std;

unsigned int string_to_uint(const string& s)
{
	stringstream ss(s);
	unsigned int value;
	ss >> value;
	return value;
}
string uint_to_string(const uint x)
{
	stringstream ss;
	string s;
	ss << x;
	ss >> s;
	return s;
}
class BigInt      //BigInt for positive integers
{
	friend BigInt multiply(const BigInt&, const BigInt&);
	friend BigInt power(const BigInt&, int);

	private:
		string number;       
		uint int_arr[Max_Arr_Number];
		uint arr_size;
	public:
		BigInt(string n="");
		BigInt(const BigInt&);
		BigInt& operator=(const BigInt&);
		string get_number() { return number; }
		void print()
		{
//			for(int i=arr_size-1; i>=0; i--)
//				cout << int_arr[i];
//			cout << endl;
			cout << number << endl;
		}
};
BigInt power(const BigInt& num, int n)
{
	if(n == 0)
		return BigInt("1");
	else if(num.number == "0")
		return num;
	else if(n == 1)
		return num;
	else if(n % 2 == 0)
	{
		int k = n/2;
		BigInt tmp = power(num, k);
		return multiply(tmp, tmp);
	}
	else
	{
		BigInt tmp = power(num, n-1);
		return multiply(tmp, num);
	}
}
BigInt& BigInt::operator=(const BigInt& b)
{
	number = b.number;
	arr_size = b.arr_size;
	for(size_t i=0; i<arr_size; i++)
		int_arr[i] = b.int_arr[i];
	return *this;
}
BigInt::BigInt(const BigInt& b)
{
	number = b.number;
	arr_size = b.arr_size;
	for(size_t i=0; i<arr_size; i++)
		int_arr[i] = b.int_arr[i];
}
BigInt::BigInt(string n):number(n)
{
	if(number.empty())
	{
		arr_size = 1;
		int_arr[arr_size-1] = 0;
	}
	else
	{
		uint num = string_to_uint(number);
		arr_size = 1;
		int_arr[arr_size-1] = num;
	}
}
BigInt multiply(const BigInt& num1, const BigInt& num2)
{
	BigInt res_int;
	for(size_t i=0; i<Max_Arr_Number; i++)
		res_int.int_arr[i] = 0;
	for(size_t i=0; i<num1.arr_size; i++)
		for(size_t j=0; j<num2.arr_size; j++)
		{
			size_t k = i+j;
			long long int tmp = static_cast<long long int>(num1.int_arr[i]) * static_cast<long long int>(num2.int_arr[j]);
			long long int tmp2;
			while(true)
			{
				if(tmp == 0)
					break;
				tmp += static_cast<long long int>(res_int.int_arr[k]);
				tmp2 = tmp % static_cast<long long int>(Modular_Number);
				tmp = tmp / static_cast<long long int>(Modular_Number);
				res_int.int_arr[k] = tmp2;
				k ++;
			}
		}

	int i = num1.arr_size + num2.arr_size - 1;
	while( i >= 0 && res_int.int_arr[i] == 0)
		i--;
	res_int.arr_size = i+1;
	string s ="";
	for(int i=res_int.arr_size-1; i>=0; i--)
	{
		string tmp = uint_to_string(res_int.int_arr[i]);
		if(tmp.size() < Len_Every)
		{
			if(i < res_int.arr_size - 1)
				tmp = tmp.insert(0, Len_Every - tmp.size(), '0');
		}
		s += tmp;
	}
	res_int.number = s;
	return res_int;
}
int main()
{
	string s;
	int n;
	while(cin >> s >> n)
	{

		size_t after_len = 0;
		size_t index = 0;
		while(index < s.size() && s[index] != '.')
			index ++;

//		s.erase(index, 1);
//		BigInt b1(s);
//		BigInt c1 = power(b1, n);
//		c1.print();
//		continue;


		if(index < s.size())
		{
			after_len = s.size() - index - 1;	
			after_len = n * after_len;
		}
		s.erase(index, 1);
		size_t start = 0;
		while(start < s.size() && s[start] == '0')
			start ++;
		if( start == s.size())
			s = "0";
		else
			s = s.substr(start);
		BigInt b(s);
		BigInt c = power(b, n);
		string res = c.get_number();
		if(res.size() < after_len)
		{
			int dif = after_len - res.size();
			res.insert(0, dif, '0');
			res.insert(0, 1, '.');
		}
		else  if(after_len > 0)
		{
			int index = res.size() - after_len;
			res.insert(index, 1, '.');
		}

		if(after_len > 0)
		{
			int i=res.size() - 1;
			while(i>=0 && res[i] == '0')
				i --;
			if(res[i] == '.')
				i --;
			res.erase(i+1, res.size()-i-1);
			if(res.empty())
				res = "0";
		}
		cout << res << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator