碼雲地址“https://gitee.com/”
7-1 輸出數組元素 (15 分)
本題要求編寫程序,對順序讀入的n個整數,順次計算后項減前項之差,並按每行三個元素的格式輸出結果。
輸入格式:
輸入的第一行給出正整數n(1<n≤10)。隨后一行給出n個整數,其間以空格分隔。
輸出格式:
順次計算后項減前項之差,並按每行三個元素的格式輸出結果。數字間空一格,行末不得有多余空格。
輸入樣例:
10
5 1 7 14 6 36 4 28 50 100
輸出樣例:
-4 6 7
-8 30 -32
24 22 50
代碼:
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int[] a = new int[n]; int i=0; int cnt=0; for(i=0;i<n;i++){ a[i]=reader.nextInt(); } for (i = 0; i < n - 1; i++){ a[i] = a[i + 1] - a[i]; } for (i = 0; i < n - 1; i++){ if (i == 0){ System.out.printf("%d", a[0]); } else if (cnt == 3){ System.out.printf("\n"); System.out.printf("%d", a[i]); cnt = 0; } else{ System.out.printf(" %d", a[i]); } cnt++; } } }
程序設計思路:
定義數組,for循環依次輸出后一個數減去前一個數的差,並且利用cnt記數,三個一輪回,最后輸出所有數。
運用到的知識點:定義數組,for循環
運行結果:

7-2 字符串逆序 (15 分)
輸入一個字符串,對該字符串進行逆序,輸出逆序后的字符串。
輸入格式:
輸入在一行中給出一個不超過80個字符長度的、以回車結束的非空字符串。
輸出格式:
在一行中輸出逆序后的字符串。
輸入樣例:
Hello World!
輸出樣例:
!dlroW olleH
代碼:
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner sca = new Scanner(System.in); String str = sca.nextLine(); StringBuffer sb = new StringBuffer(str); System.out.print(sb.reverse().toString()); } }
程序設計思路:
先創建對象,然后利用對象使用reverse()方法,將該對象實體中的字符串進行逆序輸出,並返回當前對象。
運用到的知識點:StringBuffer 對象,StringBuffer 類的常用方法
運行結果:

7-3 選擇法排序 (20 分)
本題要求將給定的n個整數從大到小排序后輸出。
輸入格式:
輸入第一行給出一個不超過10的正整數n。第二行給出n個整數,其間以空格分隔。
輸出格式:
在一行中輸出從大到小有序的數列,相鄰數字間有一個空格,行末不得有多余空格。
輸入樣例:
4
5 1 7 6
輸出樣例:
7 6 5 1
代碼:
import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int[] a = new int[n]; int x=0; for(int i=0;i<n;i++){ a[i]=reader.nextInt(); } for(int i=0;i<n;i++){ for(int j=1;j<n;j++){ if(a[j]>a[j-1]){ x=a[j]; a[j]=a[j-1]; a[j-1]=x; } } } for(int i=0;i<n;i++){ System.out.print(a[i]); if(i!=n-1){ System.out.print(" "); } } } }
程序設計思路:
定義數組,數組中的數兩兩比較,第一個數比第二個數小,就交換兩個數,一次輪流比較,最后利用for循環輸出。
運用到的知識點:定義數組,for循環 選擇排序
運行結果:

7-4 猜數字 (20 分)
一群人坐在一起,每人猜一個 100 以內的數,誰的數字最接近大家平均數的一半就贏。本題就要求你找出其中的贏家。
輸入格式:
輸入在第一行給出一個正整數N(≤104)。隨后 N 行,每行給出一個玩家的名字(由不超過8個英文字母組成的字符串)和其猜的正整數(≤ 100)。
輸出格式:
在一行中順序輸出:大家平均數的一半(只輸出整數部分)、贏家的名字,其間以空格分隔。題目保證贏家是唯一的。
輸入樣例:
7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62
輸出樣例:
22 Amy
代碼片段:
char name[n][9]; int num[n]; for(int i=0;i<n;i++){ sum+=num[i]; } int ave=sum/n/2,min=999; System.out.println("%d",ave); for(int i=0;i<n;i++){ if(abs(num[i]-ave)<min){ min=abs(num[i]-ave); t=i; } } System.out,println(" %s",name[t]);
運行結果:
還未完成,時間不夠,敬請期待下次發文。
| 學習內容 | 代碼(行) | 博客(字) |
| 第一次過程性考核 | 90 | 356 |
| 第二次過程性考核 | 100 | 300 |
| 第三次過成性考核 | 120 | 400 |
| 數組 | 95 | |
| 常用實用類 | 89 | |
| 總計 | 494 | 1056 |
階段性總結:電腦暫時安裝不上Java,不方便練習。這階段的練習和上階段的練習相比,這階段的學習思路比以前清晰,但是還有很多不足,考試的時候也經常出錯,漏洞百出。
經常性完不成,以后還需要多多練習。java課程已經接近尾聲,我覺得首要任務是熟練,熟悉掌握代碼方法。
代碼詳見“碼雲”
——————https://gitee.com/girlchujiu/codes——————
