標題:
405-遞迴函數
[打印本頁]
作者:
許承鴻
時間:
2018-8-14 18:13
標題:
405-遞迴函數
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
...
}
...
}
複製代碼
作者:
張閎鈞
時間:
2018-8-14 19:31
package hi87;
import java.util.Scanner;
public class hi788 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
System.out.print("Input m:");
int m=keyboard.nextInt();
System.out.print(f(m));
}
static int f(int m)
{
if(m==1)
{
return 2;
}
else
{
return f(m-1)+2*m;
}
}
}
複製代碼
作者:
巫沛庭
時間:
2018-8-14 19:38
package text;
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
System.out.print("Input the number n: ");
int num=keyboard.nextInt();
System.out.println("Ans: "+f(num));
}
static int f(int n)
{
if(n==1)
{
return 2;
}
else
return f(n-1)+2*n;
}
}
複製代碼
作者:
巫晉宇
時間:
2018-8-18 14:41
package wwe;
import java.util.Scanner;
public class JPA04 {
static Scanner k = new Scanner(System.in);
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
System.out.println("Input the number n: ");
int n=k.nextInt();
System.out.println("Ans:"+s(n));
}
static int s(int n)
{
if(n==1)
{
return 2;
}
else
{
return s(n-1)+2*n;
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2