码迷,mamicode.com
首页 > 其他好文 > 详细

1006

时间:2017-10-27 18:05:00      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:break   result   list   ase   ati   highlight   []   port   void   

起始点不同的最小公倍数

 

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		List<Integer> days = new ArrayList<Integer>();
		while(cin.hasNext()) {
			int p = cin.nextInt() % 23;
			int e = cin.nextInt() % 28;
			int i = cin.nextInt() % 33;
			int d = cin.nextInt();
			if(p == -1 && e == -1 && i== -1 && d == -1)
				break;
			int[] mergeResult = merge(p, e, 23, 28);
			int[] result = merge(mergeResult[0], i, mergeResult[1], 33);
			if(result[0] <= d)
				result[0] += result[1];
			days.add(result[0] - d);
		}
		for(int i=0;i<days.size();) {
			System.out.println("Case " + ++i + ": the next triple peak occurs in " + days.get(i-1) + " days.");
		}

	}

	private static int[] merge(int a, int b, int c, int d) {
		int[] result = new int[2];
		if(a > b) {
			int i = 0;
			while((a - b + i * c) % d != 0) {
				i++;
			}
			result[0] = i * c + a;
		} else if(a == b){
			result[0] = a;
		} else {
			int i = 0;
			while((b - a + i * d) % c != 0) {
				i++;
			}
			result[0] = i * d + b;
		}
		result[1] = lcm(c, d);
		return result;
	}

	private static int lcm(int m, int n) {
		int mul = m * n;
		if(m > n) {
			int t = m;
			m = n;
			n = t;
		}

		int k = 0;

		while(m != 0) {
			k = n % m;
			n = m;
			m = k;
		}

		return mul / n;
	}
}

  

1006

标签:break   result   list   ase   ati   highlight   []   port   void   

原文地址:http://www.cnblogs.com/aaaakun/p/7744127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!