2.8 Lab: Relatively Prime Array

Complete the given code so that makeGrid creates an n-by-n boolean array, result, such that result[i][j] is true if and only if i and j are relatively prime (also called coprime i.e. they have no common factors). Thus, each entry in the grid is for a pair of numbers. Location (i, j) is true if and only if i and j are coprime. For example,You may want to create some other helper methods.

public class Main
{
    // Do not change this method's signature or access.
    public static boolean[][] makeGrid(int n)
    {
        boolean[][] result = new boolean[n][n];
        for(int n1 =0; n1 < n; n1++) {
            for (int n2 = 0; n2 < n; n2++) {
                int a = n1;
                int b = n2;
                if (n1 == 0 || n2 == 0) {
                    if(n1 ==0 && n2 ==1){
                        result[n1][n2] = true;
                    }
                    else if(n1 == 1 && n2 == 0){
                        result[n1][n2] = true;
                    }
                    else
                        result[n1][n2] = false;
                }
                else{
                    if (a < b) {
                        int tmp = a;
                        a = b;
                        b = tmp;
                    }
                    int c;
                    while ((c = a % b) != 0) {
                        a = b;
                        b = c;
                    }

                    if (b == 1) {
                        result[n1][n2] = true;
                    }
                }
            }
        }


        return result;
    }




    public static void main(String[] args)
    {
        // Make sure we have the right number of arguments
        if(args.length != 1)
        {
            System.err.println("Should have exactly one argument n (the size of the grid).");
            System.exit(1);
        }

        // Make sure the argument is an integer
        int n;
        try
        {
            n = Integer.parseInt(args[0]);
        }
        catch (NumberFormatException ex)
        {
            System.err.printf("Argument must be an integer, given %s.\n", args[0]);
            System.exit(2);
            return;
        }

        // Make sure that n is positive
        if(n < 1)
        {
            System.err.printf("Argument must be positive, given %s.\n", args[0]);
            System.exit(3);
        }

        boolean[][] grid = makeGrid(n);

        // Print the grid
        for(int rowNumber = 0; rowNumber < n; rowNumber++)
        {
            for(int columnNumber = 0; columnNumber < n; columnNumber++)
            {
                System.out.print(grid[rowNumber][columnNumber] ? "*" : ".");
            }
            System.out.println();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值