递归:重复调用自身函数,一般用于某功能重复使用,一般由上至下解决问题。
举个栗子~从1加到100,重复利用相加的功能
package paixu;
public class test {
public static void main(String[] args) {
System.out.println(Getnum(100));;
}
public static int Getnum(int num){
if(num==1){
return 1;
}
return num+Getnum(num-1);
}
}
题如图片:代码如下:
import java.util.Scanner;
public class drink {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int T = scan.nextInt();
System.out.println(digui(T));
}
}
private static int digui(int T) {
int a=0;
int b;
if(T==2||T==3){
return 1;
}
// TODO Auto-generated method stub
while(T>2){
a+=T/3;
b=T%3;
T=T/3+b;
}if(T==2){
a++;
}
return a;
}
}