菜鸟学便成 : poj 1001 Exponentiation

来源:互联网 发布:淘宝网限制发布 编辑:程序博客网 时间:2024/06/08 03:34
ExponentiationTime Limit: 500MSMemory Limit: 10000KTotal Submissions: 124897Accepted: 30478DescriptionProblems 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.InputThe 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.OutputThe 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 Input95.123 120.4321 205.1234 156.7592  998.999 101.0100 12Sample Output548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.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 250int 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好打击积极性。

原创粉丝点击