蒟蒻刚学高精度求调
  • 板块灌水区
  • 楼主yueluoxingchen
  • 当前回复14
  • 已保存回复14
  • 发布时间2024/8/2 10:43
  • 上次更新2024/8/2 11:53:31
查看原帖
蒟蒻刚学高精度求调
554576
yueluoxingchen楼主2024/8/2 10:43

题目

高精度加法,40pts,其余RE

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<stack>
#include<queue>
#define debug() cout<<"qwq"<<endl
using namespace std;

string c1,c2;
string a,b;
int last=0;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

inline void write(int x)
{
	
	if (!x)
	{
		putchar('0');
		return;
	}
	int len = 0, k1 = x, c[40];
	if (k1 < 0) k1 = -k1, putchar('-');
	while (k1) c[len ++ ] = k1 % 10 ^ 48, k1 /= 10;
	while (len -- ) putchar(c[len]);
}

int len;

string add(string x,string y)
{
	string tmp="";
	len=max(x.length(),y.length());
	if(x.length()<y.length())
	{
		for(int i=x.length();i<len;i++) x='0'+x;
	}else for(int i=y.length();i<len;i++) y='0'+y;
	for(int i=len-1;i>=0;i--)
	{
		//out<<i<<":"<<x[i]<<" "<<y[i]<<endl;
		int tp=(int)(x[i]-'0');
		int tpp=(int)(y[i]-'0');
		int xxx=(int)(tp+tpp+last);
		if(xxx>=10)
		{
			tmp[i]=(char)((xxx-10)+'0');
			last=1;
		}else{
			tmp[i]=(char)(xxx+'0');
			last=0;
		}
	}
	if(last) cout<<1;
	return tmp;
}

int main()
{
	cin>>a>>b;
	string c=add(a,b);
	for(int i=0;i<len;i++) cout<<c[i];
	return 0;
}
2024/8/2 10:43
加载中...