Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
求教:测试都通过了, 前辈给的数据也可以,但是为什么WA#include <iostream> #include <string> #include <vector> #include <algorithm> //for for_each using namespace std; int removedot0(string& s); //remove decimal & leading and insignificant trailing 0s string reverse(const string& s); string MyPower(const string& s, const int n); void out(string& s){ cout <<s <<endl; } int main() { string s; int n; vector<string> vs; while(cin>>s>>n) vs.push_back(MyPower(s,n)); for_each(vs.begin(), vs.end(), out); system("pause"); return 0; } int removedot0(string& s) //return number of digits after the decimal point { //remove heading 0s while(s.begin()!=s.end() && *s.begin()=='0') s.erase(s.begin()); string::iterator i; for(i = s.begin(); i != s.end(); ++i) { if (*i == '.') { s.erase(i); //now i points to the first digit after decimal break; } } //remove tailing 0s while (i!=s.end()&&*s.rbegin() =='0') s.erase(s.end()-1); return s.size()-(i-s.begin()); } string reverse(const string& s) { size_t size=s.size(); string str(s); for(size_t i=0; i<size; ++i) str[i] = s[size-1-i]; return str; } string MyPower(const string& s, const int n) { if(n==0) return string("1"); string str(s); int nf = removedot0(str); if(str=="") return string("0"); int nf_total = nf; str = reverse(str); vector<int> vr; //the result for(size_t i(0); i<str.size(); ++i) vr.push_back(str[i]-'0'); for(int i(0); i<n-1; ++i) { vector<int> v(vr); //the multiply factor vr.clear(); for(size_t j(0); j<v.size()+str.size();++j) { int sum(0); for(size_t k=j, h=0; k>=0&&h<str.size(); ++h,--k) if (k<v.size()) sum+=v[k]*(str[h]-'0'); vr.push_back(sum); } for(size_t j(0); j<vr.size();++j) { if (j==vr.size()-1) { if(vr[j]/10) vr.push_back(vr[j]/10); } else vr[j+1] +=vr[j]/10; vr[j] %=10; } nf_total += nf; } str=""; for(size_t j(0); j<vr.size();++j) str += vr[j]+'0'; if(nf_total) str.insert(nf_total, "."); return reverse(str); } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator