微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

输入一个数n,输出1,2,3,...,9999(n个9(利用大数求解

#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<<",";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐