---
# System prepended metadata

title: '**C++程式設計**'

---

# **C++程式設計**

# 興趣來源

**<font color="#0D8738">高一上學期，因為資訊老師的教學，讓我接觸到C++程式設計，使我對程式有更多的了解，讓我學到了陣列、函式、迴圈⋯⋯</font >**
            
# 本學期所學(ZEROJUDGE題目練習)


##  **1. 基本使用**

:::success
**d051: 糟糕，我發燒了**
:::
:::info
* **<font color="#000079">浮點數 double</font>**
* **<font color="#000079">除法"/"的使用</font>**
* **<font color="#000079">C++小數的控制</font>**
  :exclamation: 
 =="/"出來的答案為商式==
 =="%"出來的答案為餘式==
:::

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

int main()
{

 int hh,mm;
 while(cin>>hh>>mm)
  {
   if((hh==7 && mm==30)||(hh>7 && mm<17))
    {
        cout<<"At School"<<endl;
    }
    else
    {
        cout<<"Off School"<<endl;
    }
  }
return 0;
}
``` 
<br>
<br>
<br>
<br>
<br>
<br>
<br>

<br>
<br>
<br>

<br>
<br>
<br>

<br>
<br>
<br>

<br>
<br>
<br>
<br>
---
##  **2.運算式與運算子**
### 

:::success
**d050: 妳那裡現在幾點了？**
:::
:::info
* **<font color="#000079">⚠️台灣時間+15後是否超過24，如果超過就變成隔天</font>**
* **<font color="#000079">使用"%24的餘數代表美國時間"</font>**
:::



```cpp=

#include <iostream>

using namespace std;

int main()
{
    int a; //台灣時間

    while(cin>>a)
    {

        cout<<(a+24-15)%24<<endl;
    }

    return 0;
}
```

```cpp=
#include <iostream>
#include <stack>

using namespace std;

int main()
{
    int n;

    while(cin>>n)
    {
        stack<int> sk;
        while(n>0)
        {
            sk.push(n%2);
            n/=2;
        }
        while(!sk.empty())
        {
            cout<<sk.top();
            sk.pop();
        }
        cout<<endl;
    }
    return 0;
}
```

## **3. if判別式**
:::warning
**if else**
:::
![](https://i.imgur.com/SbD3I5z.png)
:::success
 **f373:週年慶 Anniversary -- TOI 練習賽202010新手組1**
:::
:::info
* **利用!的性質 =》"！="表示不等於**
* **利用sum加總**
:::

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int N=0; //原價
    while(cin>>N)
    {
      int sum0=N,sum1=N; //sum0->天天百貨,sum1->琪琪百貨
      sum1-=sum1/1000*100;//琪琪百貨折扣後的價格
      sum0-=sum0/2000*200;//天天百貨折扣後的價格
      if(sum0>sum1 && sum0!=sum1)
      {
          cout<<sum1<<" "<<"1"<<endl;
      }
      else
      {
           cout<<sum0<<" "<<"0"<<endl;
      }

    }



    return 0;
}
```
:::warning
**if else if**
:::
![](https://i.imgur.com/7UhLzvh.png)

```cpp=
#include <bits/stdc++.h>

using namespace std;

int func(string s,int counts){
    int temp=0,total=0;
    for(int i=0;i<s.length();i++)
            total+=s[i]-'0';
     counts++;
    if (total == 9)
        return counts;
    else if(total < 9)
        return 0;
     else
        return func(to_string(total), counts);
}



int main()
{
    int ans;
    string s;
    while(cin>>s)
    {
        if(s=="0")
            break;

        ans=func(s,0);
        if(ans==0)
           cout << s << " is not a multiple of 9.\n";
        else
            cout<<s<<" is a multiple of 9 and has 9-degree "<<ans<<".\n";

    }
    return 0;
}

```





