zoukankan      html  css  js  c++  java
  • Routine Problem(数学)

     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 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
    ", p / m, q / m);
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    面对祖传屎山代码应该采用的5个正确姿势
    一行代码卖出570美元, 天价代码的内幕
    漫画 | 悲催的中国式软件开发
    看看我每天的工作,你们这些程序员都是“辣鸡”!
    漫画 | 浏览器一个比一个“无耻”
    程序员应该造的五大轮子
    我所尊敬的三位女程序员
    重磅!七国首脑会议决定制裁Go语言!
    漫画 | C语言哭了,过年回家,只有我还没对象
    漫画 | CPU战争40年,真正的王者终于现身!
  • 原文地址:https://www.cnblogs.com/cszlg/p/3280196.html
Copyright © 2011-2022 走看看