50分c++求调
查看原帖
50分c++求调
1047309
return_second楼主2025/1/19 14:52
#include<bits/stdc++.h>
using namespace std;
const int N=10090;
struct Big
{
	int len,a[N];
	Big(long long x=0)
	{
		len=0;
		memset(a,0,sizeof(a));
		while(x)
		{
			a[++len]=x%10;
			x/=10;
		}
	}
	Big(string s)
	{
		len=s.size();
		for(int i=1;i<=len;i++)
			a[i]=s[len-i]-'0';
	}
	void scan()
	{
		string s;
		cin>>s;
		*this=Big(s); 
	}
	void print()
	{
		for(int i=max(1,len);i>=1;i--)
			cout<<a[i]; 
	}
	bool operator<(const Big& x)const
	{
		if(len!=x.len)
			return len<x.len;
		for(int i=len;i>=1;i--)
			if(a[i]!=x.a[i])
				return a[i]<x.a[i];
		return false;
	}
	Big operator+(const Big& x)const
	{
		Big c;
		c.len=max(len,x.len);
		for(int i=1;i<=c.len;i++)
			c.a[i]=a[i]+x.a[i];
		for(int i=1;i<=c.len;i++)
		{
			c.a[i+1]+=c.a[i]/10;
			c.a[i]%=10;
		}
		if(c.a[c.len+1])
			c.len++;
		return c;
	}
	Big operator-(const Big& x)const
	{
		Big c;
		c.len=max(len,x.len);
		for(int i=1;i<=c.len;i++)
			c.a[i]=a[i]-x.a[i];
		for(int i=1;i<=c.len;i++)
			if(c.a[i]<0)
			{
				c.a[i]+=10;
				c.a[i+1]--;
			} 
		while(!c.a[c.len])c.len--;
		return c;
	}
};
int main() 
{
	Big a,b;
	a.scan();b.scan();
	if(a<b)
	{
		swap(a,b);
		putchar('-');
	}
	Big c=a-b;
	c.print();
    return 0;
}

hack:

30141
8160
2025/1/19 14:52
加载中...