输入一个数n,输出1,2,3,...,9999(n个9)(利用大数求解)
发布时间:2020-12-14 04:10:17 所属栏目:大数据 来源:网络整理
导读:#includeiostream#includevector#includeiterator#includestringusing namespace std;class bigInt{public:bigInt(){}bigInt(const bigInt data):v(data.v){}bigInt(string s){int len =s.size();while(len){if(s[len-1]='9's[len-1]=0)v.push_back(s[len-1]
#include<iostream> #include<vector> #include<iterator> #include<string> using namespace std; class bigInt { public: bigInt(){} bigInt(const bigInt &data):v(data.v) {} bigInt(string s) { int len =s.size(); while(len) { if(s[len-1]<='9'&&s[len-1]>=0) v.push_back(s[len-1]-'0'); else cout<<"不是数字n"; len--; } } bigInt(int n) { for(int i=0;i<n;i++) { v.push_back(9); } } bigInt(vector<int> v):v(v) { } bigInt operator +(bigInt d) { vector<int> data = d.v; bigInt sum; vector<int>::iterator iv = v.begin(); vector<int>::iterator idata = data.begin(); int x =0; while(iv!=v.end()&&idata!=data.end()) { x = (*iv) + (*idata)+x; sum.v.push_back(x%10); x = x/10; iv++; idata++; } while(iv!=v.end()) { x = (*iv) + x; sum.v.push_back(x%10); x = x/10; iv++; } while(idata!=data.end()) { x=(*idata) + x; sum.v.push_back(x%10); x = x/10; idata++; } if(x!=0) sum.v.push_back(x); return sum; } bigInt& operator =( bigInt& data) { v = data.v; return *this; } bigInt operator ++() { vector<int>::iterator iv = v.begin(); int x =1; while(iv!=v.end()) { x = (*iv) +x; *iv = x%10; x = x/10; iv++; } if(x!=0) v.push_back(x); return *this; } bigInt operator ++(int) { bigInt pre; pre.v = this->v; vector<int>::iterator iv = v.begin(); int x =1; while(iv!=v.end()) { x = (*iv) +x; *iv = x%10; x = x/10; iv++; } if(x!=0) v.push_back(x); return pre; } bool operator <=(bigInt& data) { bool isTrue=false; vector<int>::reverse_iterator iv = v.rbegin(); vector<int>::reverse_iterator idata = data.v.rbegin(); int lenIv = v.size(); int lenData = data.v.size(); if(lenIv>lenData) isTrue= false; else if(lenIv<lenData) isTrue= true; else { int i=lenIv; while(iv!=v.rend()) { if((*iv)>(*idata)) { isTrue= false; break; } else if(*iv<*idata) { isTrue = true; break; } else { iv++; idata++; i--; } } if(iv==v.rend()) { isTrue=true; } } return isTrue; } friend ostream& operator <<(ostream &os,const bigInt &data) { copy(data.v.rbegin(),data.v.rend(),ostream_iterator<int,char>(os,"")); return os; } private: vector<int> v; }; void out(int ); int main() { out(3); cout<<endl; //out(3); cout<<endl; } //若输入的数为n,则输出数1,2,3,4,...,9999(到n个9) void out(int n) { bigInt end(n); bigInt start("1"); for(;start<=end;start++) cout<<start<<","; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |