Codeforces Round #196 (Div. 2) / 337B Routine Problem(数学)

100 篇文章 0 订阅
83 篇文章 0 订阅

B. Routine Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.

Calculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction p / q.

Input

A single line contains four space-separated integers abcd (1 ≤ a, b, c, d ≤ 1000).

Output

Print the answer to the problem as "p/q", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1.

Sample test(s)
input
1 1 3 2
output
1/3
input
4 3 2 2
output
1/4
Note

Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension:

Sample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension:


题目可以转化为:输出多余空间面积占整个屏幕面积的多少。

这么思考后,我们要做的就是把比例变成边长。

比如在第一种情况,d/c<b/a,那么我们就把长边a和c统一长度为ac,同时b为bc,d为ad

所以多出来的面积就为1-(ac)*(ad)/((ac)*(bc))=(b * c - a * d) / (b * c)

对于d/c>b/a的情况同理,为(a * d - b * c) / (a * d)

对于d/c=b/a,就输出0/1


复杂度:O(log max(bc,ad))


完整代码:

/*30ms,0KB*/

#include<cstdio>

int gcd(int a, int b)
{
	return b > 0 ? gcd(b, a % b) : a;
}

int main(void)
{
	int a, b, c, d, temp;
	scanf("%d%d%d%d", &a, &b, &c, &d);
	if (b * c - a * d > 0)
	{
		int p = b * c - a * d, q = b * c;
		temp = gcd(p, q);
		printf("%d/%d", p / temp, q / temp);
	}
	else if (a * d - b * c > 0)
	{
		int p = a * d - b * c, q = a * d;
		temp = gcd(p, q);
		printf("%d/%d", p / temp, q / temp);
	}
	else
		printf("0/1");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值