[math][第二阶段-easy math][HDU-1722]Cake

来源:互联网 发布:二叉树层序遍历算法 编辑:程序博客网 时间:2024/05/23 02:01

Problem Description
一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食. 
 

Input
每行有两个数p和q.
 

Output
输出最少要将蛋糕切成多少块.
 

Sample Input
2 3
 

Sample Output
4
Hint
将蛋糕切成大小分别为1/3,1/3,1/6,1/6的四块即满足要求.当2个人来时,每人可以吃1/3+1/6=1/2 , 1/2块。当3个人来时,每人可以吃1/6+1/6=1/3 , 1/3, 1/3块。
 

import java.util.Scanner;public class Main {public static long GCD(long n,long m){long temp;if(m>n){temp = m;m = n;n = temp;}while(n%m!=0){temp = n%m;n = m;m = temp;}return m;}public static void main(String[] args) {// TODO Auto-generated method stubScanner in = new Scanner(System.in);while(in.hasNext()){long p = in.nextInt();long q = in.nextInt();if(p>q){long  temp=q;q = p;p = temp;}System.out.println(p+q-GCD(p,q));}}}



0 0
原创粉丝点击