標題:
0511模擬考成績
[打印本頁]
作者:
顏子翔
時間:
2019-5-11 15:03
標題:
0511模擬考成績
日期: 2019/5/11
範圍: 103, 203, 303, 403, 503
-----------------------------------
楀諺 4 / 5 (未做完的 303)
秉翰 3 / 5 (未做完的 403 , 503 )
東緯 3 / 5 (未做完的 403 , 503 )
請上述未做完的題目
各員回去後 將每行程式碼打上註解
然後貼上來
下次就由 1.東緯 2.秉翰 上台教學 403跟503
作者:
吳秉翰
時間:
2019-5-11 15:13
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();
while(m!=999)//排除999
{
System.out.print("Input n: ");
n=keyboard.nextInt();
System.out.println("Ans (尾端遞迴): "+powerTail(m,n,1/*1是r初始值*/))/*呼叫函式powerTail*/;
System.out.println("Ans (迴圈): "+powerLoop(m,n,1/*1是r初始值*/))/*呼叫函式powerLoop*/;
System.out.print("Input m: ");
m=keyboard.nextInt();
}
}
static int powerTail(int m, int n, int r)
{
if(n==0)/*當遞減至0,馬上回傳r*/
return r;
else
return powerTail(m, n-1, r*m);
}
/*
powerTail(2,3(n),1(r))
=powerTail(2,2(n-1),2(r*m))
=powerTail(2,1((n-1)-1),2*2((r*m)*m))
=powerTail(2,0(((n-1)-1)-1),2*2*2(((r*m)*m))*m) n變0,馬上回傳r(r=((r*m)*m))*m)
=2*2*2
*/
static int powerLoop(int m, int n, int r)
{
while(n!=0)//當n遞減至0,才回傳r
{
r*=m;
n--;
}
return r;
}
/*
powerLoop(2,3(n),1(r))
=powerLoop(2,2(n-1),2(r*m))
=powerLoop(2,1((n-1)-1),2*2((r*m)*m))
=powerLoop(2,0(((n-1)-1)-1),2*2*2(((r*m)*m))*m) n變0,馬上回傳r(r=((r*m)*m))*m)
=2*2*2
*/
}
複製代碼
作者:
鄭楀諺
時間:
2019-5-11 15:20
import java.util.Scanner; //import Scanner this function
public class JPA03 { //make a class "JPA03"
public static void main(String[] args) { //make a function "main"
int i, sum; //declare integers
System.out.printf("1~1000中的完美數有: "); //print out "some things"
for(i=1; i<=1000; i++){ //the loop for i to run "1 to 1000"
sum=0; //clear integer sum
for(int j=1; j<i; j++){ //the loop for getting all common factors
if(i%j==0) //to check if the integer j is the common factor of i
sum+=j; //if so, then add j to sum
}
if(i==sum){ //to check is i equal to j
System.out.printf("%d ",i); //if so, then print the number
}
}
System.out.printf("\n"); //print next line(enter)
}
}
複製代碼
作者:
吳秉翰
時間:
2019-5-11 15:34
public class JPA05 {
final static int ROW = 2;
final static int COL = 3;
public static void main(String args[]) {
int A[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
int B[][] = { { 7, 8, 9 }, { 10, 11, 12 } };
int C[][] = new int[ROW][COL];//[2][3]2*3
System.out.printf("陣列A的內容為(2x3):\n");
show(A);//顯示
System.out.printf("\n陣列B的內容為(2x3):\n");
show(B);//顯示
add(A, B, C);//呼叫相加函式
System.out.printf("\n陣列A+B=C,陣列C的內容為(2x3):\n");
show(C);//顯示
}
public static void add(int a[][], int b[][], int c[][]) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++)
c[i][j] = a[i][j] + b[i][j];
//ex.c陣列第一列的第一個=a陣列第一列的第一個+b陣列第一列的第一個
}
}
public static void show(int x[][]) {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++)
System.out.printf("%02d "/*"02d"指在不滿兩位的數字前加0*/, x[i][j]);
System.out.println();
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2