周报第二周

12.15(第八天)

矩阵相乘

1 三重循环是多数程序的极限

2 非法输入检查是程序正常运行的基本保障. 如果检查所有的非法输入, 会导致大量代码行, 这在商业代码中是必须的.

3.求解矩阵相乘常用两个办法,蛮力法和分治法,以下使用的主要是蛮力法解决低阶矩阵,分治法较高深,用来解决高阶矩阵,有兴趣可移步(48条消息) Java实现矩阵相乘问题_南 墙-CSDN博客_java矩阵乘法

package basic;
import java.util.Arrays;
public class MatrixMultiplication {
	public static void main(String args[]) {
		matrixMultiplicationTest();
	}
//	实现矩阵相乘的前提条件是前面矩阵的一行与后面矩阵的一列相等
	public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
		int m = paraFirstMatrix.length;
		int n = paraFirstMatrix[0].length;
		int p = paraSecondMatrix[0].length;

//   检查后面矩阵一列元素所含元素是否与前面矩阵的相等
		if (paraSecondMatrix.length != n) {
			System.out.println("The two matrices cannot be multiplied.");
			return null;
		}
//   结果组成的矩阵就是一个m*p阶方阵,在这里实现矩阵相乘
		int[][] resultMatrix = new int[m][p];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < p; j++) {
				for (int k = 0; k < n; k++) {
					resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
				}//蛮力法解决解决低阶矩阵较轻松
			}
		}

		return resultMatrix;
	}
//  初始化前两个矩阵,并调用multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix)方法
//	实现矩阵相乘
	public static void matrixMultiplicationTest() {
		int[][] tempFirstMatrix = new int[2][3];
		for (int i = 0; i < tempFirstMatrix.length; i++) {
			for (int j = 0; j < tempFirstMatrix[0].length; j++) {
				tempFirstMatrix[i][j] = i + j;
			}
		}
		System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));

		int[][] tempSecondMatrix = new int[3][2];
		for (int i = 0; i < tempSecondMatrix.length; i++) {
			for (int j = 0; j < tempSecondMatrix[0].length; j++) {
				tempSecondMatrix[i][j] = i * 10 + j;
			}
		}
		System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));

		int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
		System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
//		第一个矩阵乘以自己本身
		System.out.println("Trying to multiply the first matrix with itself.\r\n");
		tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
		System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
	}

}

运行结果

The first matrix is: 
[[0, 1, 2], [1, 2, 3]]
The second matrix is: 
[[0, 1], [10, 11], [20, 21]]
The third matrix is: 
[[50, 53], [80, 86]]
Trying to multiply the first matrix with itself.

The two matrices cannot be multiplied.
The result matrix is: 
null

12.17(第九天)

while语句

1 while 语句本质上比 for 更基础, 因此可以替代后者. 但 for 在很多时候更方便.

2 break 语句又出现了, 上次是在 switch 语句里. 都是表示跳出当前代码块.

这是使用while语句找出的不超过设置值的最大值的例子

package basic;
public class WhileStatement {
	public static void main(String args[]) {
		whileStatementTest();
	}
//  找出没有超过设置值的最大值
	public static void whileStatementTest() {
		int tempMax = 100;
		int tempValue = 0;
		int tempSum = 0;

//      方法一
		while (tempSum <= tempMax) {
			tempValue++;
			tempSum += tempValue;
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
		}
		tempSum -= tempValue;

		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);

//		方法二
		System.out.println("\r\nAlternative approach.");
//		将值清零
		tempValue = 0;
		tempSum = 0;
		while (true) {
			tempValue++;
			tempSum += tempValue;
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);

			if (tempMax < tempSum) {
				break;
			} 
		}
		tempSum -= tempValue;
		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
	}
}

运行结果

tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

Alternative approach.
tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

12.17(第十天)

综合任务

学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:

进行学生成绩的随机生成, 区间为 [50, 100].
找出成绩最好、最差的同学。但有挂科的同学不参加评比.
10.1 实际代码中,for 和 if 是最常见的, switch 和 while 使用少得多.
10.2 使用了 continue, 它是指继续跳过本次循环后面的代码,直接进入下一次循环. 而 break 是跳出整个循环体.
10.3 为了随机数,迫不得已提前使用了 new 语句生成对象.
10.4 通过数据测试找出程序中的 bug

import java.util.Arrays;
import java.util.Random;

public class Task1 {
	public static void main(String args[]) {
		task1();
	}

	public static void task1() {
	    //在这里定义学生个数,考试科目
		int n = 10;
		int m = 3;
		int lowerBound = 50;
		int upperBound = 100;// 原本100的上限值为了便于测试被设置成了65,
		int threshold = 60;// 重新设置成了100后会有多个成绩最好的和成绩最差的,要处理这个问题		

		// 创建一个随机数对象为学生成绩赋值
		Random tempRandom = new Random();
		int[][] data = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);
			} 
		} 

		System.out.println("The data is:\r\n" + Arrays.deepToString(data));

		// 计算每一个学生的总成绩
		int[] totalScores = new int[n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (data[i][j] < threshold) {
					totalScores[i] = 0;
					break;
				}

				totalScores[i] += data[i][j];
			}
		}
		System.out.println("The total scores are:\r\n" + Arrays.toString(totalScores));

		// 找到成绩最好的和最差的,为此设置了flag指向数组地址
		int tempBestIndex = 0;
		int tempWorstIndex = 0;
		// 
		int tempBestScore = 0;
		int tempWorstScore = m * upperBound + 1;
		for (int i = 0; i < n; i++) {
			// 挂科的同学不能参与评比
			if (totalScores[i] == 0) {
				continue;
			}

			if (tempBestScore < totalScores[i]) {
				tempBestScore = totalScores[i];
				tempBestIndex = i+1;// 这里bug就是因为数组是从0开始的原因,而第几名学生是从1开始的
			}
			
			// 不使用else if的原因是,符合条件的学生可能只有一个,所以既是最好又是最差
			if (tempWorstScore > totalScores[i]) {
				tempWorstScore = totalScores[i];
				tempWorstIndex = i+1;//同上
			}
		}

		//输出最好学生的成绩和输出学生成绩最差的学生
		if (tempBestIndex == 0) {
			System.out.println("Cannot find best student. All students have failed.");
		} else {
			System.out.println("The best student is No." + tempBestIndex + " with scores: "
					+ Arrays.toString(data[tempBestIndex]));
		}

		if (tempWorstIndex == 0) {
			System.out.println("Cannot find worst student. All students have failed.");
		} else {
			System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
					+ Arrays.toString(data[tempWorstIndex]));
		}
	}
}

运行结果

这个运行结果有多个最大值,我没有实现并列第一的结果,之后看看能不能把他实现了

The data is:
[[79, 80, 94], [61, 80, 87], [90, 63, 84], [97, 86, 50], [96, 89, 57], [62, 51, 62], [78, 80, 70], [52, 64, 60], [92, 99, 65], [86, 98, 72]]
The total scores are:
[253, 228, 237, 0, 0, 0, 228, 0, 256, 256]
The best student is No.9 with scores: [86, 98, 72]
The worst student is No.2 with scores: [90, 63, 84]

12.18(第十一天)

今天开始正式进入了数据结构方面,也是我上学期掌握的不够牢靠的知识,正是亡羊补牢的时候。

顺序表

功能:初始化,插入,删除,

package datastructure.list;

public class SequentialList {

	public static final int MAX_LENGTH = 10;
	int length;
	int[] data;

//	无参方法初始化顺序表
	public SequentialList() {
		length = 0;
		data = new int[MAX_LENGTH];
	}

//	有参方法初始化
	public SequentialList(int[] paraArray) {
		data = new int[MAX_LENGTH];
		length = paraArray.length;

		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		}
	}

//	输出顺序表内容
	public String toString() {
		String resultString = "";

		if (length == 0) {
			return "empty";
		}
		for (int i = 0; i < length - 1; i++) {
			resultString += data[i] + ", ";
		}
		resultString += data[length - 1];

		return resultString;
	}

//	重置顺序表
	public void reset() {
		length = 0;
	}

	public int indexOf(int paraValue) {
		int tempPosition = -1;

		for (int i = 0; i < length; i++) {
			if (data[i] == paraValue) {
				tempPosition = i;
				break;
			}
		}
		return tempPosition;
	}

//	插入
	public boolean insert(int paraPosition, int paraValue) {
		if (length == MAX_LENGTH) {
			System.out.println("list full");
			return false;
		}

		if ((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position " + paraPosition + " is out of bounds.");
			return false;
		}

		for (int i = length; i > paraPosition; i--) {
			data[i] = data[i - 1];
		}

		data[paraPosition] = paraValue;
		length++;

		return true;

	}

//	删除
	public boolean delete(int paraPosition) {
		if ((paraPosition < 0) || (paraPosition >= length)) {
			System.out.println("The position " + paraPosition + " is out of bounds.");
			return false;
		} 

		for (int i = paraPosition; i < length - 1; i++) {
			data[i] = data[i + 1];
		}

		length--;

		return true;
	}
	
//	各种方法的实现
	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);
		System.out.println("Initialized, the list is: " + tempFirstList.toString());
		System.out.println("Again, the list is: " + tempFirstList);

		int tempValue = 4;
	   	int tempPosition = tempFirstList.indexOf(tempValue);
	   	System.out.println("The position of " + tempValue + " is " + tempPosition);

	   	tempValue = 5;
	   	tempPosition = tempFirstList.indexOf(tempValue);
	   	System.out.println("The position of " + tempValue + " is " + tempPosition);

	   	tempPosition = 2;
	   	tempValue = 5;
	   	tempFirstList.insert(tempPosition, tempValue);
	   	System.out.println(
	   			"After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

	   	tempPosition = 8;
	   	tempValue = 10;
	   	tempFirstList.insert(tempPosition, tempValue);
	   	System.out.println(
	   			"After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);

	   	tempPosition = 3;
	   	tempFirstList.delete(tempPosition);
	   	System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);

	   	for (int i = 0; i < 8; i++) {
	   		tempFirstList.insert(i, i);
	   		System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);
	   	}
	   	
		tempFirstList.reset();
		System.out.println("After reset, the list is: " + tempFirstList);
	}

}

运行结果

Initialized, the list is: 1, 4, 6, 9
Again, the list is: 1, 4, 6, 9
The position of 4 is 1
The position of 5 is -1
After inserting 5 to position 2, the list is: 1, 4, 5, 6, 9
The position 8 is out of bounds.
After inserting 10 to position 8, the list is: 1, 4, 5, 6, 9
After deleting data at position 3, the list is: 1, 4, 5, 9
After inserting 0 to position 0, the list is: 0, 1, 4, 5, 9
After inserting 1 to position 1, the list is: 0, 1, 1, 4, 5, 9
After inserting 2 to position 2, the list is: 0, 1, 2, 1, 4, 5, 9
After inserting 3 to position 3, the list is: 0, 1, 2, 3, 1, 4, 5, 9
After inserting 4 to position 4, the list is: 0, 1, 2, 3, 4, 1, 4, 5, 9
After inserting 5 to position 5, the list is: 0, 1, 2, 3, 4, 5, 1, 4, 5, 9
list full
After inserting 6 to position 6, the list is: 0, 1, 2, 3, 4, 5, 1, 4, 5, 9
list full
After inserting 7 to position 7, the list is: 0, 1, 2, 3, 4, 5, 1, 4, 5, 9
After reset, the list is: empty

12.19(第十二天)

链表

1 支持与顺序表相同的操作: 初始化、插入、删除等

2.为节点建一个类.

3. 引用与指针的异同. 前者只能使用; 后者可以支持 p ++ 危险操作.

4 引用数据类型的赋值, 都不会产生新的对象空间.

5 链表与线性表在插入、删除时的不同: 前者不移动元素, 只改变引用 (指针)

代码实现

package datastructure.list;

public class LinkedList {
	//内部类
	class Node {
		int data;
		//指向下一个的指针
		Node next;
		
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}
	}
	
	//头指针
	Node header;
	
	//构造一个空链表
	public LinkedList() {
		header = new Node(0);
		//header.next = null; //备用的
	}
	
	//重写Object(所有类的超类)中的toString方法
	public String toString() {
		String resultString = "";

		if (header.next == null) {
			return "empty";
		} 

		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		}

		return resultString;
	}
	
	//重置为空,通过垃圾回收释放空间。
	public void reset() {
		header.next = null;
	}

	//查询,找到给定的值。如果它出现在多个位置,只需返回第一个。-1则表中没有这个值
	public int locate(int paraValue) {
		int tempPosition = -1;

		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		while (tempNode != null) {
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			} 

			tempNode = tempNode.next;
			tempCurrentPosition++;
		} 
		
		return tempPosition;
	}

	//插入,向位置插入值。如果列表已满,则不执行任何操作
	//返回是否成功
	public boolean insert(int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} 

			tempNode = tempNode.next;
		}

		//构造一个新节点
		tempNewNode = new Node(paraValue);

		// 现在连接他们
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;

		return true;
	}
	
	//删除,删除指定位置的值,返回是否成功
	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		}

		Node tempNode = header;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			}

			tempNode = tempNode.next;
		}

		tempNode.next = tempNode.next.next;

		return true;
	}
	
	public static void main(String args[]) {
		LinkedList tempFirstList = new LinkedList();
		System.out.println("Initialized, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.insert(0, i);
		}
		System.out.println("Inserted, the list is: " + tempFirstList.toString());

		tempFirstList.insert(6, 9);

		tempFirstList.delete(4);

		tempFirstList.delete(2);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		tempFirstList.delete(0);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		}
	}
}

运行结果

Initialized, the list is: empty
Inserted, the list is: 4, 3, 2, 1, 0, 
The position 6 is illegal.
Deleted, the list is: 4, 3, 1, 
Deleted, the list is: 3, 1, 
Looped delete, the list is: 1, 
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty

12.20(第13天)

栈及其应用

1 push 和 pop 均只能在栈顶操作.

2 没有循环, 时间复杂度为 O ( 1 ) .

3.任务描述: 检查一个字符串的括号是否匹配. 所谓匹配, 是指每个左括号有相应的一个右括号与之对应, 且左括号不可以出现在右括号右边. 可以修改测试字符串, 检查不同情况下的运行.

由于有一点自己的拙见,就做了一点小修改,希望自己随着能力提升也能考虑的像老师这么全面。

代码

package datastructure.list;

/**
 * 
 * @author goudiyuan
 *
 */
public class CharStack {

	// 定义栈的最大容量
	public static final int MAX_DEPTH = 10;
	// 定义栈的当前位置
	static int depth;
	// 存放数据的地方
	char[] data;

	// 创建一个空栈
	public CharStack() {
		depth = 0;
		data = new char[MAX_DEPTH];
	}

	// 重写Object中的toString方法
	public String toString() {
		String resultString = "";
		for (int i = 0; i < depth; i++) {
			resultString += data[i];
		}
		return resultString;
	}

	// 入栈
	public boolean push(char paraChar) {
		data[depth] = paraChar;
		depth++;
		return true;
	}

	// 出栈
	public char pop() {

		char resultChar = data[depth - 1];
		depth--;
		return resultChar;
	}

	// 接下来是括号匹配
	public static boolean bracketMatching(String paraString) {
		// 步骤 1.通过推送底部的"#"来初始化堆栈。
		CharStack tempStack = new CharStack();
		tempStack.push('#');
		char tempChar, tempPopedChar;
		// 步骤2.处理字符串。对于字符串,length() 是一种方法
		// 而不是成员变量。
		for (int i = 0; i < paraString.length(); i++) {
			tempChar = paraString.charAt(i);

			switch (tempChar) {
			case '(':
			case '[':
			case '{':
				tempStack.push(tempChar);
				break;
			case ')':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '(') {
					return false;
				}
				break;
			case ']':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '[') {
					return false;
				}
				break;
			case '}':
				tempPopedChar = tempStack.pop();
				if (tempPopedChar != '{') {
					return false;
				}
				break;

			default:
			}
		}
		tempPopedChar = tempStack.pop();
		if (tempPopedChar != '#') {
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		CharStack tempStack = new CharStack();

		for (char ch = 'a'; ch < 'm'; ch++) {
			tempStack.push(ch);
			System.out.println("当前栈中元素为:" + tempStack);

			// 我把上面的判断栈满的方法放到了这里
			if (depth == MAX_DEPTH) {
				System.out.println("栈已经满了,不能再进来了");
				break;
			}
		}

		char tempChar;
		for (int i = 0; i < 12; i++) {
			tempChar = tempStack.pop();
			System.out.println("将该元素出栈:" + tempChar);
			System.out.println("当前栈中元素为:" + tempStack);
			// 我把上面的判断栈空的方法放到了这里
			if (depth == 0) {
				System.out.println("栈已经空了,不能再出栈了");
				break;
			}
		}

		boolean tempMatch;
		String tempExpression = "[2+(1-3)]*4";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("表达式: " + tempExpression + "匹配吗?" + tempMatch);

		tempExpression = "() )";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("表达式: " + tempExpression + "匹配吗?" + tempMatch);

		tempExpression = "() () (())";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("表达式: " + tempExpression + "匹配吗?" + tempMatch);

		tempExpression = "({} [] )";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("表达式: " + tempExpression + "匹配吗?" + tempMatch);

		tempExpression = ")(";
		tempMatch = bracketMatching(tempExpression);
		System.out.println("表达式: " + tempExpression + "匹配吗?" + tempMatch);
	}

}

运行结果

当前栈中元素为:a
当前栈中元素为:ab
当前栈中元素为:abc
当前栈中元素为:abcd
当前栈中元素为:abcde
当前栈中元素为:abcdef
当前栈中元素为:abcdefg
当前栈中元素为:abcdefgh
当前栈中元素为:abcdefghi
当前栈中元素为:abcdefghij
栈已经满了,不能再进来了
将该元素出栈:j
当前栈中元素为:abcdefghi
将该元素出栈:i
当前栈中元素为:abcdefgh
将该元素出栈:h
当前栈中元素为:abcdefg
将该元素出栈:g
当前栈中元素为:abcdef
将该元素出栈:f
当前栈中元素为:abcde
将该元素出栈:e
当前栈中元素为:abcd
将该元素出栈:d
当前栈中元素为:abc
将该元素出栈:c
当前栈中元素为:ab
将该元素出栈:b
当前栈中元素为:a
将该元素出栈:a
当前栈中元素为:
栈已经空了,不能再出栈了
表达式: [2+(1-3)]*4 匹配吗? true
表达式: () ) 匹配吗? false
表达式: () () (()) 匹配吗? true
表达式: ({} [] ) 匹配吗? true
表达式: )( 匹配吗? false

12.21(第十四天)

递归 

1.系统会为递归建栈, 这个需要理解一下. 例如, 累加程序, 空间复杂度是 O ( n ) O(n)O(n), 因为只有运行到 paraN = 1 时, 才会弹栈.
2.Hanoi 问题虽然很有名, 但它更多的是对形参/实参的理解, 所以不写在这里给读者添堵. 再说了, 那种极端的例子也不具有代表性.

package datastructure.list;

public class Recursion {
/**
 * 
 * @Title: sumToN 
 * @param paraN
 * @return
 * @author GDY
 * @date 2021-12-21 08:06:58
 */
	
	//利用递归方法计算从0到N的和
	public static int sumToN(int paraN) {
		if(paraN<=0) {
			return 0;
		}
		return sumToN(paraN-1)+paraN;
	}
	
	//利用递归方法解决斐波那契问题
	public static int fibonacci(int paraN) {
		if(paraN<=0) {
			return 0;
		}
		if(paraN==1) {
			return 1;
		}
		return fibonacci(paraN-1)+fibonacci(paraN-2);
	}
	
	//我再加了一个找到数组的最大值
	public  static int Max(int x,int y) {
		return x>y?x:y;
	}
	
	public  static int Max(int x,int y,int z) {
		int m=Max(x,y);
		return m>z?m:z;
	}
	
	public static int Max(int a[]) {
		int m=a[0];
		for(int x:a)
			m=m>x?m:x;
		return m;
	}
	
	public static void main(String[] args) {
		int tempValue =5;
		System.out.println("求和:0到 "+tempValue+" 为 "+sumToN(tempValue));
		
		tempValue=-1;
		System.out.println("求和:0到 "+tempValue+" 为 "+sumToN(tempValue));
		
		for(int i=0;i<10;i++) {
			System.out.println("斐波那契"+i+": "+fibonacci(i));
		}
		
		int a=10,b=25,c=-90;
		int x[]= {98,21,39,28,38,23,38,23,84,29,92};
		System.out.println("两数中最大的是:"+Max(a,b));
		System.out.println("三数中最大的是:"+Max(a,b,c));
		System.out.println("数组中最大的是:"+Max(x));
	}

}

运行结果

求和:0到 5 为 15
求和:0到 -1 为 0
斐波那契0: 0
斐波那契1: 1
斐波那契2: 1
斐波那契3: 2
斐波那契4: 3
斐波那契5: 5
斐波那契6: 8
斐波那契7: 13
斐波那契8: 21
斐波那契9: 34
两数中最大的是:25
三数中最大的是:25
数组中最大的是:98

 

一周小结

随着时间的推移,之后遇到的困难可能就越来越多了,所以我也不采用周报的形式了,之后按照每一天来发,这一周最大的问题还是在链表那里,还有第七天的综合任务那里,希望能够在接下来的学习中能解决这些问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值