题设:现在有100个人,每个人手里有100块钱,现在游戏开始,每个人都拿出一块钱给随机的一个人,那么这个游戏一直持续下去,会是什么结果呢?
曾经有人拿这个问题问过五位非常聪明的博士,他们的答案不尽相同,都是很平均,差不了多少
那么真实的结果到底是什么情况呢,现在我们用java swing的GUI编程来模拟一下这个过程,看看最终结果如何
package com.company;
import java.awt.*;
import javax.swing.*;
public class AlgoFrame extends JFrame{
private int canvasWidth;
private int canvasHeight;
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
super(title);
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
AlgoCanvas canvas = new AlgoCanvas();
setContentPane(canvas);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public AlgoFrame(String title){
this(title, 1024, 768);
}
public int getCanvasWidth(){
return canvasWidth;}
public int getCanvasHeight(){
return canvasHeight;}
private int[] money;
public void render(int[] money){
this.money = money;
repaint();
}
private class AlgoCanvas extends JPanel{
public AlgoCanvas(){
// 双缓存
super(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// 抗锯齿
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.addRenderingHints(hints);
// 具体绘制
int w=canvasWidth/money.length;
for(int i=0;i<money.length;i++)