标签: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; } }
标签:break result list ase ati highlight [] port void
原文地址:http://www.cnblogs.com/aaaakun/p/7744127.html