Manao has a monitor. The screen of the monitor has horizontal to vertical length ratioa:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratioc: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 fractionp / q.
A single line contains four space-separated integers a,b,c,d (1 ≤ a, b, c, d ≤ 1000).
Print the answer to the problem as "p/q", where p is a non-negative integer,q is a positive integer and numbersp and q don't have a common divisor larger than 1.
1 1 3 2
1/3
4 3 2 2
1/4
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:
总结:参考了位同学的,主要是可按边长或宽两种参考来处理,但能够先排除一种而不用去计算两种,不可行再排除
参考代码:
/*
* Author: pirate
* Created on: 2013年8月17日
* 196DIV2B.cpp
*/
#include <set>
#include <map>
#include <list>
#include <deque>
#include <queue>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 1000000
#define I64 __int64
#define LL long long
#define PI acos(-1.0)
#define L(a) ((a)<<1)
#define R(a) ((a)<<1+1)
#define sqr(a) ((a)*(a))
#define MID(a,b) ((a)+(b))/2
#define ABS(a) (a)>0?(a):(-a)
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define INIT(a,b) memset(a,b,sizeof(a))
int gcd(int x, int y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
int lcm(int x, int y) {
return x / gcd(x, y) * y;
}
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a * d < b * c) {
int tmp;
tmp = a, a = b, b = tmp;
tmp = c, c = d, d = tmp;
}
a *= lcm(b, d) / b;
c *= lcm(b, d) / d;
int g = gcd(a, c);
a /= g;
c /= g;
printf("%d/%d\n", (a - c) / gcd(a - c, a), a / gcd(a - c, a));
return 0;
}
参考后最终的代码
#include <iostream>
using namespace std;
#define swap(a, b) do{ (a) = (a) ^ (b); (b) = (a) ^ (b); (a) = (a) ^ (b); }while(0)
#define lcm(a,b) (a)*(b)/(gcd(a,b))
inline int gcd(int a, int b)
{
int tmp;
while(b){
tmp = a%b;
a = b;
b = tmp;
}
return a;
}
int a, b, c, d;
int main()
{
cin >> a >> b >> c >> d;
if(a*d < b*c)
{
swap(a,b);
swap(c,d);
}
a *=lcm(b, d)/b;
c *=lcm(b, d)/d;
cout << (a - c)/gcd(a, a-c) << "/" << a/gcd(a, a-c) << endl;
}