2013年2月8日 星期五

TQC+ JAVA 陣列設計能力 501 ~ 510

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

/* TQC+ JAVA6 - 501 */
import java.util.Scanner;
public class JPA501 {
public static void main(String[] args) {
int[] n = new int[10];
Scanner sc = new Scanner(System.in);
int count=0,sum=0;
System.out.println("請輸入10個整數:");
//使用迴圈計算陣列
for(int a=0;a<10;a++) {
System.out.print("第"+(a+1)+"個整數:");
//a變數由迴圈來控制
n[a]=sc.nextInt();
//判斷式,如果大於60,則將此數加入sum
if(n[a]>60) {
count++;
sum+=n[a];
}
}
System.out.printf("陣列中大於60的有%d個\n", count);
System.out.printf("總合為%d\n", sum);
System.out.printf("平均值為%f\n", (double)sum/count);
}
}
/* TQC+ JAVA6 - 502 */
import java.util.Scanner;
public class JPA502 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
System.out.print("請輸入學生人數:");
Scanner sc = new Scanner(System.in);
int poe = sc.nextInt();
float sum = 0;
float[] ps = new float[poe];
//迴圈次數決定可輸入幾個學生成績,而迴圈次數由使用者輸入
for(int a =0;a<poe;a++){
System.out.print("第"+(a+1)+"個學生的成績:");
ps[a]=sc.nextFloat();
sum +=ps[a];
}
System.out.println("人數:"+poe);
System.out.println("總分:"+sum);
System.out.println("平均:"+sum/poe);
}
}
/* TQC+ JAVA6 - 503 */
public class JPA503 {
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];
System.out.printf("陣列A的內容為(3x3):\n");
show(A);
System.out.printf("\n陣列B的內容為(3x3):\n");
show(B);
add(A, B, C);
System.out.printf("\n陣列A+B=C,陣列C的內容為(3x3):\n");
show(C);
}
//相加陣列的方法
public static void add(int[][] A,int[][] B,int[][] C){
//第一個變數和第二個變數為導入方法用,第三個變數則是用來儲存相加後的陣列
for(int b=0;b<2;b++) {
for(int a=0;a<3;a++)
C[b][a]=A[b][a]+B[b][a];
}
}
//陣列顯示方法,透過兩層的for-loop可以將它print出來
public static void show(int[][] s) {
for(int b=0;b<2;b++){
for(int a=0;a<3;a++)
System.out.printf("%02d ",s[b][a]);
System.out.println("");
}
}
}
/* TQC+ JAVA6 - 504 */
public class JPA504 {
public static void main(String[] args) {
int[] n = new int[10];
//初始化前兩個數
n[0]=0;
n[1]=1;
//費式數列前十個
for(int a=2;a<10;a++)
n[a]=n[a-1]+n[a-2];
//陣列的index由2開始,其目的在於相加前面兩個index=1 and 0
for(int a=0;a<10;a++)
System.out.println(n[a]);
}
}
/* TQC+ JAVA6 - 505 */
public class JPA505 {
public static void main(String[] argv) {
String[] data = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
System.out.print("反轉陣列資料之前: ");
for(int a=0;a<10;a++)
System.out.print(data[a]+" ");
data=reverse(data);
System.out.print("\n反轉陣列資料之後: ");
for(int a=0;a<10;a++)
System.out.print(data[a]+" ");
}
//反轉方法
//其翻轉方法主要是,宣告一個新的陣列,將原陣列寫到暫時的陣列中,並且用倒著的方法來寫,最後將這個暫時的陣列傳回main方法之中
public static String[] reverse(String[] s) {
String[] temp = new String[s.length];
int b=9;
for(int a=0;a<10;a++) {
temp[b]=s[a];
b--;
}
return temp;
}
}
/* TQC+ JAVA6 - 506 */
public class JPA506 {
public static void main(String[] argv) {
int sum =0;
//觀察此三維陣列是由四維包二維再包三維的
int A[][][] = { {{1,2,3}, {4,5,6}},
{{7,8,9}, {10,11,12}},
{{13,14,15},{16,17,18}},
{{19,20,21},{22,23,24}}};
//透過三個for-loop迴圈來相加
for(int a=0;a<4;a++)
for(int b=0;b<2;b++)
for(int c=0;c<3;c++)
sum+=A[a][b][c];
System.out.printf("sum = %d\n", sum);
}
}
/* TQC+ JAVA6 - 507 */
public class JPA507 {
public static void main(String[] argv) {
int hours = 0; //停車時數
hours = 2;
park(hours);
System.out.println("--------------------");
hours = 3;
park(hours);
System.out.println("--------------------");
hours = 5;
park(hours);
System.out.println("--------------------");
hours = 8;
park(hours);
}
//計算停車費用方法
public static void park(int hours) {
int[] hourTable = {0, 2, 4, 6}; //時段陣列
int[] feeTable = {30, 50, 80, 100}; //時段費率陣列
int fee = 0; //總停車費用
System.out.println("停車時數:" + hours + "小時");
for(int a = 3 ;a>=0;a--){
//透過迴圈的方式,使用漏斗的方法,若符合條件則進入if判斷式
if(hours>hourTable[a]){
//計算金額。原本的+(時數-非屬於此時段的時間)*該時段費率
fee=fee+(hours-hourTable[a])*feeTable[a];
//設定剩下的時間
hours=hourTable[a];
}
}
System.out.println("應繳費用:" + fee + "元整");
}
}
/* TQC+ JAVA6 - 508 */
//泡泡排序法
public class JPA508 {
public static void main(String[] args){
int data[] = {2,4,3,5,7,6,9,1};//未排序資料
//先取出陣列長度
int LN = data.length-1;
for(int i=0;i<LN;i++){
for(int j = 0;j<LN;j++){
//如果後面那位數字小於前面那數字則交換
if(data[j]>data[j+1]){
int temp = data[j+1];
data[j+1] = data[j];
data[j] = temp;
}
}
//每做完一次排序,便顯示出結果
for(int x=0;x<=LN;x++)
System.out.print(data[x]+" ");
System.out.println("");
}
}
}
/* TQC+ JAVA6 - 509 */
//選擇排序法
public class JPA509{
static int t = 0;
public static void main(String[] argv) {
int[] data = {1, 3, 2, 5, 4, 6};
sort(data);
}
//設計一個選擇排序法方法
public static void sort(int[] d){
//先取得長度,比較次數為數量-1
int LN = d.length-1;
int i,j,min;
for(i = 0 ; i < LN ; i++){
//先將一開始的位置寫入
min=i;
for(j=(i+1);j<=LN;j++)
if(d[j]<d[min])//如果比較到比它更小的,則將最小的寫入min
min=j;
int temp = d[i];
d[i] = d[min];
d[min]=temp;
for(int k=0;k<=LN;k++)
System.out.print(d[k]+" ");
System.out.println();
}
}
}
/* TQC+ JAVA6 - 510 */
import java.util.Scanner;
public class JPA510 {
public static Scanner targetboard = new Scanner(System.in);
static int time = 0;
public static void main(String[] argv) {
search();
time = 0;
search();
}
//搜尋方法,主要是在做排版的工作
public static void search() {
int[] data = {5, 9, 13, 15, 17, 19, 25, 30, 45}; // 已排序資料
System.out.print("請輸入要找尋的資料:");
int target = targetboard.nextInt();
int ans = binary_search(data,target,data.length);
if(ans==-1)
System.out.println("經過 "+time+" 次的尋找\n"+target+"不在陣列中");
else
System.out.println("經過 "+time+" 次的尋找\n您要的資料在陣列中的第"+ans+"個位置");
}
//二分法搜尋方法,真正在搜尋的地方
//傳入值有三個,陣列,目標值,陣列長度
static int binary_search(int[] data,int target, int max){
int middle, left, right;
left = 0; right = max-1; // 設定啟始搜尋範圍: 左邊界及右邊界(右邊界由最大值減1得到)
while (left <= right){
time++;
middle = (left + right) / 2;// 找出中間位置
System.out.printf("尋找區間: %d(%s)..%d(%s),中間: %d(%s)\n",
left,String.valueOf(data[left]),
right,String.valueOf(data[right]),
middle,String.valueOf(data[middle]));
if (target == data[middle])
return middle; // 找到資料, 傳回找到之位置
// 調整搜尋範圍
if (target < data[middle]) // 往左半邊找 (調整右邊界)
right = middle - 1;
else // 往右半邊找 (調整左邊界)
left = middle + 1;
}
return -1; // 沒找到資料, 傳回 -1
}
}


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

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

沒有留言:

張貼留言