Programming Assignment 2: Loops and Conditionals

Here is the original link

  • GeneralizedHarmonic.java(在数论中起重要作用)
public class GeneralizedHarmonic
{
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        int r = Integer.parseInt(args[1]);
        double H = 0.0;
        for (int i = 1; i <= n; i++)
        {
            H += 1 / Math.pow(i, r);
        }
        System.out.println(H);
    }
}
  • BandMatrix.java(数值线性代数中出现概率很大)
public class BandMatrix
{
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        int width = Integer.parseInt(args[1]);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (Math.abs(j - i) <= width)
                {
                    System.out.print("*  ");
                }
                else
                {
                    System.out.print("0  ");
                }
            }
            System.out.println();
        }
    }
}
  • RandomWalker.java(随机游走,布朗运动的离散版本)
public class RandomWalker
{
    public static void main(String[] args)
    {
        int step = 0;
        int r = Integer.parseInt(args[0]);
        int x = 0, y = 0;
        System.out.printf("(%d, %d)\n", x, y);
        while (Math.abs(x) + Math.abs(y) < r)
        {
            double random = Math.random();
            // north
            if (random < .25)
            {
                y++;
            }
            // east
            else if (random < .5)
            {
                x++;
            }
            // south
            else if (random < .75)
            {
                y--;
            }
            // west
            else
            {
                x--;
            }
            System.out.printf("(%d, %d)\n", x, y);
            step++;
        }
        System.out.println("steps = " + step);
    }
}
  • RandomWalkers.java
public class RandomWalkers
{
    public static void main(String[] args)
    {
        int r = Integer.parseInt(args[0]);
        int trials = Integer.parseInt(args[1]);
        int sum = 0;
        for (int i = 0; i < trials; i++)
        {
            int x = 0, y = 0, step = 0;
            while (Math.abs(x) + Math.abs(y) < r)
            {
                double random = Math.random();
                // north
                if (random < .25)
                {
                    y++;
                }
                // east
                else if (random < .5)
                {
                    x++;
                }
                // south
                else if (random < .75)
                {
                    y--;
                }
                // west
                else
                {
                    x--;
                }
                step++;
            }
            sum += step;
        }
        System.out.println("average number of steps = " + 1.0 * sum / trials);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值