Routine Problem(数学)
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.
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 numbers pand q don't have a common divisor larger than 1.
将屏幕的宽度和影片画面的宽度设为一样,屏幕和影片的高根据比例做相应调整。若调整后屏幕的高>影片的高,则确定将影片的宽调整为屏幕的宽是正确的。否则表明应将影片的高调整为屏幕的高。
AC Code:
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 }