---
# System prepended metadata

title: C++ Week 01 basics
tags: [c++_beginner]

---

###### tags: `c++_beginner`
# Week 01
> 這周進度用會用飆的哈哈，線上題共5題，但都很基礎。我自己有放幾題之前遇到比較有趣的題目可以試試看。


教學影片的部分因為進度不同，請依照標題和自身情況跳著看，預計兩周內解決4小時:)。(不會全部4小時看完啦)
[Link](https://www.youtube.com/watch?v=vLnPwxZdW4Y&t=9526s)
<iframe width="560" height="315" src="https://www.youtube.com/embed/vLnPwxZdW4Y?si=jH-Mr9SjWjow0Ku0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>



## I/O

### 講解

所謂的input 和output，very easy ya~

Input 方法
```cpp=
int a;
cin>>a;
```
```cpp=
int a;
scanf("%d",a);
```
Output 方法
```cpp=
int a=1;
cout<<a;
```
```cpp=
int a=1;
printf("%d",a);
```


### 題目


1. [Say "Hello,world" With C++](https://www.hackerrank.com/challenges/cpp-hello-world/problem?isFullScreen=true)
2. [Input and Output](https://www.hackerrank.com/challenges/cpp-input-and-output/problem?isFullScreen=true)


## Data type
### 講解


資料類別，一樣easy的啦~
| Data Type | Size | Description |
| -------- | -------- | -------- |
| boolean|1 byte|Stores true or false values|
| char|1 byte|Stores a single character/letter/number, or ASCII values|
|int|2 or 4 bytes|Stores whole numbers, without decimals|
|float|4 bytes|Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits|
|double|8 bytes|Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits|


Example
```cpp=
int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99;     // Floating point number
double myDoubleNum = 9.98;   // Floating point number
char myLetter = 'D';         // Character
bool myBoolean = true;       // Boolean
string myText = "Hello";     // String
```

### 題目
1. [Basic Data Type](https://www.hackerrank.com/challenges/c-tutorial-basic-data-types/problem?isFullScreen=true)



## Conditional Statements



### 講解
其實就是if else 那些，跟C的語法一模一樣

---

Logical conditions:
* Less than: a < b
* Less than or equal to: a <= b
* Greater than: a > b
* Greater than or equal to: a >= b
* Equal to a == b
* Not Equal to: a != b


---

Conditional statements:

* Use **if** to specify a block of code to be executed, if a specified condition is true
* Use **else** to specify a block of code to be executed, if the same condition is false
* Use **else if** to specify a new condition to test, if the first condition is false
* Use **switch** to specify many alternative blocks of code to be executed
Example
```cpp=
if (20 > 18) {
  cout << "20 is greater than 18";
}
```
### 題目
1. [Conditional Statements](https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem?isFullScreen=true)
## Loops
### 講解
for迴圈與while迴圈

For 迴圈:
```cpp=
for (int i = 0; i < 5; i++) {
  cout << i << "\n";
}
```
While 迴圈

```cpp=
int i = 0;
while (i < 5) {
  cout << i << "\n";
  i++;
}
```


### 題目


1. [For Loop](https://www.hackerrank.com/challenges/c-tutorial-for-loop/problem?isFullScreen=true)



## Test
### 01 : 一層一層
使用者輸入一整數n，然後輸出一層一層遞減的數。如:
Input
```
5
```
Output
```
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
```
### 02: 停雨
雨到底停了沒?
輸入一整數n, 1<=n<=10，代表下雨機率。
如果n為1 ~ 4,則輸出一個高度為n的正方形;如果n為5 ~ 7，則輸出一個高為n的等腰直角三角形;如果n為8 ~ 10，則輸出一個高為n的叉叉。
Sample01:
Input
```
3
```
Output
```
***
***
***
```
Sample02:
Input
```
5
```
Output
```
*
**
***
****
*****
```
Sample03:
Input
```
8
```
Output
```
*      *
 *    *
  *  *
   **
   **
  *  *
 *    *
*      *
```

