首页 > 学院 > 开发设计 > 正文

First Chapter of <Algorithms>

2019-11-06 08:37:18
字体:
来源:转载
供稿:网友

First chapter of “Algorithm” is the fundamental of java PRogramming language and a brief introduction to algorithms and data structure, including their concepts. some interesting problems and the basic implementations of them.

Code fragment

find the maximum of the array values

double max = a[0];for(int i = 0; i < a.length; i++) { if(a[i] > max) max = a[i];}

compute the average of the array values

double sum = 0;for(int i = 0; i < a.length; i++) { sum += a[i];}double average = sum / a.length;

copy to another array

double[] b = new double[a.length];for(int i = 0; i < a.length; i++) { b[i] = a[i];}

reverse the elements within an array

double tmp;for(int i = 0; i < a.length; i++) { tmp = a[i]; a[i] = a[a.length-1-i]; a[a.length-1-i] =tmp;}

matrix-matrix multiplication(square matrix)

double[][] c = new double[N][N];for(int i = 0; i< N; i++) for(int j = 0; j < N; j++) for(int k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j];
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表