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.ios
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.spa
A single line contains four space-separated integers a, b, c, d (1 ≤ a, b, c, d ≤ 1000).code
Print the answer to the problem as "p/q", where p is a non-negative integer, q is a positive integer and numbers pand q don't have a common divisor larger than 1.blog
將屏幕的寬度和影片畫面的寬度設爲同樣,屏幕和影片的高根據比例作相應調整。若調整後屏幕的高>影片的高,則肯定將影片的寬調整爲屏幕的寬是正確的。不然代表應將影片的高調整爲屏幕的高。ci
AC Code:input
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 5 using namespace std; 6 7 long long lcd(long long y, long long x) 8 { 9 long long r = x % y; 10 while(r){ 11 x = y; 12 y = r; 13 r = x % y; 14 } 15 return y; 16 } 17 18 int main() 19 { 20 long long a, b, c, d, p, q, m; 21 while(scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &d) != EOF){ 22 if(a * d == b * c) { 23 puts("0/1"); 24 continue; 25 } 26 b *= c; 27 d *= a; 28 a = c = a * c; 29 if(b > d){ //以寬爲基準 30 p = b - d; 31 q = b; 32 } 33 else{ //以高爲基準 34 p = d * a - b * c; 35 q = a * d; 36 } 37 m = lcd(p, q); 38 printf("%I64d/%I64d\n", p / m, q / m); 39 } 40 return 0; 41 }