学习简单算法的第一天

来源:互联网 发布:淘宝图片太大怎么截 编辑:程序博客网 时间:2024/05/21 17:55

买了本算法书,作为一位只能做码农的苦逼理科男,从今天开始学算法了

数组练习。编写一段程序,创建一个N*N的布尔数组a[][]。其中当 i 和 j 互质时(没有相同的因子),a[ i ][ j ] 为 true, 否则为 false.

//两个数互质public static boolean bool(int a, int b) {if(gcd(a,b) == 1)return true;elsereturn false;}
//欧几里得算法(求最大公约数)public static int gcd(int x, int y) {if (y == 0)return x;System.out.println(y + " " + x % y);return gcd(y, x % y);}
//创建一个二维布尔数组public static boolean[][] create_bool(int M, int N){boolean[][] b = new boolean[M][N];for(int i = 0; i < b.length; i++ )for(int j = 0; j < b[i].length; j++) b[i][j] = bool(i,j);return b;}

不知道怎么测试,试了几个数都正确。。。。。。。。。。。。。。