关闭

1088. Rational Arithmetic (20)

标签: 其他
142人阅读 评论(0) 收藏 举报
分类:

PAT测试不支持%I64d 可以使用%lld,,被坑了好久。但是题目没有难度

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long long gcd(long long a,long long b){
	if(a<0){
		a=-a;
	}
	if(b<0){
		b=-b;
	}
	long long t;
	if(a<b){
		t=a;
		a=b;
		b=t;
	}
	long long n;
	while(b){
		n=a%b;
		a=b;
		b=n;
	}
	return a;
}


void print1(long long num,long long deno){
	long long flag=gcd(deno,num);
	int temp=0;
	num=num/flag;deno=deno/flag;
	if((num*deno)<0){
		cout<<'(';
		temp=1;
	}
	if(deno<0){
		num=-num;
		deno=-deno;
	}
	long long num1=abs(num);
	if(num==0){
		cout<<0;
	}
	else if(deno==1){
		cout<<num;
	}
	else if(num1<deno){
		cout<<num<<'/'<<deno;
	}
	else{
		long long k=num/deno;
		num=num1%deno;
		cout<<k<<' '<<num<<'/'<<deno;
	}
	if(temp){
		cout<<')';
	}
	return ;
}

void Sum(long long a,long long b,long long c,long long d){
	
	long long deno,num,flag;
	deno=b*d;
	num=a*d+c*b;
	print1(a,b);
	cout<<' '<<'+'<<' ';
	print1(c,d);
	cout<<' '<<'='<<' ';
	print1(num,deno);
	cout<<endl; 
	return ;
}

void Dif(long long a,long long b,long long c,long long d){
	long long deno,num,flag;
	deno=b*d;
	num=a*d-c*b;
//	flag=gcd(deno,num);
//	num=num/flag;deno=deno/flag;
	print1(a,b);
	cout<<' '<<'-'<<' ';
	print1(c,d);
	cout<<' '<<'='<<' ';
	print1(num,deno);
	cout<<endl;
	return ;
}

void Pro(long long a,long long b,long long c,long long d){
	long long deno,num,flag;
	deno=b*d;
	num=a*c;
//	flag=gcd(deno,num);
//	num=num/flag;deno=deno/flag;
	print1(a,b);
	cout<<' '<<'*'<<' ';
	print1(c,d);
	cout<<' '<<'='<<' ';
	print1(num,deno);
	cout<<endl;
	return ;
}

void Quo(long long a,long long b,long long c,long long d){
	if(c==0){
		print1(a,b);
		cout<<' '<<'/'<<' ';
		print1(c,d);
		cout<<' '<<'='<<' ';
		printf("Inf");
		return ;
	}
	long long deno,num,flag;
	deno=b*c;
	num=a*d;
//	flag=gcd(deno,num);
//	num=num/flag;deno=deno/flag;
	print1(a,b);
	cout<<' '<<'/'<<' ';
	print1(c,d);
	cout<<' '<<'='<<' ';
	print1(num,deno);
	return ;
}

int main(){
	long long a,b,c,d;
	scanf("%lld/%lld%lld/%lld",&a,&b,&c,&d);//输入用%lld
//	cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
	Sum(a,b,c,d);
	Dif(a,b,c,d);
	Pro(a,b,c,d);
	Quo(a,b,c,d);
	return 0;
} 


0
0
查看评论

1088. Rational Arithmetic (20) -- 公约数,字符串输出处理

题目地址 http://www.patest.cn/contests/pat-a-practise/1088For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate...
  • qq_26437925
  • qq_26437925
  • 2015-08-16 16:08
  • 419

PAT (Advanced Level) 1088. Rational Arithmetic (20) 分数四则运算

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input...
  • tuzigg123
  • tuzigg123
  • 2015-07-30 21:32
  • 706

Rational Arithmetic

1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y...
  • jgzquanquan
  • jgzquanquan
  • 2017-08-07 14:17
  • 96

1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For ...
  • zzyafyj
  • zzyafyj
  • 2014-11-30 10:36
  • 1299

PAT 1088. Rational Arithmetic (20)

一个测试点错误,目前还没找出来 考试时这道题都懒得做。。实在太麻烦 感觉出这种题挺没意义的 #define MAX1 100005 #define MAX2 1015 #include #include #include #include #include #include #include #...
  • u012443276
  • u012443276
  • 2014-11-30 14:15
  • 294

PAT-A 1088. Rational Arithmetic (20)

有理数的四则运算
  • Daniel960601
  • Daniel960601
  • 2017-03-25 20:50
  • 161

【PAT】1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input S...
  • realxuejin
  • realxuejin
  • 2015-10-13 21:12
  • 495

1088. Rational Arithmetic (20)解题报告

//我使用了C++的类和运算符重载#include #include #include #define N 1000 /* run this program using the console pauser or add your own getch, system("pause&...
  • chr1991
  • chr1991
  • 2016-12-28 14:12
  • 91

1088. Rational Arithmetic 解析

难点主要在分式的通分上面,转化成求最大公因数的问题。 #include #include #include using namespace std; struct Rational { long int fra; long int dec; }; Rational r1, r2; i...
  • sheepyWYY
  • sheepyWYY
  • 2017-05-05 16:55
  • 114

【PAT】1088. Rational Arithmetic

#define LOCAL #include #include #include #include #include using namespace std; typedef long long ll; ll gcd(ll a,ll b){ return b==0?a:gcd(b...
  • KID_LWC
  • KID_LWC
  • 2017-02-05 22:42
  • 63
    个人资料
    • 访问:20795次
    • 积分:920
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论
  • ccf 俄罗斯方块

    oxygen_czp: 楼主这个算法只能说是投机取巧,不能说是正确的 算法虽然能够得100 但我已经发现两处有问题1:当...