POJ1001 且适合带小数点大数的幂(R^N)
来源:互联网 发布:淘宝企业店铺运营费用 编辑:程序博客网 时间:2024/04/28 20:03
/** \function calculator R^N * * \param * \param * \return * */#include <iostream>#include <vector>#include <string>using namespace std;//big number multiplication.//str1 is the first number.//str2 is the second number.//return str1*str2string BigNumberMulti(const string& str1,const string& str2){ string product_str; vector<int> product_number,product_number_reverse; //reset every member of vector product_number to 0. int i=0,limit=str1.size()+str2.size()-1; for(i=0;i!=limit;i++) { product_number.push_back(0); } //multiplication content and the product is stored in product_number. string::const_iterator iter_str1,iter_str2; vector<int>::iterator iter_int; vector<int>::difference_type bit_move=0;//multiplication move one bit by once for(iter_str2=str2.begin();iter_str2!=str2.end();iter_str2++) { for(iter_str1=str1.begin(),iter_int=product_number.begin()+bit_move;iter_str1!=str1.end();iter_str1++,iter_int++) { *iter_int=*iter_int+(*iter_str2-'0')*(*iter_str1-'0'); } bit_move++; } //transform ints to chars,product_number to product_str. int remainder_now=0,remainder_pre=0; vector<int>::reverse_iterator reverse_iter_int; for(reverse_iter_int=product_number.rbegin();reverse_iter_int!=product_number.rend();reverse_iter_int++) { remainder_now=(*reverse_iter_int+remainder_pre)%10; product_number_reverse.push_back(remainder_now); remainder_pre=(*reverse_iter_int+remainder_pre)/10; } while(remainder_pre!=0) { product_number_reverse.push_back(remainder_pre%10); remainder_pre=remainder_pre/10; } for(reverse_iter_int=product_number_reverse.rbegin();reverse_iter_int!=product_number_reverse.rend();reverse_iter_int++) { product_str.push_back('0'+*reverse_iter_int); } return product_str;}/** \round calculator R^n * * \param int n * \return string * */string power(const string& R_int,int n){ if(n==0) { string str0("1"); return str0; } else { string str1=power(R_int,n/2); if(n%2==0)return BigNumberMulti(str1,str1); else return BigNumberMulti(BigNumberMulti(str1,str1),R_int); }}/** \function transform R_double to R_int * * \param * \param * \return * */class Two_Var{public: string R_int; int point_steps;};Two_Var R_transform(const string& R_double){ Two_Var temp; string::const_iterator iter; int point_position,po_pre=0; for(iter=R_double.begin();iter!=R_double.end();iter++) { if(*iter=='.') break; po_pre++; } point_position=R_double.size()-(po_pre+1); if(point_position<1) point_position=0; temp.point_steps=point_position; string str_int; for(iter=R_double.begin();iter!=R_double.end();iter++) { if(*iter!='.'&&*iter!='0') break; } for(;iter!=R_double.end();iter++) if(*iter!='.') str_int.push_back(*iter); temp.R_int=str_int; return temp;}/** \add point(.) in N_int,meanwhile delete 0 at the end * * \param1 int type of the product * \param2 the position to insert point(.) * \return final perfect product * */string add_0andpoint(const string& product_str,int pointsteps){ string final_str; string::const_iterator iter,iter1; int flag=0;//0 shows have not added point,1 shows have added point. if(product_str.size()>pointsteps) { iter1=product_str.begin()+product_str.size()-pointsteps; for(iter=product_str.begin();iter!=product_str.end();) { if(flag==0&&iter==iter1) { final_str.push_back('.'); flag=1; } else { final_str.push_back(*iter); iter++; } } } else { final_str.push_back('.'); flag=1; int i=0; for(i=1;i<pointsteps-product_str.size()+1;i++) final_str.push_back('0'); for(iter=product_str.begin();iter!=product_str.end();iter++) final_str.push_back(*iter); } //delete zeros of the end string::reverse_iterator iter2,iter3; for(iter2=final_str.rbegin();iter2!=final_str.rend();iter2++) if(*iter2!='0') break; for(iter3=final_str.rbegin();iter3!=final_str.rend();iter3++) if(*iter3=='.') break; if(iter3==final_str.rend()) return final_str; string::difference_type zeros_mount=iter2-final_str.rbegin(); if(iter3>iter2) final_str.erase(final_str.size()-zeros_mount,zeros_mount); else if(iter3==iter2) final_str.erase(final_str.size()-zeros_mount-1,zeros_mount+1); if(final_str.size()==0) final_str="0";//if final_str is null,let it be "0"; return final_str;}int main(){ int N;//power string R_double;//str input,the R of R^N Two_Var twovar; while (cin>>R_double>>N) { twovar=R_transform(R_double); cout<<add_0andpoint(power(twovar.R_int,N),N*twovar.point_steps)<<endl; } return 0;}
0 0
- POJ1001 且适合带小数点大数的幂(R^N)
- poj1001 大数的幂 java的用法
- poj1001(java 大数处理)
- uva 748 - Exponentiation 带小数点的大数乘法
- 大数相乘(小数点)
- C++大数精度计算(带小数点)
- (hdu 2.3.2)Exponentiation(大数:求R的n次幂)
- 带小数点的键盘
- 大数相关poj1001
- JAVA 处理 大数 POJ1001
- POJ1001大数相乘
- poj1001----大数乘法
- poj1001大数乘方java
- poj1001 java 大数
- poj1001&大数乘法
- 大数的N次幂
- 大数的阶层(N!)
- 非递归且最坏情况下仍然为O(n)的第K大数程序
- Java中static关键字的作用
- 找回丢失的磁盘空间
- chardet编码识别模块
- Android基础第七篇
- 2015-2016中国手机游戏行业年度研究报告
- POJ1001 且适合带小数点大数的幂(R^N)
- 习题2-4 子序列的和(subsequence)(java版本)
- 循环结构
- poj 1511 Invitation Cards(最短路)
- 利用Vue.js实现拼图游戏
- leetcode01_TwoSum
- Mybatis高效可配置的查询
- java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I最终解决方案(很多人不知道)
- Cocos2d-x入门教程(一)环境搭建与HelloWorld