# WEEK5
## 上週題目講解
### 01 Standard Output
> 解題思路:
> 依照指定內容進行輸出
```cpp
#include <stdio.h>
int main(){
printf("class name: programmimg language\nclassroom: \"234\"");
return 0;
}
```
### 02 Get Nth Even Number
> 解題方法:
> 找關係,第 n 個偶數是 (n-1)*2
> 解題思路:
> 1. 輸入一個數
> 2. 進行運算
> 3. 輸出運算結果
```cpp
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int r= (n-1)*2;
printf("%d",r);
return 0;
}
```
### 03 Find Coefficients
> 解題方法:
> 由兩個跟,如何推回一元二次方程式的係數
> 解題思路:
> 1. 輸入一個數
> 2. 進行運算
> 3. 輸出運算結果
```cpp
#include<stdio.h>
int main(void){
int x1, x2;
scanf("%d %d", &x1, &x2);
int coefficients[3];
coefficients[0] = 1;
coefficients[1] = 0 - x1 - x2;
coefficients[2] = x1 * x2;
printf("(1, %d, %d)", coefficients[1], coefficients[2]);
return 0;
}
```
### 04 Pressure
> 解題方法:
> 根據公式進行運算
> 解題思路:
> 1. 輸入一個數
> 2. 進行運算
> 3. 輸出運算結果
```cpp
#include<stdio.h>
int main(void){
float given_mass1, given_mass2, molar_mass1, molar_mass2, t, v, result;
scanf("%f %f %f %f %f %f", &molar_mass1, &molar_mass2, &given_mass1, &given_mass2, &v, &t);
result = (given_mass1 / molar_mass1 + given_mass2 / molar_mass2) * 0.082 * (t + 273.15) / v;
printf("%.5f", result);
return 0;
}
```
### 05 Find Century
> 解題方法:
> 分成能整除100及不能整除100的情況
> 解題思路:
> 1. 輸入一個數
> 2. 進行判斷及運算
> 3. 輸出運算結果
```cpp
#include <stdio.h>
int main(){
int year;
scanf("%d", &year);
if (year%100 == 0) printf("%d", year/100);
else printf("%d", (year/100)+1);
return 0;
}
```
### 06 Collinearity
> 解題方法:
> 判斷兩條線斜率是否相同
> 解題思路:
> 1. 輸入四個數
> 2. 進行運算及判斷
> 3. 若斜率相等輸出 true,否則輸出 false
==需要特別注意不能除0,會出現 run time error==
```cpp
解法一
#include <stdio.h>
int main() {
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if(x1*y2 - x2*y1 == 0) {
printf("true");
} else {
printf("false");
}
return 0;
}
```
```cpp
(d1101645)解法二
#include <stdio.h>
ain()
{
int x1,y1,x2,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(x1 == 0){
x1 = 1;
}
if(x2 == 0){
x2 = 1;
}
if((y1/x1) == (y2/x2)){
printf("true");
}else{
printf("false");
}
}
```
### 07 Quadrants
> 解題方法:
> 判斷兩個輸入的正負
> 解題思路:
> 1. 輸入兩個數
> 2. 進行正負號的判斷
> 3. 對應到正確的象限並輸出結果
```cpp
#include <stdio.h>
int main() {
int x, y;
scanf("%d %d", &x, &y);
if(x > 0 && y > 0) printf("1");
else if(x > 0 && y < 0) printf("4");
else if(x < 0 && y > 0) printf("2");
else printf("3");
return 0;
}
```
### 08 Check Case
> 解題方法:
> 判斷兩個輸入符合哪一種情況
> 解題思路:
> 1. 輸入兩個數
> 2. 進行判斷,可以用 ASCII 或 <ctype.h>
> 3. 輸出結果
```cpp
#include <ctype.h>
#include <stdio.h>
int main()
{
char a, b;
scanf("%c %c", &a, &b);
if (a>=65 && a<=90 && b>=65 && b<=90)
printf("1");
else if (a>=97 && a<=122 && b>=97 && b<=122)
printf("1");
else if(a>=65 && a<=90 && b>=97 && b<=122)
printf("0");
else if(b>=65 && b<=90 && a>=97 && a<=122)
printf("0");
else
printf("-1");
return 0;
}
```
做法2
```cpp
if (!isalpha(a) || !isalpha(b))
printf("-1");
else if ((isupper(a) && isupper(b)) || (islower(a) && islower(b)))
printf("1");
else
printf("0");
```
### 09 Pillars
> 解題方法:
> 計算距離 (包含間隔距離及柱子本身的距離)
> 解題思路:
> 1. 輸入三個數
> 2. 若柱子數量小於等於 1,輸出 0。
> 3. 輸出計算後的結果
```cpp
#include <stdio.h>
int main()
{
int num_of_pillars, distance, width;
scanf("%d %d %d", &num_of_pillars, &distance, &width);
if (num_of_pillars == 1) {
printf("0");
}else{
printf("%d", (num_of_pillars - 1) * distance * 100 + (num_of_pillars - 2) * width);
}
}
```
### 10 Is this a triangle
> 解題方法:
> 檢查兩邊之和大於第三邊
> 解題思路:
> 1. 輸入三個數
> 2. 進行邊長關係的判斷
> 3. 輸出判斷的結果
```cpp
#include<stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a >= b + c) {
printf("false");
} else if (b >= a + c) {
printf("false");
} else if (c >= a + b) {
printf("false");
} else {
printf("true");
}
}
```
```cpp
#include<stdio.h>
int main(){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a>0 && b>0 && c>0){
if(a+b>c && a+c>b && b+c>a)
printf("true");
else
printf("false");
}
else
printf("false");
}
```
### 11 Predicate Function
> 解題方法:
> 依照公式計算
> 解題思路:
> 1. 輸入三個數
> 2. 進行計算及判斷
> 3. 輸出結果
```cpp
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int r = b*b - 4*a*c;
if(r >= 0)
printf("0");
else if(r < 0)
printf("1");
}
```
### 12 Final Grad
> 解題方法:
> 依照題目敘述寫出 if-else 判斷
> 解題思路:
> 1. 輸入兩個數
> 2. 進行判斷
> 3. 輸出判斷的結果
```cpp
#include<stdio.h>
int main(){
int exam, projects;
scanf("%d %d", &exam, &projects);
if (exam > 90 || projects > 10) {
printf("100");
}
else if (exam > 75 && projects >= 5) {
printf("90");
}
else if (exam > 50 && projects >= 2) {
printf("75");
}
else {
printf("0");
}
}
```
### 13 next multiple of 5
> 解題方法:
> 用迴圈找下一個可以整除5的數
> 解題思路:
> 1. 輸入一個數
> 2. 進行迴圈判斷
> 3. 輸出判斷的結果
```cpp
作法一
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i = n;
while(i <= (n + 5))
{
if(i % 5 == 0){
printf("%d", i);
break;
}
i++;
}
}
```
```cpp
(d1101645)作法二
#include <stdio.h>
#include <ctype.h>
int main()
{
int a;
scanf("%d",&a);
if(a == 0){
printf("0");
}else{
a +=4;
printf("%d",a/5*5);
}
}
```
### 14 Add Digits
> 解題方法:
> 用兩層while迴圈,外層判斷此數字是否已經剩下一位數,內層將所有位數相加
> 解題思路:
> 1. 輸入一個數
> 2. 進行迴圈判斷
> 3. 輸出最終的結果
```cpp
#include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
while (num / 10)
{
int sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
num = sum;
}
printf("%d", num);
return 0;
}
```
### 15 Square Star Pattern
> 解題方法:
> 用兩層 for 迴圈分別代表行及列,將 n*n 的空間走過一次
> 解題思路:
> 1. 輸入一個數
> 2. 用雙層 for 迴圈印出 *
```cpp
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("*");
}
if(i < n-1)
printf("\n");
}
return 0;
}
```
### 16 Hollow Square Star Pattern
> 解題方法:
> 用兩層 for 迴圈分別代表行及列,將 n*n 的空間走過一次,再加上一些判斷
> 解題思路:
> 1. 輸入一個數
> 2. 用雙層 for 迴圈將 n*n 的空間走過一次
> 3. 若符合條件,則印出 *
```cpp
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1 ||i==n||j==1||j==n)
{
printf("*");
}
else
printf(" ");
}
if(i <= n-1)
printf("\n");
}
return 0;
}
```
### 17 Hollow Square Pattern with Diagonal
> 解題方法:
> 用兩層 for 迴圈分別代表行及列,將 n*n 的空間走過一次,再加上一些判斷
> 解題思路:
> 1. 輸入一個數
> 2. 用雙層 for 迴圈將 n*n 的空間走過一次
> 3. 若符合條件,則印出 *
==有六條線,每條線分別有一個條件表示==
```cpp
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n)
{
printf("*");
}
else
{
printf(" ");
}
}
if(i <= n-1)
printf("\n");
}
return 0;
}
```
### 18 Ugly Number
> 解題方法:
> 將輸入不斷的除2、3、5,看是否最後能整除 (n剩下1)
> 解題思路:
> 1. 輸入一個數
> 2. 用 while 迴圈重複進行除法
> 3. 看是否最後能整除 (n剩下1)
```cpp
#include<stdio.h>
int main(){
int n,x;
int c;
scanf("%d", &n);
while(n>1){
if(n%2==0){
n=n/2;
}else if(n%3==0){
n=n/3;
}else if(n%5==0){
n=n/5;
}else{
printf("false");
break;
}
}
if ( n == 1){
printf("true");
}
}
```
## 本周題目說明
### 03 Asperand pixels
> 解題方向:找關係
### 06 Sum of a sequence
> 解題方向:善用 for 迴圈的定義
</br>
</br>
### ==什麼時候用 for、什麼時候用 while==
| BASIS FOR COMPARISON | FOR | WHILE |
| -------------------- | ------------------ | ----------------- |
| Declaration | for(initialization; condition; iteration){//body of 'for' loop} | while ( condition) {statements; //body of loop} |
| Format | Initialization, condition checking, iteration statement are written at the top of the loop. | Only initialization and condition checking is done at the top of the loop. |
| Use | The 'for' loop used only when we already knew the number of iterations. | The 'while' loop used only when the number of iteration are not exactly known. |
| Iteration statement | In 'for' loop iteration statement is written at top, hence, executes only after all statements in loop are executed. | In 'while' loop, the iteration statement can be written anywhere in the loop. |
For
```
for (宣告索引變數; 執行條件; 每次迭代索引變數的變化) {
重複執行的程式
}
```
While
```
while (執行條件) {
重複執行的程式
}
```
兩者最大的差異就在於有沒有**宣告索引變數**,而for loop因為宣告了索引變數,所以可以記錄目前跑了幾次迴圈,迴圈次數到了就可以跳出迴圈了。相對的While不須宣告索引變數,只要執行條件仍然滿足,就可以一直迴圈下去。
因此,當已知需要進行迴圈的次數,用 for;而當只知道迴圈執行條件,而不清楚總共需跑幾次迴圈時,就只能使用 While。
以小狗繞圈圈為例: 如果要讓小狗繞10圈,可用for迴圈;如果要讓小狗一直跑到主人回到家,但不知道會跑幾圈時,就只能用while迴圈。
[參考資料:Difference Between for and while loop](https://techdifferences.com/differenece-between-for-and-while-loop.html)
### 08 Twisted Sum
假設輸入 n 個數字
```cpp
int sum = 0;
for (int i = 1; i <= n; i++) {
while (num > 0) {
// Add the last digit of the number to the sum
// Remove the last digit from the number
}
}
return 0;
```
N = 12
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + (1 + 0) + (1 + 1) + (1 + 2) = 51
### 09 A pile of Cubes
解釋一下圖形