pata1088(要吐血了也没全过!!!)

本文介绍了一种基于C++的有理数基本算术运算的实现方法,包括加、减、乘、除等操作。文章提供了详细的代码示例,展示了如何进行有理数的输入、最简形式的输出及特殊情况的处理。

1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
写到吐血的一道烂题!!!(仅供自己审查,并没有全部通过,还有第三个测试点)

#include<iostream> 
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
struct rational{
	ll a;
	ll b;
}t[2];
ll GCD(ll a,ll b){
	a=fabs(a);
	b=fabs(b);
	return b==0?a:GCD(b,a%b);
}
rational sum(rational aa,rational bb,ll gcd){
	ll ta=0,tb=0;
	ll k=aa.b*bb.b/gcd;//最小公倍数,也即是分母的值 
	ta=(aa.a*(k/aa.b))+(bb.a*(k/bb.b));//分子的值
	ll m=(GCD(ta,k));
	ta=ta/m;
	k=k/m;
	rational _sum;
	_sum.a=ta;
	_sum.b=k;
	return _sum; 
}
rational sumoppose(rational aa,rational bb,ll gcd){
	ll ta=0,tb=0;
	ll k=aa.b*bb.b/gcd;//最小公倍数,也即是分母的值 
	ta=(aa.a*(k/aa.b))-(bb.a*(k/bb.b));//分子的值
	ll m=(GCD(ta,k));
	ta=ta/m;
	k=k/m;
	rational _sum;
	_sum.a=ta;
	_sum.b=k;
	return _sum; 
}
rational product(rational aa,rational bb){
	rational _product;
	_product.a=aa.a*bb.a;
	_product.b=aa.b*bb.b;
	return _product;
}
rational productoppose(rational aa,rational bb){
	rational _product;
	_product.a=aa.a*bb.b;
	_product.b=aa.b*bb.a;
	return _product;
}
rational yuefen(rational aa){
	ll k=GCD(aa.a,aa.b);
	aa.a/=k;
	aa.b/=k;
	return aa;
}
void showresult(rational aa){
	if(aa.a<0){
		cout<<"(";
	}
	if(aa.a==0){
		cout<<0;
	}else if(aa.a%aa.b!=0){
		if(aa.b==1){
			cout<<aa.a;
		}else if(abs(aa.a)>aa.b){
			cout<<aa.a/aa.b<<' '<<(ll)abs(aa.a)%aa.b<<"/"<<aa.b;
		}else{
			cout<<aa.a<<"/"<<aa.b;
		}
	}else if(aa.a%aa.b==0){
		cout<<aa.a/aa.b;
	}
	
	if(aa.a<0){
		cout<<")";
	}
}
int main()
{
	rational tt[4];
	for(ll i=0;i<2;i++){
		ll temp1=0;
		ll temp2=0;
		char temp3;
		cin>>temp1>>temp3>>temp2;
		t[i].a=temp1;
		t[i].b=temp2;
	}
	ll k=GCD(t[0].b,t[1].b);
	tt[0]=sum(t[0],t[1],k);
	tt[1]=sumoppose(t[0],t[1],k);
	tt[2]=product(t[0],t[1]);
	tt[3]=productoppose(t[0],t[1]);
	for(ll i=0;i<4;i++){
		tt[i]=yuefen(tt[i]);
		if(tt[i].b<0){
			tt[i].b*=-1;
			tt[i].a*=-1;
		}
	}
	
	for(ll i=0;i<4;i++){
		//ll ttt=i;
		switch (i){
			case 0 :{
				showresult(t[0]);
				cout<<" + ";
				showresult(t[1]);
				cout<<" = ";
				showresult(tt[i]);
				cout<<endl;
				break;
			}
			case 1 :{
				showresult(t[0]);
				cout<<" - ";
				showresult(t[1]);
				cout<<" = ";
				showresult(tt[i]);
				cout<<endl;
				break;
			}
			case 2 :{
				showresult(t[0]);
				cout<<" * ";
				showresult(t[1]);
				cout<<" = ";
				showresult(tt[i]);
				cout<<endl;
				break;
			}
			case 3 :{
				if(t[1].a){
					showresult(t[0]);
					cout<<" / ";
					showresult(t[1]);
					cout<<" = ";
					showresult(tt[i]);
					cout<<endl;
				}else{
					showresult(t[0]);
					cout<<" / ";
					showresult(t[1]);
					cout<<" = ";
					cout<<"Inf";
					cout<<endl;
				}
				break;
			}
		}
			
	}
	return 0;
}







根据原作 https://pan.quark.cn/s/0ed355622f0f 的源码改编 野火IM解决方案 野火IM是专业级即时通讯和实时音视频整体解决方案,由北京野火无限网络科技有限公司维护和支持。 主要特性有:私有部署安全可靠,性能强大,功能齐全,全平台支持,开源率高,部署运维简单,二次开发友好,方便与第三方系统对接或者嵌入现有系统中。 详细情况请参考在线文档。 主要包括一下项目: 野火IM Vue Electron Demo,演示如何将野火IM的能力集成到Vue Electron项目。 前置说明 本项目所使用的是需要付费的,价格请参考费用详情 支持试用,具体请看试用说明 本项目默认只能连接到官方服务,购买或申请试用之后,替换,即可连到自行部署的服务 分支说明 :基于开发,是未来的开发重心 :基于开发,进入维护模式,不再开发新功能,鉴于已经终止支持且不再维护,建议客户升级到版本 环境依赖 mac系统 最新版本的Xcode nodejs v18.19.0 npm v10.2.3 python 2.7.x git npm install -g node-gyp@8.3.0 windows系统 nodejs v18.19.0 python 2.7.x git npm 6.14.15 npm install --global --vs2019 --production windows-build-tools 本步安装windows开发环境的安装内容较多,如果网络情况不好可能需要等较长时间,选择早上网络较好时安装是个好的选择 或参考手动安装 windows-build-tools进行安装 npm install -g node-gyp@8.3.0 linux系统 nodej...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值