1. 程式人生 > >測試題-POJ-1001

測試題-POJ-1001

1. 題目大意

考察大數運算,有些時候由於編譯器所能接受單個變數大小有限,需要單獨編寫程式完成運算。 該題是完成Rb的指數運算,其中0<=R<=99.999 , 0<b<=25。

2. AC難點

輸出格式:純小數不需要輸出小數點前的零;不能省略小數點後的必須零;表示精度的多餘零必須省略。

3. 大體思路

1.將數字按位整型儲存; 2.多位乘法的實質是錯位乘加; 3.為了防止溢位,每次運算後再次將所有位數化為整形(進位) 4.完善的輸出函式(防止輸出錯誤)

4. 程式碼

#include <iostream>

using namespace std;

int res[6];

class zhishu
{
public:
	int arr[150];	//小數點前50位,小數點後75位
	int high, low;

	void init()
	{
		high = 48;
		low = 53;
		for (int i = 0; i < 150; i++)
			arr[i] = 0;
	}

	void set(int *nums)
	{
		for (int i = 1; i < 6; i++)
			arr[48 + i - nums[0]] = nums[i];
		format();
	}

	void format()
	{
		int i, co = 0;
		for (i = low + 4; i > high - 3; i--)
		{
			arr[i] = arr[i] + co;
			co = arr[i] / 10;
			arr[i] = arr[i] % 10;
		}
		i = 0;

		while (arr[i] == 0 && i < 150)
		{
			i++;
		}
		high = i;
		i = 149;
		while (arr[i] == 0 && i >= 0)
		{
			i--;
		}
		low = i;
	}

	void multi(int *nums)
	{
		int i, temp[150] = { 0 };
		for (i = high; i < low + 1; i++)
			for (int j = 1; j < 6; j++)
				temp[i - nums[0] + j - 1] = temp[i - nums[0] + j - 1] + nums[j] * arr[i];
		for (i = high - nums[0]; i < low - nums[0] + 5; i++)
			arr[i] = temp[i];
		format();
	}

	void print_exp()
	{
		int i;
		if (high > low)
			cout << 0;
		else if (high > 49)
		{
			printf_s(".");
			for (i = 50; i < low + 1; i++)
			{
				cout << arr[i];
			}
		}
		else if (low < 50)
		{
			for (i = high; i < 50; i++)
				cout << arr[i];
		}
		else
		{
			for (i = high; i < 50; i++)
				cout << arr[i];
			printf_s(".");
			for (i = 50; i < low + 1; i++)
				cout << arr[i];
		}
		cout << '\n';
	}
};

void trans(char *input)
{
	int i, j = 1;
	res[0] = 5;
	for (i = 0; i < 6; i++)
	{
		if (input[i] == '.')
			res[0] = i - 1;
		else
		{
			res[j] = input[i] - 48;
			j++;
		}
	}
}

int main()
{
	int expo;
	zhishu result;
	char input[7];
	int i;
	while (cin >> input >> expo)
	{
		result.init();
		trans(input);
		result.set(res);
		for (i = 0; i < expo - 1; i++)
		{
			result.multi(res);
		}
		result.print_exp();
	}
}

5. 測試輸入集

95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 .00001 1 .12345 1 0001.1 1 1.1000 1 10.000 1 000.10 1 000000 1 000.00 1 .00000 0 000010 1 000.10 1 0000.1 1 00.111 1

0.0001 1 0.0001 3 0.0010 1 0.0010 3 0.0100 1 0.0100 3 0.1000 1 0.1000 3 1.0000 1 1.0000 3 1.0001 1 1.0001 3 1.0010 1 1.0010 3 1.0100 1 1.0100 3 1.1000 1 1.1000 3 10.000 1 10.000 3 10.001 1 10.001 3 10.010 1 10.010 3 10.100 1 10.100 3 99.000 1 99.000 3 99.001 1 99.001 3 99.010 1 99.010 3 99.100 1 99.100 3 99.998 1 99.998 3