您的位置:首页 > 编程语言 > C语言/C++

【高精度】c++ stl解决poj1001问题

2012-10-14 12:31 381 查看
前言:
从去年大四初,保送研究生之后,就没有任何的就业压力了。而现在,作为研一的新生,却听到师兄们找工作诸多不顺的消息。自己就在CSDN上搜了一些面试题来做做看。虽然,学过C/C++/JAVA,桌面开发MFC,手机android开发,matlab等等。也开发过一些小的项目,但是,看到这些面试题时,发现大量的考察算法和数据结构等知识。于是乎,我需要加强自己对数据结构的理解和算法的实现。拿POJ上面的题目练练手。
Poj1001[http://poj.org/problem?id=1001]
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,andthe 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
我用c++ stl解决了此问题。

编译器:G++(vc++6.0不能通过!)

废话少说,贴代码:

/*
*author:ZhengHaibo
*email:zhb931706659@126.com
*copyright:ZhengHaibo
*Nanjing University Of Posts and TeleCom.
*/
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
//calculate v1*v2
vector<int> Multi(vector<int> v1,vector<int> v2)
{
vector<int> vt;//save the result
for (int i=0;i<v1.size()+v2.size()+1;i++)
{
vt.push_back(0);//Init to 0
}
for (int i=0;i<v1.size();i++)
{
for (int j=0;j<v2.size();j++)
{
vt[i+j]+=v1[i]*v2[j];
vt[i+j+1]+=vt[i+j]/10;
vt[i+j]%=10;
}
}
return vt;
}
int main()
{
string str;
int n;
while(cin>>str>>n){
vector <int> numArray;
vector <int> resultArray;
int dotPos=0,IndexLen;
IndexLen=str.length()-1;
//delete 0 in the tail of str. 12.10 ->12.1
for (int i=str.length()-1;i>=0;i--)
{
if(str[i]!='0')
{
IndexLen=i;
break;
}
}
//transform string to vector<int>
for (int i=IndexLen;i>=0;i--)
{
if (str[i]!='.')
{
numArray.push_back(str[i]-'0');
}
else
dotPos=IndexLen-i;
}
//copy vector
for (int i=0;i<numArray.size();i++)
{
resultArray.push_back(numArray[i]);
}
//calculate numArray^n
for (int j=0;j<n-1;j++)
{
resultArray = Multi(resultArray,numArray);
int len=resultArray.size();
while (resultArray[len-1]==0)//remove 0 in the big bit
{
resultArray.pop_back();
len--;
}

}
//output the resultArray in prooply way
if(n*dotPos>=resultArray.size())//if the result is less than 1,the dot pos is out of resultArray
{
cout<<".";
for(int k=0;k<n*dotPos-resultArray.size();k++)
{
cout<<"0";
}
}
for (int k=resultArray.size()-1;k>=0;k--)
{
if (k==n*dotPos-1)//the dot pos is in resultArray
{
if (k!=0)
{
cout<<".";
}
}
cout<<resultArray[k];
}
cout<<endl;//if not,Presentation Error!
//by ZhengHaibo njupt
}
return 0;
}


我第一次提交的时候,忘记了每行最后有一个回车,结果显示“Presentation Error”,加上cout<<endl;就好了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息