UB求助
  • 板块学术版
  • 楼主Alg_orithm
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/8/5 15:09
  • 上次更新2025/8/5 18:46:48
查看原帖
UB求助
1029575
Alg_orithm楼主2025/8/5 15:09

我在写高精度的时候发现的神奇事件??dalao求解

#include<bits/stdc++.h>
using namespace std;
const int N=8010;
int jz=10;
string alphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct bigint
{
	int a[N];
};
bigint rev(bigint x)
{
	bigint res;
	res.a[0]=x.a[0];
	for(int i=1;i<=x.a[0];i++) res.a[res.a[0]-i+1]=x.a[i];
	return res;
}
void init(bigint& x)
{
	memset(x.a,0,sizeof(x.a));
}
bigint lowToHigh(long long y)
{
	bigint res;
	init(res);
	long long x=y;
	while(x)
	{
		res.a[++res.a[0]]=x%jz;
		x/=jz;
	}
	return res;
}
bigint input()
{
	bigint res;
	init(res);
	string s;
	cin>>s;
	res.a[0]=s.size();
	for(int i=0;i<s.size();i++)
		res.a[res.a[0]-i]=alphabet.find(s[i]);
	return res;
}
void output(bigint x)
{
	for(int i=x.a[0];i>0;i--)
		cout<<alphabet[x.a[i]];
}
bigint operator*(bigint x,bigint y)
{
	bigint c;
	init(c);
	c.a[0]=x.a[0]+y.a[0];
	for(int i=1;i<=x.a[0];i++)
		for(int j=1;j<=y.a[0];j++)
		{
			c.a[i+j-1]+=x.a[i]*y.a[j];
			c.a[i+j]+=c.a[i+j-1]/jz;
			c.a[i+j-1]%=jz;
		}
	while(c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
	return c;
}
bigint operator+(bigint x,bigint y)
{
	bigint c;
	init(c);
	int t=0;
	c.a[0]=max(x.a[0],y.a[0])+1;
	for(int i=1;i<=c.a[0];i++)
		{
			c.a[i]=x.a[i]+y.a[i]+t;
			t=c.a[i]/jz;
			c.a[i]%=jz;	
		}
	while(c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
	if(t) c.a[++c.a[0]]=t;
	return c;	
} 
bigint operator/(bigint x,long long b)
{
	bigint a,c;
	a=rev(x);
	long long t=0;
	for(int i=1;i<=a.a[0];i++)
	{
		t=t*jz+a.a[i];
		if(t<b) c.a[i]=0;
		else
		{
			c.a[i]=t/b;
			t%=b;
		}
	}
	c=rev(c);
	while(c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
	return c;
}
bigint operator-(bigint x,bigint y)
{
	bigint res;
	init(res);
	for(int i=1;i<=x.a[0];i++)
	{
		res.a[i]+=x.a[i]-y.a[i];
		if(res.a[i]<0)
		{
			res.a[i+1]--;
			res.a[i]+=jz;
		}
		if(res.a[i]) res.a[0]=i;
	}
	return res;
}
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	bigint a,z;
    long long b;
	a=input();
	cin>>b;
	output(a/b);
	return 0;
}

这段代码输入10 5,什么也不输出

但把output(a/b);改成

z=a/b;
output(z);

输入10 5,输出2

以及测评记录 我把第一个样例下载下来,本地运行没问题

2025/8/5 15:09
加载中...