--- tags: Cmoney_Java題目 --- Java_Cmoney_st106 ===   1.需要的 function --- 1.1 列印出第幾天有幾隻公母 --- 先將公母的個數公式算出來, 再將 male, female 變數,更新成這輪繁殖的結果, 這樣下輪迴圈,就會用上次繁殖結果的個數,繼續計算新的繁殖個數 ```java= public static void numbers(int male, int female, int day) { int[] numbers = new int[2]; for (int i = 0; i < day; i++) { numbers[0] = male * 2 + female; numbers[1] = male; male = numbers[0]; female = numbers[1]; } System.out.println(numbers[0] + " " + numbers[1]); } ``` 2.主程式 --- 直接把`sc.nextInt()`塞進去當引數 ```java= public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { numbers(sc.nextInt(), sc.nextInt(), sc.nextInt()); } } ``` 3.完整程式 --- ```java= import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int[] production = production(sc.nextInt(),sc.nextInt(),sc.nextInt()); System.out.println(production[0]+" "+production[1]); } } public static int[] production(int male, int female, int day) { int[] production = new int[2]; for (int i = 0; i < day; i++) { production[0] = 2*male+female; production[1] = male; male = production[0]; female = production[1]; } return production; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up