现在的位置: 首页 > 综合 > 正文

菜鸟学便成 : poj 1001 Exponentiation

2018年03月20日 ⁄ 综合 ⁄ 共 2875字 ⁄ 字号 评论关闭
Exponentiation
Time Limit: 500MS		Memory Limit: 10000K
Total Submissions: 124897		Accepted: 30478
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

题目的大意就是:求R的n次方其中0.0<R<99.999,n为0<n<=25的整数。输入格式中第1到6列为R,第8到9列为n。输出格式中需要注意:前面的零不能打印(如sample2);后面的零不要打印(如0.00100不能打印);如果结果为整数,不要打印小数点。


#include <stdio.h>
#include <string.h>

#define LENGTH 6
#define NEWLENGTH 5
#define  MAX 250

int tmpResult[MAX];
int result[MAX];
int tData[MAX];

void solve1001(char s[],int n)
{
	//保留出小数点以外的数据
	int data[NEWLENGTH];
	
	//统计数据中小数点后面有多少位
	int zeroNum = 0;	

	memset(tmpResult,-1,sizeof(tmpResult));	
	memset(result,0,sizeof(result));
	
	int i,j;
	for (i=0,j=0;i<LENGTH;i++)
	{
		if (s[i]=='.')
		{
			//得到小数点后面位数
			zeroNum = LENGTH-i-1;
			continue;
		}
		//复制数据
		data[j] = s[i] - '0';
		//将数据放到数组末尾
		tmpResult[MAX-LENGTH+1+j] = s[i] - '0';
		j++;
	}
	

	for (i=1;i<n;i++)
	{
		int resultCount = 0;
		memset(result,0,sizeof(result));
		for (j=NEWLENGTH-1;j>=0;j--)
		{
			int k = MAX - 1;
			int addAC = 0;
			
			memset(tData,-1,sizeof(tData));
			
			
			while (tmpResult[k]!=-1)
			{	
				int t = data[j] * tmpResult[k] + addAC;
				addAC = t/10;
				t %= 10;
				tData[k] = t;
				k--;			
			}
			if (addAC)
			{
				tData[k] = addAC;
			}
			
			
			k = MAX - 1;
			int multiAC = 0;
			
			
			while (tData[k]!=-1)
			{
				int t = tData[k] + result[k-resultCount] + multiAC;
				multiAC = t/10;
				t %= 10;
				result[k-resultCount] = t;
				k--;
				
			}
			if (multiAC)
			{
				result[k-resultCount] = multiAC;
			}
					
			resultCount++;						

		}

		int m;
		for (m=0;result[m]<=0||result[m]>9;m++)
		{
			tmpResult[m] = -1;
		}
		for (;m<MAX;m++)
		{
			tmpResult[m] = result[m];
			//printf("%d",result[m]);//打印中间结果
		}

	}


	int finalZeroNum = MAX - zeroNum * n;
	int start=0,end=MAX;
	int m;
	//打印最终结果
	for (m=0;result[m]==0&&m<finalZeroNum;m++)
	{		
	}
	start = m;
	for (m=MAX-1;result[m]==0&&m>=finalZeroNum;m--)
	{
	}
	end = m + 1;


	//printf("%d %d %d\n",start,end,finalZeroNum);

	for(m=start;m<end;m++)
	{
		if(m==finalZeroNum)
		{
			printf(".");
		}
		printf("%d",result[m]);
	}
	printf("\n");
			
				
}


int main()
{
	//将标准输入重定向,从out.txt中读取输入数据用于测试,实际online judge时需要注释掉
	freopen( "D:\\out.txt", "r", stdin);
	char s[LENGTH];
	int n;
	
	while (scanf("%s%d",s,&n)==2)
	{
		solve1001(s,n);
	}
	//solve1001("95.123",12);
	return 0;
}

结果AC不了,貌似是一些特殊情况没考虑,真心无语,不过算法应该是正确的,sample都可以通过,求大神指教.真心感觉Online judge好打击积极性。

抱歉!评论已关闭.