【高精度】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不能通过!)
废话少说,贴代码:
我第一次提交的时候,忘记了每行最后有一个回车,结果显示“Presentation Error”,加上cout<<endl;就好了。
从去年大四初,保送研究生之后,就没有任何的就业压力了。而现在,作为研一的新生,却听到师兄们找工作诸多不顺的消息。自己就在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;就好了。
相关文章推荐
- 【高精度】c++ stl解决poj1001问题
- Dev_c++下解决编译winsock和pthread 的链接问题C++&STL
- C++优先队列解决哈夫曼(Huffmam)编码问题 (STL priority_queue)
- 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题
- C++抽象编程——STL(3)——解决排队问题的代码及其分析
- C++实现动态规划算法之解决0-1背包问题
- C++报错及解决问题(持续跟新)
- 解决ActiveMQ中,Java与C++交互中文乱码问题
- c++ 数据结构 用递归方法解决迷宫问题
- 解决C++项目编译时的multiple definition of重定义问题
- C++之stl::string写时拷贝导致的问题
- 解决NDK的STL版本不支持exception的问题
- paip.c++ qt __gxx_personality_sj0 __gxx_personality_v0问题的解决
- 在用到iframe的前端页面调用c++的或者android方法时提示找不到方法问题解决
- 用C/C++解决一些数学问题(二)
- c++解决实际问题
- 解决linux eclipse c++找不到符号或者~~~name a type的问题 && c++11
- Sublimetext2/3中java、c/c++的输入问题的解决
- C++ - 函数模板(function template)右值引用参数 问题 及 解决
- 解决C++编译出现的重定义问题:multiple definition of ’XXX‘错误