1. 编码实现一个类:(1)提供一个静态方法,可以将输入的一个int[]数组按照从小到大的顺序排列;(2)提供静态方法,对排好序的数组使用折半查找(使用递归和非递归两种形式分别实现)查找某一个整数。
public class MyArray {
public void asort(int a[]){
int s= a.length;
int t;
for(int i = 0;i<s;i++){
int k = i;
for(int j = s - 1; j>i; j--){
if(a[j]<a[k])k = j;
}
t= a[i];
a[i]= a[k];
a[k]=t;
}
}
public int asearch(int a[],int n){
int l = 0;
int r = a.length - 1;
while (l<=r){
int m = (l+r)/2;
if(a[m]==n)return m;
else if(a[m] > n)r = m-1;
else if(a[m] < n)l = m+1;
}
return -1;
}
public void show(int a[]){
for(int i:a){
System.out.print(i);
}
System.out.println();
}
}
2. 使用一维数组编码实现一个栈(Stack)类,要求提供以下操作:(1)boolean isEmpty():判断栈当前是否为空;(2)入栈操作void push(obj):把数据元素obj插入堆栈;(3)出栈操作Object pop():出栈,并返回删除的数据元素;(4)Object getTop():取堆栈当前栈顶的数据元素并返回;(5)利用Stack类实现一个方法:输入一个正整数,输出该整数所对应的二进制数。
public class MyStack {
Object[] a;
int basicsize = 50;
int maxsize = 50;
int size;
public MyStack(){
a = new Object[basicsize];
size = -1;
}
public void push(Object o){
a[size+1] = o;
size++;
}
public Object pop(){
Object t = a[size];
a[size] = null;
size--;
if(t instanceof Integer){
int i = ((Integer)t).intValue();
if(i>0){
String s = Integer.toBinaryString(i);
return s;
}
}
return t;
}
public Object getTop(){
return a[0];
}
public boolean isEmpty(){
return size == 0;
}
}
3. 按照要求使用Java编码。
1) 以类型int[][]声明一个叫matrix的二维数组变量,将矩阵初始化为一个5个元素的数组。
2) 以下列方式为matrix的内部元素赋值:matrix从零开始循环到其长度值;例如索引为i,在每次迭代中,将matrix[i]指向一个新的整数数组,其长度为i。然后用索引变量j,对数组中的每一个元素进行循环。在每次内部循环中,将matrix[i][j]赋值为(i*j)。
3) 通过循环打印matrix中的所有元素,结果为:
<>
<0>
<0 2>
<0 3 6>
<0 4 812>
public class DyadicArray{
public void show(){
int[][] matrix = new int[5][5];
int size = 5;
for(int i=0;i<size;i++){
for(int j=0;j<i;j++){
matrix[i][j]=i*j;
}
}
for(int i=0;i<size;i++){
System.out.print("<");
for(int j=0;j<i;j++){
if(0==j)
System.out.print(matrix[i][j]);
else
System.out.print(" "+matrix[i][j]);
}
System.out.println(">");
}
}
}
4. 利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。(7)Matrix transpose():返回当前矩阵的转置矩阵;(8)getMax():返回矩阵中的最大值及其所在行和列;(9)print():以行和列的形式打印出当前矩阵。
public class Matrix {
double[][] matrix;
int row;
int col;
public Matrix(int row, int col) {
if (row < 0 || col < 0) {
System.out.println("Wrong Input!");
} else {
matrix = new double[row][col];
this.row = row;
this.col = col;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
this.matrix[i][j] = 0;
}
}
}
}
public Matrix add(Matrix b) {
if (b.row != this.row || b.col != this.col) {
System.out.println("Can't add!");
return null;
} else {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
this.matrix[i][j] += b.matrix[i][j];
}
}
return this;
}
}
public Matrix multiply(Matrix b) {
double f = 0;
double sum = 0;
int i = 0;
int j = 0;
int k = 0;
if (b.col != this.row || b.row != this.col) {
System.out.println("Can't multiply!");
return null;
} else {
Matrix ma = new Matrix(this.row, b.col);
for (i = 0; i < row; i++) {
for (j = 0; j < b.col; j++) {
for (k = 0; k < col; k++) {
f = this.matrix[i][k] * b.matrix[k][j];
sum += f;
}
ma.matrix[i][j] = sum;
sum = 0;
}
}
return ma;
}
}
public void set(int row, int col, double value) {
if (row > this.row || row < 0 || col > this.col || col < 0) {
System.out.println("Wrong Input!");
} else {
this.matrix[row][col] = value;
}
}
public double get(int row, int col) {
if (row > this.row || row < 0 || col > this.col || col < 0) {
System.out.println("Wrong Input!");
return 0;
} else {
return this.matrix[row][col];
}
}
public int width() {
return col;
}
public int height() {
return row;
}
public void print() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (0 == j)
System.out.print(matrix[i][j]);
else
System.out.print(" " + matrix[i][j]);
}
System.out.println();
}
}
}
5.测试
public class Test {
public static void main(String[] args){
System.out.println("第一题查找");
int a[] = {1,7,3,2,5,4,7};
MyArray m = new MyArray();
m.asort(a);
m.show(a);
System.out.println(m.asearch(a, 3));
System.out.println("第二题栈");
MyStack s = new MyStack();
for(int i=0;i<10;i++){
s.push(i);
}
System.out.println("Top:"+s.getTop());
System.out.println("Pop:"+s.pop());
System.out.println("第三题二维数组");
new DyadicArray().show();
System.out.println("第四题矩阵");
int row=2;
int col=3;
Matrix x = new Matrix(row,col);
double value = 0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
value++;
x.set(i,j,value);
}
}
Matrix y = new Matrix(col,row);
value = 0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
value++;
y.set(j,i,value);
}
}
x.print();
y.print();
x.multiply(y).print();
}
}