Created
August 13, 2015 15:26
-
-
Save qwIvan/d683e8fd316a764129b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
/** | |
* 分馒头算法 | |
* 秋香需要按照3:4:5:6的比例把馒头分给江南四大才子9527、9528、9529、9530这4人, | |
* 让每个人领到馒头的时间尽可能分散,两次领到馒头的时间间隔保持不变,以免出现时而吃不完、时而不够吃的情况。 | |
*/ | |
public class ManTouQueue { | |
public static void main(String[] args) { | |
int multi = 3*4*5*6; | |
Fetcher fa = new Fetcher(multi/3); | |
Fetcher fb = new Fetcher(multi/4); | |
Fetcher fc = new Fetcher(multi/5); | |
Fetcher fd = new Fetcher(multi/6); | |
ArrayList<Fetcher> arr = new ArrayList<>(); | |
arr.add(fa); | |
arr.add(fb); | |
arr.add(fc); | |
arr.add(fd); | |
for (int i = 0; i < multi; i++) { | |
Collections.min(arr).give("馒头"); | |
} | |
} | |
} | |
class Fetcher implements Comparable<Fetcher>{ | |
int weight; | |
int sum = 0; | |
Fetcher(int w){ | |
weight = w; | |
} | |
@SuppressWarnings("NullableProblems") | |
@Override | |
public int compareTo(Fetcher o) { | |
if (sum<o.sum)return -1; | |
else if (sum>o.sum)return 1; | |
else return 0; | |
} | |
public void give(String food){ | |
sum+=weight; | |
System.out.println(weight + "\t" + food); | |
} | |
} | |
/** | |
* 现在秋香需要把馒头分给13亿人,13亿人都带有自己的权值,有什么效率更高的分发算法呢? | |
* */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment