標題:
20220521遞迴小測驗
[打印本頁]
作者:
葉桔良
時間:
2022-5-21 13:34
標題:
20220521遞迴小測驗
本帖最後由 葉桔良 於 2022-5-21 14:40 編輯
本帖隱藏的內容需要回復才可以瀏覽
402
import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
...
}
...
}
複製代碼
403
import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
...
}
...
}
複製代碼
408
import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s, c;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
}
...
}
複製代碼
作者:
朱春男
時間:
2022-5-21 13:57
402
import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
int n;
System.out.print("Input n(0<=n<=16):");
n=keyboard.nextInt();
while(n!=999) {
System.out.println(n+"的階乘(尾端遞迴)="+add(n));
System.out.println(n+"的階乘(迴圈)="+round(n));
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
}
}
static int add(int n) {
if(n==1) {
return 1;
}
else {
return n*add(n-1);
}
}
static int round(int n) {
int m=1,o=n;
for(int i=1;i<=o;i++) {
m=m*n;
n--;
}
return m;
}
}
複製代碼
作者:
朱春珠
時間:
2022-5-21 14:04
本帖最後由 朱春珠 於 2022-5-21 14:06 編輯
402
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
int n;
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
while(n!=999)
{
System.out.println(n+"的階乘(尾端遞迴) = "+a(n));
System.out.println(n+"的階乘(迴圈) = "+b(n));
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
}
}
static int a(int n)
{
if(n==1)
return 1;
else
return n*a(n-1);
}
static int b(int n)
{
int x=1;
for(int i=n;i>=1;i--)
{
x=x*n;
n--;
}
return x;
}
}
複製代碼
作者:
朱春男
時間:
2022-5-21 14:07
403
import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
int m,n;
System.out.print("Input m:");
m=keyboard.nextInt();
while(m!=999) {
System.out.print("Input n:");
n=keyboard.nextInt();
System.out.println("Ans(尾端遞迴)="+add(m,n,1));
System.out.println("Ans(迴圈)="+round(m,n,1));
System.out.print("Input m:");
m=keyboard.nextInt();
}
}
static int add(int m,int n,int r) {
if(n==0)
return r;
else
return add(m,n-1,r*m);
}
static int round(int m,int n,int r) {
while(n!=0)
{
r*=m;
n--;
}
return r;
}
}
複製代碼
作者:
朱春男
時間:
2022-5-21 14:13
408
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s, c;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
}
static String reverse(String s) {
if(s.equals(""))
return "";
else
return reverse(s.substring(1))+s.substring(0,1);
}
}
複製代碼
作者:
朱春珠
時間:
2022-5-21 14:15
403
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
int m,n;
System.out.print("Input m: ");
m=keyboard.nextInt();
System.out.print("Input n: ");
n=keyboard.nextInt();
while(m!=999)
{
System.out.println("Ans 的階乘(尾端遞迴) = "+a(n,m));
System.out.println("Ans 的階乘(迴圈) = "+b(n,m));
System.out.print("Input m: ");
m=keyboard.nextInt();
System.out.print("Input n: ");
n=keyboard.nextInt();
}
}
static int a(int n,int m)
{
if(n==1)
return m;
else
return m*a(n-1,m);
}
static int b(int n,int m)
{
int r=1;
for(int i=n;i>=1;i--)
{
r=r*m;
n--;
}
return r;
}
}
複製代碼
作者:
朱春珠
時間:
2022-5-21 14:21
408
import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s, c;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
}
static String reverse(String s)
{
if(s.equals(""))
return "";
else
return reverse(s.substring(1))+s.substring(0,1);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2