HDU 1722 Cake (数论 gcd)(Java版)

Big Number

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1722

    ——每天在线,欢迎留言谈论。

题目大意:

 给你两个数 n1,n2 。

 然后你有一块蛋糕,提前切好,使得不管来 n1 还是 n2 个人都能够当场平均分配。

 求 “提前切好” 的最小蛋糕块数。

知识点:

(请无视)公式:N = a + b + gcd(a, b) ;

思路:

(勿无视)先份成p块,然后再拼到一起,再从原来开始的地方,将蛋糕再分成q份,中间肯定会出现完全重合的块数为k,则此是需要分的块数就是 p + q - k 。

 PS. K = gcd(a, b) 。

Java AC代码:

 1 import java.util.Scanner;
 2 import java.math.*;
 3 public class Main {
 4     public static Scanner scn = new Scanner(System.in);
 5     public static void main(String[] args) {
 6         int a,b;
 7         while(scn.hasNext()) {
 8             a = scn.nextInt();
 9             b = scn.nextInt();
10             System.out.println(a+b-tool.gcd(a, b));
11         }
12         System.exit(0);
13     }
14 }
15 class tool {
16     public static int gcd(int a, int b) {
17         int temp;
18         if (a < b) {
19             temp = a;
20             a = b;
21             b = temp;
22         }
23         while (b != 0){
24             temp = a;
25             a = b;
26             b = temp % b; 
27         }
28         return a;
29     }
30 }

2017-07-23 17:16:21

原文地址:https://www.cnblogs.com/Twobox/p/7225365.html