本帖最後由 李知易 於 2025-5-9 21:01 編輯
請於其中四題挑選三題作答
請於程式碼前標註題號
範例:
(一)- #include<*************>
- using n*******e s*d;
- int main()
- {
- cin >> a;
- cout << a;
- return 0;
- }
複製代碼 (二)- #include<*************>
- using n*******e s*d;
- int main()
- {
- cin >> a;
- cout << a;
- return 0;
- }
複製代碼
題目 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
-------------------------------------------
解答:
(一)- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- re:
- int n;
- cin >> n;
- if(n % 2 == 0)
- cout << n << "為偶數" << endl;
- else
- cout << n << "為奇數" << endl;
- goto re;
- return 0;
- }
複製代碼 (二)
參考之前題目九九乘法表
(三)- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- re:
- int n, sum = 0;
- cin >> n;
- for(int i = 1; i <= n; i++)
- sum += i;
- cout << sum << endl;
- goto re;
- return 0;
- }
複製代碼 (四)- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- re:
- int a, b, c, d, e, f, g, h, i;
- cout << "請輸入五個數:";
- cin >> a >> b >> c >> d >> e;
- f = a > b ? a : b;
- g = f > c ? f : c;
- h = g > d ? g : d;
- i = h > e ? h : e;
- cout << "最大為" << i << endl;
- f = a < b ? a : b;
- g = f < c ? f : c;
- h = g < d ? g : d;
- i = h < e ? h : e;
- cout << "最小為" << i << endl;
- goto re;
- return 0;
- }
複製代碼 (五)挑戰題- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- re:
- string str;
- cin >> str;
- int len = str.length();//int len = str.size();
- cout << len << endl;
- for(int i = len - 1; i >= 0; i--)
- cout << str[i];
- cout << endl;
- goto re;
- return 0;
- }
複製代碼 進階解法:- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- re:
- string str;
- cin >> str;
- int len = str.length();//int len = str.size();
- cout << len << endl;
- reverse(str.begin(), str.end());
- cout << str << endl;
- goto re;
- return 0;
- }
複製代碼 |