---
# System prepended metadata

title: WEEK7

---

# WEEK7



## 第一次小考

### 01 calculate the remainder 計算餘數

> 利用 if - else 依序判斷是否可以整除 0 2 5 10 並輸出結果
> 能整除、不能整除要分成兩種情況輸出

```cpp
#include <stdio.h>

int main() {
    int x, y;
    
    // Read two integers from the user
    scanf("%d", &x);
    scanf("%d", &y);
    
    // Calculate the remainder of x divided by y
    int remainder = x % y;
    
    if (remainder == 0) {
        // x is divisible by y (remainder is 0)
        printf("The remainder of %d / %d is 0.\n", x, y);
        
        // Check if x is even
        if (x % 2 == 0) {
            printf("%d is even.\n", x);
        } else {
            printf("%d is not even.\n", x);
        }
        
        // Check if x is divisible by 5
        if (x % 5 == 0) {
            printf("%d is divisible by 5.\n", x);
        } else {
            printf("%d is not divisible by 5.\n", x);
        }
        
        // Check if x is divisible by 10
        if (x % 10 == 0) {
            printf("%d is divisible by 10.\n", x);
        } else {
            printf("%d is not divisible by 10.\n", x);
        }
    } else {
        printf("The remainder of %d / %d is %d.", x, y, remainder);
    }
    
    return 0;
}
```


### 02 scholarship 獎學金


> 建議用 for 迴圈進行五次輸入
> 每輸入一次就用 switch 進行一次計算及輸出

```cpp
#include<stdio.h>

int main(){
    int i;
    float score;
    float total = 0.0;
    for(i = 0; i < 5; i++){
        scanf("%f",&score);
        switch ((int)score/10)
        {
        case 10:case 9:
            printf("A\n");
            total += (score * 11);
            break;
        case 8:
            printf("B\n");
            total += (score * 9.85);
            break;
        case 7:
            printf("C\n");
            total += (score * 7.75);
            break;
        case 6:
            printf("D\n");
            total += (score * 5.15);
            break;
        case 5:case 4:case 3:case 2:case 1:case 0:
        	printf("F\n");
            total += (score * 1.1);
            break;
        default:
            break;
        }
    }
    printf("Total scholarship : %.2f",total);
    return 0;
}
```



### 04 distance between two points 兩點間距離


>善用 math.h 中的函數

```cpp
#include <stdio.h>
#include <math.h>

int main() {

    int x1, x2, y1, y2;
    float distance;

    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    // 計算距離
    distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

    printf("%.3f", distance);
    
    return 0;

}
```

![image](https://hackmd.io/_uploads/BydboJ71C.png)
==要特別注意函式參數的型態!!==
標頭檔 math.h 宣告許多跟數學計算相關的函數 (function) ，如求平方根、指數、對數、三角函數等，大部分函數都是以 double 型態為預設回傳值 (return type) 及參數 (parameter) 的資料型態 (data type) ，也另提供 float 與 long double 型態對應的函數。
[math.h](http://kaiching.org/pydoing/c/c-std-math.html)


### 08 multiplication table 乘法表

> 用 for 迴圈處理重複的問題

```
#include <stdio.h>
main(void){
	int x;
	scanf("%d,",&x);
	
	int a = x * 1;
	int b = x * 2;
	int c = x * 3;
	int y = x * 4;
	int e = x * 5;
	int f = x * 6;
	int g = x * 7;
	int h = x * 8;
	int i = x * 9;
	int j = x * 10;
	
	printf("%d * 1 = %d",x,a);
	printf("\n%d * 2 = %d",x,b);
	printf("\n%d * 3 = %d",x,c);
	printf("\n%d * 4 = %d",x,y);
	printf("\n%d * 5 = %d",x,e);
	printf("\n%d * 6 = %d",x,f);
	printf("\n%d * 7 = %d",x,g);
	printf("\n%d * 8 = %d",x,h);
	printf("\n%d * 9 = %d",x,i);
	printf("\n%d * 10 = %d",x,j);
}
```
```cpp
#include <stdio.h>
int main() {
  int number, i;
  scanf("%d", &number);

  for (i = 1; i <= 10; ++i) {
      printf("%d * %d = %d", number, i, number * i); 
      printf("\n");
  }
  return 0;
}
```


### 14 Caesar Cipher Problem 凱薩加密法


> 除了單純加3、最後3個另外處理的做法
> 可以使用 %26 來使字母循環
> 但需要特別注意要先將 ASCII code 的值減去 'A' 在 ASCII 表中的位置(65)
> 讓字母 shift 到 0-26

```cpp
#include<stdio.h>
int main(){
	char c;
    scanf("%c", &c);
    printf("%c", (c-65+3)%26+65);
    return 0;
}
```
![app-encoding.ascii](https://hackmd.io/_uploads/rke1i1QyR.png)





### 15 3n+1 Problem 3n+1問題


>看起來很難，但其實只要依照題目敘述依序寫出判斷條件即可

```cpp
#include <stdio.h>
int main() {
    int n, cycle=1, i;
    scanf("%d", &n);
    while(n!=1) {
        if(n%2 == 1)
            n = 3*n+1;
        else 
            n/=2;
        cycle++;
    }
    printf("%d", cycle);
    return 0;
}
```






## week7


### 08、09

```cpp
#include <stdio.h>
int main() {
    int n;
    while(scanf("%d", &n) != EOF) {
        //寫程式在這裡
    }
    return 0;
}
```
[EOF的用法](https://blog.csdn.net/diviner_s/article/details/109502597)



### 作答方式
在下面的 code 區塊將有已經寫好的 code template，大家只需要完成**函數的部分**就可以完成作答。

在自己的編譯器上面測試函數能夠正常使用後，把函數放到底下的提交區塊後提交即可。


### Function
![螢幕擷取畫面 2024-04-03 172017](https://hackmd.io/_uploads/H1fVBs91C.png)


![image](https://hackmd.io/_uploads/Sy8-_sq1C.png)

![image](https://hackmd.io/_uploads/ByiNdsckC.png)

![image](https://hackmd.io/_uploads/rkoPdj9yR.png)



### 怎麼將程式用函式改寫

**原本的程式**
```
#include <stdio.h>
#include <stdlib.h>

int main() {

	int n, m;

	scanf("%d", &n);

	for(int i=0;i<n;i++){
			printf("*");
		}
		printf("\n");
		for(int j=0;j<(n-2);j++){
		printf("*");
		for(int i=0;i<(n-2);i++){
			printf(" ");
		}
		printf("*\n");
		}
		for(int i=0;i<n;i++){
			printf("*");
		}
		printf("\n");


	return 0;
}
```

**改成函式寫法**
```
#include <stdio.h>
#include <stdlib.h>

void print(int);

void print(int n) {

}

int main() {

	int n, m;

	scanf("%d %d", &n, &m);

	print_rectangle(n);


return 0;
}
```


### 11

**Code Template**
```cpp
//PREPEND BEGIN
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print();
//PREPEND END

//TEMPLATE BEGIN
#include<stdio.h>
void print() {
    // 請完成 function 使其能夠在執行後輸出 Hello World!
    // Please complete the function so that when executed, it will output Hello World!
}
//TEMPLATE END

//APPEND BEGIN
int main() {
    print();
  	return 0;
}
//APPEND END
```

**Your Code**
```cpp
#include<stdio.h>
void print() {
  printf("Hello World!");
}
```

### 12

```cpp
//PREPEND BEGIN
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print_rectangle(int);
void print_trangle(int);
//PREPEND END

//TEMPLATE BEGIN
#include<stdio.h>
void print_rectangle(int n) {

}

void print_trangle(int m) {

}
//TEMPLATE END

//APPEND BEGIN
int main() {

int n, m;

scanf("%d %d", &n, &m);

print_rectangle(n);

print_trangle(m);

return 0;
}
//APPEND END
```

### 13

```cpp
//PREPEND BEGIN
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int diameter(int);
int circumference(int);
int area(int);
//PREPEND END

//TEMPLATE BEGIN
#include<stdio.h>
int diameter(int r){

}
int circumference(int r) {
    int pi = 3;
}
int area(int r) {
	int pi = 3;
}

//TEMPLATE END

//APPEND BEGIN
int main() {
  	int r;
  	scanf("%d", &r);
    printf("%d\n%d\n%d", diameter(r), area(r), circumference(r));
  	return 0;
}
//APPEND END
```

### 14

```cpp
//PREPEND BEGIN
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

char convert(int, int);
//PREPEND END

//TEMPLATE BEGIN
#include<stdio.h>

char convert(int n, int c) {

}
//TEMPLATE END

//APPEND BEGIN
int main() {

int n, c;

scanf("%d %d", &n, &c);

printf("%c", convert(n, c));

return 0;
}
//APPEND END
```

### 15

```cpp
//PREPEND BEGIN
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void algorithm(int);
//PREPEND END

//TEMPLATE BEGIN
#include<stdio.h>
void algorithm(int n) {

}
//TEMPLATE END

//APPEND BEGIN
int main() {
    int n;
    scanf("%d", &n);
    algorithm(n);
    return 0;
}
//APPEND END
```

















