Programming Assignment 3: Arrays

Here is the original link

  • DiscreteDistribution.java (概率论中的离散分布)
public class DiscreteDistribution
{
    public static void main(String[] args) {
        int m = Integer.parseInt(args[0]);
        int n = args.length - 1;
        int[] a = new int[n + 1];
        for (int i = 1; i <= n; i++)
        {
            a[i] = Integer.parseInt(args[i]);
        }
        int Sn = 0;
        int[] s = new int[n + 1];
        for (int i = 1; i <= n; i++)
        {
            s[i] = s[i - 1] + a[i];
            Sn += a[i];
        }
        while((m--) > 0)
        {
            int r = (int) (Math.random() * Sn);
            for (int i = 1; i <= n; i++)
            {
                if (r < s[i])
                {
                    System.out.print(i + " ");
                    break;
                }
            }
        }
    }
}
  • ThueMorse.java (ThueMorse序列常见于图像设计和音乐作品)
public class ThueMorse
{
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        char[] seq = new char[2 * n];
        int index = 0;
        seq[index++] = '0';
        for (int i = 1; i < n; i <<= 1)
        {
            for (int J = index, j = 0; j < J; j++)
            {
                seq[index++] = seq[j] == '0' ? '1' : '0';
            }
        }
        // System.out.println(seq);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (seq[i] == seq[j])
                {
                    System.out.print("+  ");
                }
                else
                {
                    System.out.print("-  ");
                }
            }
            System.out.println();
        }
    }
}
  • Birthday.java (23人之中,有50%的概率两人生日在同一天)
public class Birthday
{
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        int trials = Integer.parseInt(args[1]);

        int[] count = new int[n + 2];
        boolean[] flag = new boolean[n];
        for (int t = 0; t < trials; t++)
        {
            for (int i = 0; i < n; i++)
            {
                flag[i] = false;
            }
            int people = 0;
            while (true)
            {
                people++;
                int date = (int)(Math.random() * n);
                if (flag[date])
                {
                    break;
                }
                flag[date] = true;
            }
            count[people]++;
        }
        double fraction = 0.0;
        for (int i = 1, sum = 0; fraction < 0.5; i++)
        {
            sum += count[i];
            fraction = 1.0 * sum / trials;
            System.out.println(i + "\t" + count[i] + "\t" + fraction);
        }
    }
}
  • Minesweeper.java (扫雷小程序)
public class Minesweeper
{
    public static void main(String[] args) {
        int m = Integer.parseInt(args[0]);
        int n = Integer.parseInt(args[1]);
        int k = Integer.parseInt(args[2]);
        boolean[][] mines = new boolean[m][n];

        int total = m * n;
        int[] array = new int[total];
        for (int i = 0; i < total; i++)
        {
            array[i] = i;
        }
        for (int i = 0; i < k; i++)
        {
            int r = i + (int)(Math.random() * (total - i)); 
            int t = array[i];
            array[i] = array[r];
            array[r] = t;
        }
        for (int i = 0; i < k; i++)
        {
            mines[array[i] / n][array[i] % n] = true;
        }
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (mines[i][j])
                {
                    System.out.print("*  ");
                }
                else
                {
                    int count = 0;
                    if (i - 1 >= 0 && j - 1 >= 0 && mines[i - 1][j - 1]) count++;
                    if (i + 1 < m  && j - 1 >= 0 && mines[i + 1][j - 1]) count++;
                    if (i - 1 >= 0 && j + 1 < n  && mines[i - 1][j + 1]) count++;
                    if (i + 1 < m  && j + 1 < n  && mines[i + 1][j + 1]) count++;

                    if (j + 1 < n  && mines[i][j + 1]) count++;
                    if (j - 1 >= 0 && mines[i][j - 1]) count++;
                    if (i + 1 < m  && mines[i + 1][j]) count++;
                    if (i - 1 >= 0 && mines[i - 1][j]) count++;
                    System.out.print(count + "  ");
                }
            }
            System.out.println();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值