`
lethorld
  • 浏览: 6739 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Exponentiation 大实数乘法

 
阅读更多
Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
Hint

If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer

C++
while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */
{
...
}
Source

East Central North America 1988

#include <stdio.h>
#include <stdlib.h>

char* left; // 左操作数 
char* right; // 右操作数 
int ll; // 左操作数长度 
int rl; // 有操作数长度
int ldp; // 做操作数小数位数 
int rdp; // 右操作数小数位数 
char* result; // 保存结果的全局变量 
int length; // 数据的长度
int dp; // 小数位数 
 
void deal(char* s, int n);
void print();
void calculate();
void calculate0(int i, int j);
void update(int remain, int index);

int main(int argc, char *argv[])
{
    int n;
    char* s = (char*)malloc(7);
    while (scanf("%s%d", s, &n) == 2)
		deal(s, n);

	system("PAUSE");
	return 0;   
}

void deal(char* s, int n)
{
	int sp=0, i, j, k;
	char num[6];;

	// 计算小数位数
	for (j=5; j>=0; j--)
		if (s[j] == '.')
			break;
		else 
			sp++;

	// 将实数转化成整数,保存在num字符数组中
	for (i=0; i<5; i++) 
	{ 
		num[i] = 5-sp-i>0 ? s[i]:s[i+1];
		num[i] = num[i] - '0';
	}
	num[5] = '\0';

	// 装在左操作符
	free(left);
	left = (char*)malloc(6);
	for (i=0; i<6; i++)
		left[i] = num[i];
	ll = 5;
	ldp = sp;

	// 装在右操作符
	free(right);
	right = (char*)malloc(6);
	for (i=0; i<6; i++)
		right[i] = num[i];
	rl = 5;
	rdp = sp;	

	// 装在初始结果
	free(result);
	result = (char*)malloc(6);
	for (i=0; i<6; i++)
		result[i] = num[i];
	length = 5;
	dp = sp;

	for (k=0; k<n-1; k++) 
	{
		calculate(); // 计算表达式
		// printf("The %d time:\n", k);
		// print(); // 打印当前结果
		// 重新装载操作符
		free(left);
		left = (char*)malloc(length + 1);
		for (i=0; i<length+1; i++)
			left[i] = result[i];
		ll = length;
		ldp = dp;
	}
	print();
}

void calculate()
{
	free(result);
	result = (char*)malloc(ll + rl + 1);    
	int i, j;
    for (i=0; i<ll+rl; i++) 
		result[i] = 0;
	result[ll+rl] = '\0';

    length = 0;
    dp = ldp + rdp;
     
    for (i=0; i<ll; i++)
        for (j=0; j<rl; j++)
            calculate0(i, j);
     
    result[++length] = '\0';
}

void calculate0(int i, int j)
{
     int x = ((int)left[i]) * ((int)right[j]);
     
     // 计算结果保存位置
     int temp = result[i+j+1] + x;
     length = i+j+1;
     result[length] = temp % 10;
     temp /= 10;
     update(temp, length - 1);
}

void update(int remain, int index)
{
     int temp = remain;
     while (index >= 0 && temp)
     {
          temp += result[index];
          result[index] = temp % 10;
          temp /= 10;
		  index--;
     }
}

void print()
{
	char* temp = (char*)malloc(length+1);
	int i;
	for (i=0; i<length+1; i++) 
		temp[i] = result[i] + '0';

	int maxL = (dp > length) ? dp : length;
	maxL++;
	char* show = (char*)malloc(maxL + 1);
	show[maxL] = '\0'; // 置字符串结束符
	for (i=maxL-1; i>=0; i--)
	{
		if (dp == maxL - i - 1)
		{
			show[i] = '.'; // 置小数点
		} else if (dp > maxL - i - 1)
		{
			show[i] = temp[length-maxL+i];
		} else {
			// 需要判断数组是否越界
			show[i] = length-maxL+i+1<0 ? '0' : temp[length-maxL+i+1];
		}
	}
			// 去除小数部分多余的尾数
	int tail = maxL;
	while (show[--tail] == '0');
	show[show[tail]=='.' ? tail : tail+1] = '\0'; // 置字符串结束符

	// 去除整数部分多余的尾数
	int begin = -1;
	while (show[++begin] == '0');
	printf("%s\n", show+begin);
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics