Board logo

標題: 05/09小考專區 [打印本頁]

作者: 李知易    時間: 7 天前 19:06     標題: 05/09小考專區

本帖最後由 李知易 於 2025-5-9 21:01 編輯

請於其中四題挑選三題作答
請於程式碼前標註題號

範例:

(一)
  1. #include<*************>
  2. using n*******e s*d;
  3. int main()
  4. {
  5.     cin >> a;
  6.     cout << a;
  7.     return 0;
  8. }
複製代碼
(二)
  1. #include<*************>
  2. using n*******e s*d;
  3. int main()
  4. {
  5.     cin >> a;
  6.     cout << a;
  7.     return 0;
  8. }
複製代碼
[attach]21029[/attach]

題目 1:判斷奇偶數
--------------------------------------
輸入一個數,判斷其為奇數或偶數。
輸入範例: 3
輸出範例: 3為奇數
--------------------------------------

題目 2:九九乘法表
----------------------------------
印出一九九乘法表(1x1 ~ 9x9)
如下:
1x1=1 1x2=2 ... 1x9=9
2x1=2 ...
----------------------------------

題目 3:計算 1 到 N 的總和
----------------------------------------------------
讓使用者輸入一個正整數 N,並輸出從 1 到 N 的總和。
輸入範例: 10
輸出範例: 總和為 55 (1+2+3+...+10= 55)
----------------------------------------------------

題目 4:最大值與最小值
-------------------------------------------
輸入五個整數,找出其中的最大值與最小值。
輸入範例: 1 3 5 2 4
輸出範例: 最大值為 5
           最小值為 1
-------------------------------------------
四題挑三題做
挑戰提:
題目 5:字串反轉
-------------------------------------------
輸入一個字串,輸出其長度並將其反轉。
輸入範例: applebanana
輸出範例: 11
               ananabelppa
-------------------------------------------
解答:
(一)
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     re:
  6.     int n;
  7.     cin >> n;
  8.     if(n % 2 == 0)
  9.         cout << n << "為偶數" << endl;
  10.     else
  11.         cout << n << "為奇數" << endl;
  12.     goto re;
  13.     return 0;
  14. }
複製代碼
(二)
參考之前題目九九乘法表

(三)
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     re:
  6.     int n, sum = 0;
  7.     cin >> n;
  8.     for(int i = 1; i <= n; i++)
  9.         sum += i;
  10.     cout << sum << endl;
  11.     goto re;
  12.     return 0;
  13. }
複製代碼
(四)
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     re:
  6.     int a, b, c, d, e, f, g, h, i;
  7.     cout << "請輸入五個數:";
  8.     cin >> a >> b >> c >> d >> e;
  9.     f = a > b ? a : b;
  10.     g = f > c ? f : c;
  11.     h = g > d ? g : d;
  12.     i = h > e ? h : e;
  13.     cout << "最大為" << i << endl;
  14.     f = a < b ? a : b;
  15.     g = f < c ? f : c;
  16.     h = g < d ? g : d;
  17.     i = h < e ? h : e;
  18.     cout << "最小為" << i << endl;

  19.     goto re;
  20.     return 0;
  21. }
複製代碼
(五)挑戰題
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     re:
  6.     string str;
  7.     cin >> str;
  8.     int len = str.length();//int len = str.size();
  9.     cout << len << endl;
  10.     for(int i = len - 1; i >= 0; i--)
  11.         cout << str[i];
  12.     cout << endl;
  13.     goto re;
  14.     return 0;
  15. }
複製代碼
進階解法:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     re:
  6.     string str;
  7.     cin >> str;
  8.     int len = str.length();//int len = str.size();
  9.     cout << len << endl;
  10.     reverse(str.begin(), str.end());
  11.     cout << str << endl;
  12.     goto re;
  13.     return 0;
  14. }
複製代碼

作者: 張詠博    時間: 7 天前 19:24

此帖僅作者可見
作者: 張詠博    時間: 7 天前 19:26

此帖僅作者可見
作者: 陳頎諠    時間: 7 天前 19:26

此帖僅作者可見
作者: 陳頎諠    時間: 7 天前 19:29

此帖僅作者可見
作者: 鄭子宸    時間: 7 天前 19:30

此帖僅作者可見
作者: 鄭子宸    時間: 7 天前 19:33

此帖僅作者可見
作者: 張詠博    時間: 7 天前 19:33

此帖僅作者可見
作者: 洪睿輿    時間: 7 天前 19:34

此帖僅作者可見
作者: 朱立人    時間: 7 天前 19:35

此帖僅作者可見
作者: 陳頎諠    時間: 7 天前 19:40

此帖僅作者可見
作者: 鄭子宸    時間: 7 天前 19:42

此帖僅作者可見
作者: 楊昀瑾    時間: 7 天前 19:47

此帖僅作者可見
作者: 朱立人    時間: 7 天前 20:14

此帖僅作者可見
作者: 黃品錡    時間: 7 天前 21:05

此帖僅作者可見
作者: 黃品錡    時間: 7 天前 21:06

此帖僅作者可見
作者: 江立辰    時間: 6 天前 07:34

此帖僅作者可見
作者: 黃品錡    時間: 3 天前 19:44

此帖僅作者可見




歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2