---
# System prepended metadata

title: 羅工 C++

---


# 基本架構與輸出
<font color="#f00">#include</font> 用來引入標頭檔
<font color="#f00"><iostream></font> 預先定義的函式庫
<font color="#f00">int main( ){ }</font> 定義函數
```cpp=
#include<iostream>
using namespace std;

int main()
{
    return 0;
}
```
# 注意事項
    * 每打完一行要加;(分號)
    * endl 代表 end line，結束一行(換行)
    * 盡量不要在程式碼裡面寫中文，有時候會出錯
# 變數型態

| 型態 | 可儲存的資料 |
| -------- | -------- |
| int     | 100、-5、1246     |
| char     | 'a'、'R'、'1'、'@'、'*'     |
| float     | 3.14159、4.3、-1.1     |
| string    | "Hello"、"^_^"、"Rock!"     |
| double    | 3.14159、4.3、-1.1   |

# cout輸出
![](https://hackmd.io/_uploads/r1wkNkrin.png)

# cin 輸入
![](https://hackmd.io/_uploads/ByERBJHs3.png)
    
# 小範例
```cpp=
#include<iostream>
using namespace std;

int main()
{
    int a,b,c;
    cin >>a>>b>>c;
    cout <<a<<b<<c;
    return 0;
}
```
![](https://hackmd.io/_uploads/ryPHvyri2.png)

# <font color="red">7/31上課題目</font>
![](https://hackmd.io/_uploads/ryxrff8o2.png)
```cpp=
#include<iostream>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    cout<<a+b<<endl;
    cout<<a-b<<endl;
}
```

![](https://hackmd.io/_uploads/H1bXHzUs2.png)
```cpp=
#include<iostream>
using namespace std;

int main()
{
    int c1,c2;
    cin>>c1>>c2;
    cout<<c1<<endl;
    cout<<c2<<endl;
}
```
![](https://hackmd.io/_uploads/S1m0BfLsn.png)
```cpp=
#include<iostream>
using namespace std;

int main()
{
    float num1,num2;
    cin>>num1>>num2;
    cout<<num1<<endl;
    cout<<num2<<endl;
    cout<<num1/num2<<endl;
}
```
![](https://hackmd.io/_uploads/HJuxvMIj3.png)

```cpp=
#include<iostream>
using namespace std;

int main()
{
    string name;
    cin >>name;
    cout<<"My name is "+name;
}
```

# <font color="red">8/1上課題目</font>
![](https://hackmd.io/_uploads/SkjODvvoh.png)
```cpp=
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int a,b,c;
    float max;
    cin>>a>>b>>c;
    max=(-b+sqrt(b*b-4*a*c))/(2*a);
    cout<<max<<endl;
    max=(-b-sqrt(b*b-4*a*c))/(2*a);
    cout<<max<<endl;
}
```
![](https://hackmd.io/_uploads/HJ9jtvwsh.png)
```cpp=
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    float a,b,c,max;
    cin >> a >> b >> c;
    max=(a+b+c)/2;
    cout<<"周長 : "<<max<<endl;
    cout << "海龍公式 : " <<sqrt(max*(max-a)*(max-b)*(max-c))<<endl;
}
```

# <font color="red">8/4上課題目</font>
![](https://hackmd.io/_uploads/S1wCTl0s2.png)
![](https://hackmd.io/_uploads/Hk_iZMCjh.png)

```cpp=
#include <iostream>
using namespace std;

int main() {
    char a;
    cin>> a;
    string st;
    cin>>st;
    switch(a)
    {
        //"宜蘭地區身分證"
        case 'G':
            int m=5+st[0]*8+st[1]*7+st[2]*6+st[3]*5+st[4]*4+st[5]*3+st[6]*2+st[7]*1+st[8]*1;
            if(m%10==0 && st.size()==9){
                cout<<"Yes";
            }else{
                cout<<"error";
            }
    }
}
```
# <font color="red">8/7上課題目</font>
![](https://hackmd.io/_uploads/rkbvJ81nh.png)
```cpp=
#include <iostream>

using namespace std;

int main() {
    int a=1,b,c;
    cin>>b;
    cout<<"質數:";
    while(a<b){
        for(;a<=b;a++){
            for(int i=1;i<=b;i++){
                if(a%i==0){
                    c++;
                }
                if(i==b&&c==2){
                        cout<<a<<"  ";
                }
            }
            c=0;
        }
    }
}    
```
    