zoukankan      html  css  js  c++  java
  • 杭电ACM2503a/b+c/d

    a/b + c/d

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4200    Accepted Submission(s): 2457

    Problem Description
    给你2个分数,求他们的和,并要求和为最简形式。
     
    Input
    输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0<a,b,c,d<1000),表示两个分数a/b 和 c/d。
     
    Output
    对于每组测试数据,输出两个整数e和f,表示a/b + c/d的最简化结果是e/f,每组输出占一行。
     
    Sample Input
    2
    1 2 1 3
    4 3 2 3
     
    Sample Output
    5 6
    2 1
     1 import java.util.Scanner;
    2
    3 public class Main {
    4 public static int MaxCommonDivisor(int m,int n){
    5 int temp;
    6 if(m<n){
    7 temp = m;
    8 m = n;
    9 n = temp;
    10 }
    11 while(n!=0){
    12 temp = m%n;
    13 m = n;
    14 n = temp;
    15 }
    16 return m;
    17 }
    18 public static void main(String[] args) {
    19 Scanner scan = new Scanner(System.in);
    20 int t = scan.nextInt();
    21 int a,b,c,d,e,f;
    22 for(int i=1;i<=t;i++){
    23 a = scan.nextInt();
    24 b = scan.nextInt();
    25 c = scan.nextInt();
    26 d = scan.nextInt();
    27 e = a*d+b*c;
    28 f = b*d;
    29 int gcd = MaxCommonDivisor(e,f);
    30 e = e/gcd;
    31 f = f/gcd;
    32 System.out.println(e+" "+f);
    33 }
    34 }
    35 }
  • 相关阅读:
    prototype
    JS中我们为什么要new个实例而不直接执行
    购物车,实现增删改查;无bug,还有一个直接修改购物车数量功能未实现
    jquery中判断复选框有没有被选上
    git
    scss
    gulp基本操作
    nodejs,,一些基本操作--server。js
    node.js介绍及简单例子
    自己定义jquery插件轮播图
  • 原文地址:https://www.cnblogs.com/bchxsx322/p/2430737.html
Copyright © 2011-2022 走看看