2013年2月8日 星期五

TQC+ JAVA 遞迴程式設計 401 ~ 410

TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

/* TQC+ JAVA6 - 401 */
import java.util.Scanner;
public class JPA401 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
//一開始先輸入一個數字
System.out.print("Input n (0<=n<=16):");
int i = keyboard.nextInt();
//只要不等於結束需要的999便可進入迴圈
while(i!=999){
//進入迴圈後再次檢查是否有在0~16的範圍中
if(i<=16&&i>=0)
System.out.println(i+"的階乘 = "+R(i));
//顯示完後,再請使用者輸入一個數字
System.out.print("Input n (0<=n<=16):");
i = keyboard.nextInt();
}
}
//遞迴計算方法
static int R(int i ){
if(i==0)//當傳入為零時,則回傳1
return 1;
else //每次皆會乘上自己的方法,並且傳入i-1的值進入
return i*R(i-1);
}
}
/* TQC+ JAVA6 - 402 */
import java.util.Scanner;
public class JPA402 {
public static void main(String[] args) {
int size = 0;
do{
System.out.print("Input n ( 0 <= n <= 16 ):");
size = new Scanner(System.in).nextInt();
}while(size>16&&size!=999||size<0&&size!=999);
while(size!=999){
//迴圈方法
int L = facLoop(size);
System.out.printf("%d的階乘(迴圈) = %d\n",size,L);
//尾端遞迴
int T = facTail(size,1);
System.out.printf("%d的階乘(尾遞迴) = %d\n",size,T);
do{
System.out.print("Input n ( 0 <= n <= 16 ):");
size = new Scanner(System.in).nextInt();
}while(size>16&&size!=999||size<0&&size!=999);
}
}
//迴圈方法:使用for-loop
public static int facLoop(int a){
int sum = 1;
for(int b=1;b<=a;b++)
sum = b*sum;
return sum;
}
//尾端遞迴:不斷呼叫自己的方法,進行運算
public static int facTail(int a,int b){
if(a==1)
return b;
else
return facTail(a-1,a*b);
}
}
/* TQC+ JAVA6 - 403 */
import java.util.Scanner;
public class JPA403 {
public static void main(String[] args) {
int m ,n;
System.out.print("Input m :");
m = new Scanner(System.in).nextInt();
while(m!=999){
System.out.print("Input n :");
n = new Scanner(System.in).nextInt();
int ans0 = 1;
int ans1 = facTail(m,n,ans0);
System.out.println("Ans(尾端遞迴):"+ans1);
int ans2 = facLoop(m,n);
System.out.println("Ans(迴圈):"+ans2);
System.out.print("Input m :");
m = new Scanner(System.in).nextInt();
}
}
//迴圈
public static int facLoop(int m,int n){
int sum = 1;//累積要初始化為1
for(;n>0;n--)
sum = m*sum;
return sum;
}
public static int facTail(int m,int n,int sum){
if(n==0)
return sum;
else
return facTail(m,n-1, sum*m);
//不斷傳入三個變數,第一個變數用來儲存要乘的數,第二個數用來計算還要乘幾次,第三個數用來累積到目前為止的乘積和
}
}
/* TQC+ JAVA6 - 404 */
import java.util.Scanner;
public class JPA404 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
//先請使用者輸入m值
System.out.print("Input m :");
int m = keyboard.nextInt();
//迴圈判斷,m要不等於999才可進入
while(m!=999){
//進入迴圈後再請使用者輸入n值
System.out.print("Input n :");
int n = keyboard.nextInt();
//進行最大公因數計算
System.out.println(R(m,n));
//迴圈最後在要求使用者輸入下一個迴圈需要的m,若此m在進入下一個迴圈時條件不過,則不進入迴圈了
System.out.print("Input m :");
m = keyboard.nextInt();
}
}
//gcd求最大公因數演算法
static int R(int m,int n){
if(m%n==0)//如果mod為零時,則找到最大公因數,回傳值
return n;
else {//如果mod數不為零時,則把mod得到的數寫入m,再次進行gcd
int tem = n;
n = m%n;
m = tem;
return R(m,n);
}
}
}
/* TQC+ JAVA6 - 405 */
import java.util.Scanner;
public class JPA405 {
static int sum = 0;
public static void main(String[] args) {
System.out.print("Input the number :");
int m = new Scanner(System.in).nextInt();
int ans =sum2(m);
System.out.printf("Ans:%d",ans);
}
//sum2方法,按照題目給的提示,將程式代入即可
public static int sum2(int m) {
if(m==1)
return 2;
else {
//有類似尾端遞迴的概念
sum = sum + sum2(m-1)+2*m;
return sum;
}
}
}
/* TQC+ JAVA6 - 406 */
import java.util.Scanner;
public class JPA406 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s;
System.out.print("Input a String :");
s = keyboard.nextLine();
System.out.println(s + " has " + countA(s) + " As");
System.out.print("Input a String :");
s = keyboard.nextLine();
System.out.println(s + " has " + countA(s) + " As");
}
//重點在此計算A個數的方法
public static int countA(String str) {
//當傳入的字串為空字串時,回傳A的數另為0
if(str.equals(""))
return 0;
//擷取字串的第一個字元,如果為A,則加1,並且利用substring讀出位置1之後的所有字串再傳入countA
if(str.substring(0,1).equals("A"))
return 1+countA(str.substring(1));
//如果第一個字串不等於A,直接利用substring讀出位置1之後的所有字串傳入countA
else
return countA(str.substring(1));
}
}
/* TQC+ JAVA6 - 407 */
import java.util.Scanner;
public class JPA407 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s;
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.printf("迴圈:%d\n", sumLoop(s));
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.printf("迴圈:%d\n", sumLoop(s));
}
//迴圈方法
static int sumLoop(String s) {
int ln = s.length();
int[] num = new int[ln];
for(int i=0;i<ln;i++)
num[i]=Integer.parseInt(s.substring(i, i+1));
int sum =0;
for(int i=0;i<ln;i++)
sum+=num[i];
return sum;
}
//尾端遞迴
static int sumTail(String s,int i){
if(s.equals(""))//如果傳入的字串為空,回傳累加的i值
return i;
else//字串不為空時,則利用substring讀取位置1之後的字串,並傳入
//後面的i值則將字串的第一個字元讀出轉成數字並累加
return sumTail(s.substring(1),i+Integer.parseInt(s.substring(0, 1)));
}
}
/* TQC+ JAVA6 - 408 */
import java.util.Scanner;
public class JPA408 {
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(""))//如果傳入字串為空,則回傳s字串
return s;
else//將字串的第一個字元從後方累加上去,然後傳入的字串則是透過substring讀出位置1之後的所有字串並傳入reverse
return reverse(s.substring(1))+s.substring(0, 1);
}
}
/* TQC+ JAVA6 - 409 */
import java.util.Scanner;
public class JPA409 {
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.print("Input a character: ");
c = keyboard.nextLine();
System.out.printf("%s\n", removeChar(s, c));
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.print("Input a character: ");
c = keyboard.nextLine();
System.out.printf("%s\n", removeChar(s, c));
}
static String removeChar(String s,String c)
{
if(s.equals(""))//如果傳入的字串為空的話,則回傳空字串
return "";
if(s.substring(0, 1).equals(c))//如果傳入字串的第一個字元等於要刪除的字元
//則利用substring讀取位置1之後的所有字串並再次傳入removeChar
return removeChar(s.substring(1),c);
else//如果不等於要刪除的字元,則補回至原本的字串中
return s.substring(0, 1)+removeChar(s.substring(1),c);
}
}
/* TQC+ JAVA6 - 410 */
import java.util.Scanner;
public class JPA410 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
String s, c1, c2;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.print("Input a character: ");
c1 = keyboard.nextLine();
System.out.print("Input another character: ");
c2 = keyboard.nextLine();
System.out.printf("%s\n", replace(s, c1, c2));
}
static String replace(String s,String c1,String c2){
if(s.equals(""))//如果傳入的字串為空的話,則回傳空字串
return "";
if(s.substring(0, 1).equals(c1))//如果讀出的字元等於要取代的字元,則將新的字元加上去
return c2+replace(s.substring(1),c1,c2);
else//如果不等於的話,補回至原本的字串
return s.substring(0, 1)+replace(s.substring(1),c1,c2);
}
}


TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言