上課視頻1:鏈接:https://pan.baidu.com/s/1Ml1yqEr0YpKcxTd5XBVcoA 密碼:qoe1
上課視頻2:鏈接:https://pan.baidu.com/s/1u0X6sKwJKdOISbphuSQaVQ 密碼:4h02
上課練習(xí):
從1+2+3+4+...+100,要求 不用循環(huán)實(shí)現(xiàn),用函數(shù)遞歸。
public static void main(String[] args) {
//1+++5
int i = 1;
int sum = 0;
sum = count(i);
System.out.println(sum);
}
public static int count(int j) {
if (j>5) {
return 0;
}else {
return j+count(++j);
}
}
上課練習(xí)1
求sum= n+(n-1)+(n-2 )+ …….+(n-100)
要求不能用循環(huán)腮敌。只能用函數(shù)實(shí)現(xiàn)。(函數(shù)的遞歸)
package com.lianwei.funcation;
import java.util.Scanner;
public class DiGui02 {
public static void main(String[] args) {
//求sum= (n-0)+(n-1)+(n-2 )+ …….+(n-100)
//要求不能用循環(huán)。只能用函數(shù)實(shí)現(xiàn)糜工。(函數(shù)的遞歸)
System.out.println("請(qǐng)從控制臺(tái)輸入一個(gè)整數(shù):");
int n = new Scanner(System.in).nextInt();
int i = 0;
int sum = 0;
sum = count(i,n);
System.out.println(sum);
}
public static int count(int i,int n) {
if (i > 3) {
return 0;
}else {
return n - i + count(++i, n);
}
}
}
什么是函數(shù)的遞歸
就是函數(shù)自己調(diào)用自己弊添,在一定的情況下不調(diào)用自己。
作業(yè)1
描述:每 3 個(gè)可樂(lè)蓋可兌換 1 一瓶可樂(lè)捌木,求買 n 瓶可樂(lè)最終可喝到多少瓶可樂(lè)油坝。