POJ1001 且适合带小数点大数的幂(R^N)
2016-09-03 19:15
399 查看
/** \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*str2 string 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; }
相关文章推荐
- [转]POJ 1001 Exponentiation Java大数处理高精度小数
- POJ 1001 Exponentiation(大数幂Java实现)
- Exponentiation(POJ-1001) Java大数例题
- poj 1001 uva 748 Exponentiation(高精度小数乘法)
- 大数乘法——POJ 1001
- 简单题POJ-1001,大数乘法
- Java大数 poj 1001
- POJ 1001 Exponentiation 模拟小数幂
- POJ 题目1001 ,hdu1753 (大数,Java简单题)
- POJ 1001 Exponentiation(大数处理)
- POJ 1001 Exponentiation (高精度/大数乘法)
- POJ 1001 Exponentiation 无限大数的指数乘法 题解
- POJ 2635 N进制大数与小数取模
- POJ 1001 Exponentiation 高精度小数相乘
- poj 1001 求一个小数的N次方
- POJ 1001-Exponentiation(NYOJ 155 求高精度幂):大数问题
- poj1001大数乘方java
- HDU 1063 && POJ 1001 Exponentiation (JAVA浮点数大数)
- POJ 1001-Exponentiation(NYOJ 155 求高精度幂):大数问题
- POJ 1001(大数相乘)