{%hackmd hackmd-dark-theme %}
<style>
h2 {
color: rgb(153, 50, 204) !important;
}
</style>
:::spoiler **Python**
## HW3
請實作一程式,此程式可以要求使用者輸入10組資料。每次所輸入一組資料的內容如下所示:
O d1 d2
O代表指令動作
當O為1時,則請由小到大輸出d1以及d2所有小於100的共同因數
當O為2時,則請由小到大輸出d1以及d2所有小於100的共同倍數,若無,則印出None
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
| 1 10 20 | 1 2 5 10 |
| 2 50 60 | None |
| 2 30 45 | 90 |
```python=
for i in range(0,10):
o,d1,d2 = input().split()
o=int(o)#改變型態
d1=int(d1)#改變型態
d2=int(d2)#改變型態
if(o==1):
i=0
while(i<100):#因數
i=i+1
if(d1 % i==0 and d2 % i==0):
print(i)
else:
i=0
j=0
while(i<100):
i=i+1
if(i%d1==0 and i%d2==0):
j+=1
print(i)
if(j==0):
print("None")
```
###### tags: `Program`
## HW4
請實作一程式,此程式可以要求使用者輸入三行字串,當輸入完一行字串後,請計算在此字串中字元a~zA~Z所出現的頻率次數,請輸出頻率次數非零的字元(請依a~zA~Z順序輸出)以及其頻率次數。
請用連續星星符號個數來代表頻率次數,如出現N次,請以N個星星符號表示。
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
| Hello World |d * e * l *** o ** r * H * W * |
| Good Morning, everyone. |d * e *** g * i * n *** o **** r ** v * y * G * M * |
| byebye |b ** e ** y ** |
```python=1
for _ in range(3):
array1 = [0] * 26
array2 = [0] * 26
for x in input():
if x.islower():#檢查是否為小寫字母
array1[ord(x) - 97] += 1
elif x.isupper():#檢查是否為大寫字母
array2[ord(x) - 65] += 1
for i, cnt in enumerate(array1):
if cnt > 0:
print(chr(i + 97), '*' * cnt, end=' ')
for i, cnt in enumerate(array2):
if cnt > 0:
print(chr(i + 65), '*' * cnt, end=' ')
```
## HW5
請實作一程式,此程式可以要求使用者輸入一整數N,若N>0,則接下來會請使用者輸入N組資料,每一組資料格式如下所示:
L TOKEN1 TOKEN2
L代表要輸出L * L個圖案,左下方的圖案為TOKEN1,其餘圖案為TOKEN2
TOKEN1或TOKEN2不能為空白、\t 或 \n
舉例,假設TOKEN1 = ‘$’,TOKEN2 = ‘@’,則
請用連續星星符號個數來代表頻率次數,如出現N次,請以N個星星符號表示
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
| 3 ||
| 5 @ # |@####<br /> @@###<br /> @@@##<br /> @@@@#<br /> @@@@@ |
| 7 & * |&******<br /> &&*****<br /> &&&****<br /> &&&&***<br /> &&&&&**<br /> &&&&&&*<br /> &&&&&&&|
```python=
for _ in range(int(input())):
l,a,b=input().split()
l = int(l)
for i in range(1, l+1):
print(a*i+b*(l-i))#字串可相加相乘
```
## HW6
剪刀石頭布遊戲是我們日常生活中常玩的一個小遊戲,常以三戰兩勝或五戰三勝或N戰(N+1)/2勝等結果來決定最後的勝負。當我們用Y代表剪刀、M代表石頭、O代表布,請試做一程式可以允許使用者可以不斷地輸入一組資料,直到此組資料為不合法資料則結束程式。每組資料一開頭為N值(正奇數值,若不為正奇數值則為不合法資料),代表之後的勝負是幾戰幾勝,接下來輸入兩個人的出拳結果,請根據所輸入的資料並印出是第一個人或是第二個人贏得最終勝利。
假設第一個人贏,則顯示The first person wins the game
假設第二個人贏,則顯示The second person wins the game
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
| 3
Y M
O O
Y Y
O Y
5 | The second person wins the game|
M Y
M M
O M
Y O
0 |The first person wins the game|
```python=
while True:
n= int(input())
if n%2==0:
break
win = (n+1) // 2
a, b = 0, 0
while a < win and b < win:
n1,n2=input().split()
if n1=='Y'and n2=='O'or n1=='M' and n2=='Y' or n1=='O' and n2=='M':
a+=1
elif n1==n2:
continue
else:
b+=1
if(a>b):
print("The first person wins the game")
else:
print("The second person wins the game")
```
## HW7
請試做一程式,此程式可以允許使用者不斷地輸入一組資料,直到此組資料為不合法資料則結束程式。每組資料格式如下所示:
k d1 d2 d3
k d1 d2 d3 均需為正整數
請根據每組資料,印出 d1, d2, d3 的前k小公倍數
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
|5 1 3 4 5 6 7|3 ***
4 26 28 |549828 19656 29484 39312
2 2 9 10|10 **********
0|跳出
```python=
while True:
k,n1,n2,n3=map(int,input().split())
if(k==0):
break
for i in range(n1*n2*n3+1):
if i%n1==0 and i%n2==0 and i%n3==0:
for j in range(1, k+1):
print(i*j,end=' ')
break
```
:::
:::spoiler **C**
## HW8
請實作一程式,此程式可以不斷地要求使用者輸入一組資料,直到此組資料的第一個數字不為正整數則結束程式。請根據每組資料的內容印出其相對應的結果。每組資料的格式內容如下所示:
N O d1 d2 … dN
N代表接下來有一個指令動作O以及N筆資料d1, d2, …, dN
O為1時則請印出d1, d2, …, dN中最小的值,即minimum{d1, d2, …, dN},並印出相對應的星星(星星與星星之間沒有空白)
O為2時則請印出d1, d2, …, dN中最大的值,即maximum{d1, d2, …, dN},並印出相對應的星星(星星與星星之間沒有空白)
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
5 1 3 4 5 6 7|3 ***|
2 2 9 10|10 **********
0|跳出
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, o, ans, d;
while (cin >> n && n != 0) {
cin >> o >> ans;
if (o == 1) {
for (int i = 0; i < n - 1; i++)
{
cin >> d;
if (ans > d)
ans = d;
}
}
else
{
for (int i = 0; i < n - 1; i++)
{
cin >> d;
if (ans < d)
ans = d;
}
}
cout << ans << ' ' << string(ans, '*') << endl;
}
return 0;
}
```
## HW7
請實作一程式,此程式可以要求使用者輸入3組資料,每一組資料的格式如下所示:
N1 N2 N3
請輸出每一組資料內三個數字的最小公倍數
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
6 2 4|12
1 5 3|15
8 2 16|16
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
for (int x = 1; x <= 3; x++)
{
int a[3];
cin >> a[0] >> a[1] >> a[2];
for (int i = 1; i <= a[0]* a[1] * a[2]; i++)
{
if (i % a[0] == 0 && i % a[1] == 0 && i % a[2] == 0)
{
printf("%d\n", i);
break;
}
}
}
return 0;
}
```
## HW6
請實作一程式,此程式可以要求使用者輸入3組資料,每一組資料的格式如下所示:
N1 N2 N3
請輸出每一組資料內三個數字的最大公因數
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
6 2 4|2
1 5 3|1
8 2 16|2
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
for(int x=1;x<=3;x++)
{
int a[3];
cin >> a[0] >> a[1] >> a[2];
int maxma = max(max(a[0], a[1]), a[2]);
for (int i = maxma; i >= 1; i--)
{
if (a[0] % i == 0 && a[1] % i == 0 && a[2] % i == 0)
{
printf("%d\n", i);
break;
}
}
}
return 0;
}
```
## HW5
請實作一程式,此程式可以要求使用者輸入N組資料,每一組資料的格式如下所示:
O N1 N2
O 為指令動作,只能是1 or 2
當O是1時,假設N1和N2的中最小值為MIN、最大值為MAX,請輸出從MIN到MAX範圍內的整數和,包含MIN及MAX。
當O是2時,請輸出從MIN到MAX範圍內的整數乘積,包含MIN及MAX。
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
3|
1 1 10|55
2 1 10|3628800
1 10 1|55
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int k, a, b, c;
cin >> k;
for (int i = 0; i < k; i++)
{
int sum = 0;
cin >> a >> b >> c;
if (a == 1)
{
if (b > c)//b=1 c=10
{
while (c <= b)//成立繼續
{
sum += c;
c++;
}
}
else
{
while (b <= c)
{
sum += b;
b++;
}
}
printf("%d\n", sum);
}
else
{
sum = 1;
if (b > c)
{
while (c <= b)
{
sum *= c;
c++;
}
}
else
{
while (b <= c)
{
sum *= b;
b++;
}
}
printf("%d\n", sum);
}
}
return 0;
}
```
## HW4
請實作一程式,此程式可以要求使用者輸入3組資料,每一組資料包含9個數字,且每個數字範圍為[1,10] (包含1及10)。請輸出每一組資料的總和及中間值。
在此注意到的是,中間值為將輸入資料由小到大排列取第 (9+1)/2 個數字(即第五個數字)
舉例:假設輸入資料分別為 1 9 3 2 5 4 8 6 7 時
將此輸入資料排序的結果為 1 2 3 4 5 6 7 8 9,因此取排列好的第5個數字,即5
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
1 9 3 2 5 4 8 6 7|45 5
1 1 3 2 5 4 8 6 7|37 4
7 6 8 4 5 2 3 1 1|37 4
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = 3;
while (n--) {
int nums[10] = {};
int total = 0;
for (int i = 0; i < 9; i++) {
cin >> nums[i];
total += nums[i];
}
sort(nums, nums + 9);
cout << total << " " << nums[4] << endl;
}
return 0;
}
```
## sort用法
```c=
#include <iostream>
#include <algorithm>
int main() {
int arr[] = {4, 5, 8, 3, 7, 1, 2, 6, 10, 9};
std::sort(arr, arr+10);
std::cout << "sort array by default (increasing):" << std::endl;
for (int i = 0; i < 10; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
1 2 3 4 5 6 7 8 9 10
## HW9
請實作一程式,此程式可以不斷地要求使用者輸入一值K,直到K<=0時結束程式。當一大於零的K值輸入後,請輸出相對應的K K乘法表。
當K為9時,即是九九乘法表
| <div style="width:290px">輸入資料</div> | <div style="width:290px">輸出結果</div> |
| -------- | -------- |
1|1*1=1
2|1*1=1 1*2=2 2*1=2 2*2=4
3|1*1=1 1*2=2 1*3=3
2*1=2 2*2=4 2*3=6
3*1=3 3*2=6 3*3=9|
|0|跳出
```c=
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int k;
while(true)
{
cin >> k;
if (k <= 0)
break;
for (int i = 1; i <= k; i++)
{
for (int j = 1; j <= k; j++)
printf("%d*%d=%d ", i, j, i * j);
printf("\n");
}
}
return 0;
}
```
{"metaMigratedAt":"2023-06-17T13:32:53.029Z","metaMigratedFrom":"Content","title":"Untitled","breaks":true,"contributors":"[{\"id\":\"d36855e0-469d-4775-a472-89b92f04b15c\",\"add\":11338,\"del\":1677}]"}