---
# System prepended metadata

title: 程式設計(C++)
tags: [資訊]

---

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

# *I 變數與運算子*

## EX_01 三角形三邊長，以海龍公式求三角形面積

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

using namespace std;

int main()
{
    int a,b,c;
    double s,area;

    cout << "請輸入三角形三邊長:";
    cin >> a >> b >> c;
    s=(double)(a+b+c)/2;
    area=sqrt(s*(s-a)*(s-b)*(s-c));

    cout << "三角形面積為 " << area << endl;
    return 0;
}
```

## d483  hello, world

```cpp=
#include <iostream>

using namespace std;

int main()
{
    cout << "hello, world" << endl;
    return 0;
}
```

## a001 哈囉

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

int main()
{
    string s;

    while(cin >> s)
    {
        cout << "hello, " << s << endl;
    }

    return 0;
}
```

## d489 伏林的三角地

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a,b,c,s;

    while(cin >> a >> b >> c)
    {
        s=(double)(a+b+c)/2;

    cout << s*(s-a)*(s-b)*(s-c) << endl;
    }
    return 0;
}
```

## d827 買鉛筆

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        cout << n/12*50+n%12*5 << endl;
    }

    return 0;
}
```

## d060 還要等多久啊？

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int m;

    while(cin >> m)
    {
        cout << (m<=25 ? 25-m : 25-m+60) << endl;
    }
    return 0;
}
```

## d051 糟糕，我發燒了!

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

using namespace std;

int main()
{
    int f;

    while(cin >> f)
    {
        cout << fixed << setprecision(3) << (f-32)/1.8 << endl;
    }
    return 0;
}
```

## b004 繩子上吃草的牛

```cpp=
#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

int main()
{
    double D,L,s,l;
    const double PI=2*acos(0);

    while(cin >> D >> L)
    {
        s=sqrt((L/2)*(L/2)-(D/2)*(D/2));
        l=L/2;
        printf("%.3f\n", PI*s*l);
    }
    return 0;
}
```

## d039 11044 - Searching for Nessy

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int t,n,m;

    cin >> t;
    while(t>0)
    {
        cin >> m >> n;

        cout << (m/3)*(n/3) << "\n";
        t=t-1;
    }

    return 0;
}
```

## a861 Secure the Perimeter

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int H,W;

    while(cin >> H >> W)
    {
        cout << 2*H+2*W << endl;
    }

    return 0;
}
```

## d226 Back to High School Physics

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int t,v;

    while(cin >> t >> v)
    {
        cout << 2*v*t << endl;
    }

    return 0;
}
```

## d053 Big Chocolate

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int M,N;
    while(cin >> M >> N)
    {
        cout << (M-1)+(N-1)*M << "\n";

    }

    return 0;
}
```

## d277 矩形對角線

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int N;

    while(cin >> N)
    {
        if((N%2)==1)
        {
            cout << N-1 << endl;
        }
        else
        {
            cout << N << endl;
        }
    }

    return 0;
}
```

## b681 1.山洞探險

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int L;

    while(cin >> L)
    {
        cout << (L>0 ? 2*L-1 : L*(-1)*2) << endl;
    }
    return 0;
}
```

## d127 二、牧場面積

```cpp=
#include <iostream>

using namespace std;

int main()
{
    long long int L,S;

    while(cin >> L)
    {
        S=L/2;
        cout << (S/2)*(S/2+S%2) << endl;
    }

    return 0;
}
```

## d461 班際籃球賽

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        cout << n-1<< endl;
    }

    return 0;
}
```

## d096 00913 - Joana and the Odd Numbers

```cpp=
#include <iostream>

using namespace std;

int main()
{
    long long int N;

    while(cin >> N)//1<N<1000000000
    {
        N=(N+1)/2;
        cout << (1+((6+6+4*(N-2))*(N-1)/2)-2)*3 << endl;
    }

    return 0;
}
```

## c776 106北二1.六邊形屋瓦

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n,m;

    while(cin >> n >> m)
    {
        cout << ((n*m*6)-(2*(m-1)*n)-(n-1)*m) << endl;
    }
    return 0;
}
```

## d549 矩形中的几何

```cpp=
#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

int main()
{
    double A,B,C,D;

    while(cin >> A >> B >> C)
    {
        D= sqrt(A*A+C*C-B*B);
        printf("%.2f\n",D);
    }

    return 0;
}
```

# *II 條件判斷*

## EX_02 判斷三邊長是否可以構成三角形，如果可以，求三角形面積

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

using namespace std;

int main()
{
    int a,b,c;
    double s,area;

    cout << "請輸入三角形三邊長:";
    cin >> a >> b >> c;

    if(a>0 && b>0 && c>0 && a+b>c && b+c>a && a+c>b)
    {
        s=(double)(a+b+c)/2;
        area=sqrt(s*(s-a)*(s-b)*(s-c));

        cout << "三角形面積為 " << area << endl;
    }
    else
    {
        cout << "不能構成三角形";
    }

    return 0;
}
```

## EX_03 驗證登入帳號、密碼

```cpp=
#include <iostream>

using namespace std;

int main()
{
    string usr,pwd;

    cout << "enter account:" << endl;
    cin >> usr;
    cout << "enter password:" << endl;
    cin >> pwd;

    if(usr=="999" && pwd=="666")
    {
        cout << "Welcome";
    }
    else
    {
        if(usr!="999")
        {
            cout << "account error" << endl;
        }

        if(pwd!="666")
        {
            cout << "password error" << endl;
        }
    }
    return 0;
}
```

## EX_04 將成績轉換成等級制

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int score;

    cout << "enter score:" << endl;
    cin >> score;

    if(score>100 || score<0)
    {
        cout << "error" << endl;
    }
    else
    {
        if(score>=80)
        {
            cout << "A";
        }
        else if(score>=70)
        {
            cout << "B";
        }
        else if(score>=60)
        {
            cout << "C";
        }
        else if(score>=0)
        {
            cout << "D";
        }
    }
    return 0;
}
```

## d058 BASIC 的 SGN 函數

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    
    while(cin >> n)
    {
        if(n>0)
            cout << "1" << endl;
        else if(n==0)
            cout << "0" << endl;
        else if(n<0)
            cout << "-1" << endl;
    }
    return 0;
}
```

## a006 一元二次方程式

```cpp=
#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

int main()
{
    int a,b,c,m,n,d;

    while(cin >> a >> b >> c)
    {
        d=b*b-4*a*c;
        if(d<0)
            cout << "No real root";
        m=(-b+sqrt(d))/2/a;
        n=(-b-sqrt(d))/2/a;

        if(d==0)
            cout << "Two same roots x=" << m;
        else if(d>0)
            cout << "Two different roots x1=" << m << " , x2=" << n;

    }
    return 0;
}
```

## a004 文文的求婚

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int Y;
    while(cin >>Y)
    {
        if(Y%4==0 && Y%100!=0 || Y%400==0)
        {
            cout << "閏年\n" << endl;
        }
        else
        {
            cout << "平年\n" << endl;
        }
    }
    return 0;
}
```

## a012 10055 - Hashmat the Brave Warrior

```cpp=
#include <iostream>

using namespace std;

int main()
{
    long long int m,n,ans;

    while(cin >> m >>n)
    {
        if( m>n )
        {
            cout << m-n << endl;
        }
        else
        {
            cout << n-m << endl;
        }
    return 0;
    }
}
```

## d065 三人行必有我師

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a,b,c;

    while(cin >> a >> b >> c)
    {
        if(a>b && a>c)
        {
            cout << a << endl;
        }
        else if(b>c)
        {
            cout << b << endl;
        }
        else
        {
            cout << c << endl;
        }
    }

    return 0;
}
```

## a053 Sagit’s 計分程式

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int N;

    while(cin >> N)
    {
        if(N>=0 && N<=10)
        {
            cout << N*6 << endl;
        }
        else if(N<=20)
        {
            cout << 60+(N-10)*2 << endl;
        }
        else if(N<=40)
        {
            cout << 80+(N-20)*1 << endl;
        }
        else
        {
            cout << 100 << endl;
        }
    }

    return 0;
}
```

## d064 ㄑㄧˊ 數？

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int i;

    while(cin>> i)
    {
        if(i%2==1)
        {
            cout << "Odd" << endl;
        }
        else
        {
            cout << "Even" <<endl;
        }
    }
    return 0;
}
```

## d066 上學去吧！

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int hh,mm;

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

## d460 山六九之旅

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a;

    while(cin >> a)
    {
        if(0<=a&&a<=5)
        {
           cout << "0" << endl;
        }
        else if(6<=a&&a<=11)
        {
            cout << "590" << endl;
        }
        else if(12<=a&&a<=17)
        {
            cout << "790" << endl;
        }
        else if(18<=a&&a<=59)
        {
            cout << "890" << endl;
        }
        else if(60<=a)
        {
            cout << "399" << endl;
        }
        else
        {
            cout << " " << endl;
        }
    }
    return 0;
}
```

## a273 小朋友下樓梯

```cpp=
#include <iostream>

using namespace std;

int main()
{
    long long int n,k;

    while(cin >> n >> k)
    {
        if(n==0 && k==0)
        {
            cout << "Ok!" << endl;
        }
        else if(k==0)
        {
            cout << "Impossib1e!" << endl;
        }
        else if(n==0)
        {
            cout << "Ok!" << endl;
        }
        else
        {
            while(n>0)
            {
                n=n-k;

                if(n==0)
                {
                    cout << "Ok!" << endl;
                }
            }
            if(n<0)
            {
                cout << "Impossib1e!" << endl;
            }
        }
    }
    return 0;
}
```

## b186 97七區資訊學科1(改編)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    long long int co,ch,ca;

    while(cin >> co >> ch >>ca)
    {

        if(co/10>=1 && ca/2>=1 && co/10>=ca/2)
        {
            cout << co << " 個餅乾，" << ch+ca/2 << " 盒巧克力，" << ca << " 個蛋糕。" << endl;
        }
        else if(co/10>=1 && ca/2>=1 && co/10<ca/2)
        {
            cout << co << " 個餅乾，" << ch+co/10 << " 盒巧克力，" << ca << " 個蛋糕。" << endl;
        }
        else
        {
            cout << co << " 個餅乾，" << ch << " 盒巧克力，" << ca << " 個蛋糕。"<<endl;
        }
    }
    return 0;
}
```

## a005 Eva 的回家作業

```cpp=
#include <iostream>

using namespace std;

int main()
{
    long long int t,a,b,c,d;

    cin >> t;

    while(t--)
    {
        cin >> a >> b >> c >> d;

        if(d-c==c-b && c-b==b-a)
        {
            cout << a << " " << b << " " << c << " " << d << " " << 2*d-c << endl;
        }
        else
        {
            cout << a << " " << b << " " << c << " " << d << " " << d*d/c << endl;
        }
    }
    return 0;
}
```

## d502 第三題：產品包裝

```cpp=
#include <iostream>

using namespace std;

int main()
{
unsigned int a,b,c,d,abox=0,sum=0;

while(cin >> a >> b >> c >> d)
{
    int em=(c*37)+(64-8*(b%8));

    if(a<=em)
    {
        abox=0;
    }
    else
    {
        if(a>em)
        {
            abox=(a-em)/64;
        }
        if(abox==0)
        {
            abox+=1;
        }
    }

    sum=d+c+b/8+abox;

    if(b%8!=0)
        {
            sum+=1;
        }

    cout << sum << endl;
}
return 0;
}
```

# *III 迴圈*

## EX_05 計算n!

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n,sum=1;

    cout << "請輸入n：";
    cin >> n;

    for(int i=1;i<=n;i++)
    {
        sum*=i;
    }
    cout << sum;
    return 0;
}
```

## EX_06 3層聖誕樹

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    cout << "請輸入n：" ;
    cin >> n ;

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=(n-i)+2;j++)
        {
            cout << " " ;
        }

        for(int j=1;j<=2*i-1;j++)
        {
            cout << "*" ;
        }
        cout << endl;
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=(n-i)+1;j++)
        {
            cout << " " ;
        }

        for(int j=1;j<=(2*i+1);j++)
        {
            cout << "*" ;
        }
        cout << endl;
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++)
        {
            cout << " " ;
        }

        for(int j=1;j<=2*i+3;j++)
        {
            cout << "*" ;
        }
        cout << endl;
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=(2*n+3)/3;j++)
        {
            cout << " " ;
        }

        for(int j=1;j<=(2*n+3)/3;j++)
        {
            cout << "*" ;
        }
        cout << endl;
    }
    return 0;
}
```

## EX_07 存錢買手機(While)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int i=1,sum=0,money;

    while(sum<36000)
    {
        cout << "請輸入第 " << i << " 個月的存款： ";
        cin >> money;
        sum+=money;
        i++;

    }
    cout << "存了 " << i-1 << " 個月，總共 " << sum;
}
```

## EX_08 小算盤開根號

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    bool sol=false;

    cin >> n;

    for(int i=0;i<=n/2;i++)
    {
        if(i*i==n)
        {
            cout << i;
            sol=true;
            break;
        }
    }

    if(!sol)
    {
     cout << "只能求完全平方數的平方根！" << endl;
    }
    return 0;
}
```

## EX_09 小算盤開根號(二分搜尋法)

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

using namespace std;

int main()
{
    double n,lower,upper,mid;
    int cnt=0;

    cin >> n;
    lower=0;
    upper=n;

    while(upper-lower>1e-13)
    {
        mid=(upper+lower)/2;
        cnt++;

        if(mid*mid>n)
            upper=mid;
        else if(mid*mid<n)
            lower=mid;
        else
            break;
    }
    printf("經過了 %d 次運算，根號 %f 為 %.13f", cnt,n,mid);

    return 0;
}
```

## EX_10 小算盤開根號(牛頓法)

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

using namespace std;

int main()
{
    double a,n;
    int cnt=0;

    cin >> n;
    a=n;

    do
    {
        a=(a*a+n)/(2*a);
        cnt++;
    }while(a*a-n>1e-13);

    printf("經過了 %d 次運算，根號 %f 為 %.13f",cnt,n,a);

    return 0;
}
```

## d010 盈數、虧數和完全數

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int N,S=0;

    while(cin >> N)
    {
        for(int i=1;i<N;i++)
        {
            if(N%i==0)
                S+=i;
        }

        if(S<N)
            cout << "虧數" << endl;
        else if(S==N)
            cout << "完全數" << endl;
        else if(S>N)
            cout << "盈數" << endl;

        S=0;
    }
    return 0;
}
```

## d490 我也愛偶數

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

int main()
{
    int a,b;
    while(cin >> a >> b)
    {
        int sum=0;
        for(int i=a;i<=b;i++)
        {
            if(i%2==0)
            {
                sum=sum+i;
            }
        }
        cout << sum << endl;
    }
    return 0;
}
```

## d074 電腦教室

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >>n)
    {
        int m=-1,p;

        for(int i=0;i<n;i++)
        {
            cin >> p;
            if(m<p)
            {
                m=p;
            }
        }
        cout << m << endl;
    }
    return 0;
}
```

## a215 明明愛數數

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n,m;

    while(cin >> n >> m)
    {
        int sum=0,cnt=0;

        do
        {
            sum+=n;
            cnt++;
            n++;
        }while(sum<=m);
    cout << cnt << endl;
    }
    return 0;
}
```

## a024 最大公因數(GCD)-1

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a,b;

    while(cin >> a >> b)
    {
        for(int i=min(a,b);i>=1;i--)
        {
            if(a%i==0 && b%i==0)
            {
                cout << i << " " << endl;
                break;
            }
        }
    }
    return 0;
}
```

## a024 最大公因數(GCD)-2

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a,b;

    while(cin >> a >> b)
    {
        int r;
        do
        {
            r=a%b;
            a=b;
            b=r;
        }while(r>0);
        cout << a << endl;
    }
    return 0;
}
```

## a149 乘乘樂 (1)

```cpp=
#include <iostream>

using namespace std;

int main()
{

    int T;
    cin >> T;

    while(T--)
    {
        int n,sum=1;
        cin >>n;
        do
        {
           sum*=n%10;
           n/=10;
        }while(n>0);
        cout << sum << endl;
    }
    return 0;
}
```

## c418 Bert的三角形 (1)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=i;j++)
            {
                cout << "*";
            }
            cout << endl;
        }
    }
    return 0;
}
```

## c418 Bert的三角形 (2)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                cout << "_";
            }

            for(int j=1;j<=i;j++)
            {
                cout << "*";
            }
            cout << endl;
        }
    }
    return 0;
}
```

## c420 Bert的三角形 (3)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
            {
                cout << "_";
            }

            for(int j=1;j<=2*i-1;j++)
            {
                cout << "*";
            }

            for(int j=1;j<=n-i;j++)
            {
                cout << "_";
            }

            cout << endl;
        }
    }
    return 0;
}
```

## d498 我不說髒話

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        for(int i=n;i>0;i--)
        {
            cout << "I don't say swear words!" << endl;
        }
    }
    return 0;
}
```

## d186 11461 - Square Numbers

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

using namespace std;

int main()
{
    int a,b;

    while(cin >> a >> b)
    {
         if(sqrt(a)!=floor(sqrt(a)))
            cout << floor(sqrt(b))-floor(sqrt(a));
         else if(a!=0 && b!=0)
            cout << floor(sqrt(b))-floor(sqrt(a))+1;

            cout << endl;
    }
    return 0;
}
```

## a248 新手訓練 ~ 陣列應用

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a,b,N;

    while(cin >> a >> b >> N)
    {
        cout << a/b << ".";

        while(N--)
        {
            a=a%b*10;
            cout << a/b;
        }
        cout << endl;
    }
    return 0;
}
```

# *IV 陣列*

:::info
int arr[5];                      // 宣告一個大小為5的int陣列
arr[0]=80;                       // 陣列的索引值由0開始
arr[5]=90;                       // 索引值0~4
int arr[5] =  { 1, 3, 5, 7, 9 }; // 宣告陣列時設定初值
int arr[5] =  { 1 };             // arr[0]初值1，其餘預設為0
int arr[] = { 1, 3, 5, 7, 9 };   // 陣列大小依初值自動計算

int arr2D[4][2];                 // 宣告二維陣列(多維陣列)
int arr2D[4][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
:::

## EX_11 樂透選號器

```cpp=
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int num[6];
    int mark[50]={0};
    int t;
    srand(time(NULL));

    for(int i=0;i<6;i++)
    {
        do
        {
            t=rand()%49+1;
        }while(mark[t]==1);
        num[i]=t;
        mark[t]=1;
    }

    for(int i=0;i<6;i++)
    {
        for(int j=0;j<5;j++)
        {
            if(num[j]>num[j+1])
            {
                int t;
                t=num[j];
                num[j]=num[j+1];
                num[j+1]=t;
            }
        }
    }

     for (int i=0;i<6;i++)
    {
        cout << num[i] << " ";
    }

    return 0;
}
```

## EX_12 求學測總級分和平均

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int score[4][8] = {{12,13,15,10,12,0,0,0},{15,13,15,15,14,0,0,0},{14,14,12,15,13,0,0,0},{11,10,12,12,10,0,0,0}};

    for(int i=0;i<4;i++)
    {
        int sum=0;
        for(int j=0;j<8;j++)
        {
            sum+=score[i][j];
        }
        score[i][5]=sum;
        score[i][6]=sum/5;
    }

    for(int i=0;i<4;i++)
    {
        int p=0;
        for(int j=0;j<4;j++)
        {
            if (score[i][5]<score[j][5])
            {
                p++;
            }
            score[i][7]=p+1;
        }
    }

    cout << "座號\t國文\t英文\t數學\t自然\t社會\t總分\t平均\t名次\n";

    for(int i=0;i<4;i++)
    {
        cout << i+1 << "\t";
        for(int j=0;j<8;j++)
        {
            cout << score[i][j] << "\t";
        }
        cout <<endl;
    }

    return 0;
}
```

## b138 NOIP2005 1.陶陶摘苹果

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int apl[10];
    int h,sum=0;

    for(int i=0;i<10;i++)
    {
        cin >> apl[i];
    }
    cin >> h;
    for(int i=0;i<10;i++)
        {
        if(h+30>=apl[i])
            sum+=1;
        }
        cout << sum << endl;
        sum=0;
    return 0;
}
```

## b127 會議中心（Room）

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        int num[n];
        num[0]=1;
        num[1]=1;

        for(int i=2;i<=n;i++)
        {
            num[i]=num[i-1]+num[i-2];
        }
        cout << num[n] << endl;
    }
    return 0;
}
```

## a034 二進位制轉換

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    int a[128];

    while(cin >> n)
    {
        int i=0;

        while(n>0)
        {
            a[i]=n%2;
            n/=2;
            i++;
        }

        for(int j=i-1;j>=0;j--)
        {
            cout << a[j];
        }
        cout << endl;
    }
    return 0;
}
```

## b139 NOIP2005 2.校门外的树

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int L,M;

    while(cin >> L >> M)
    {
        int l[L+1],sum=0;

        for(int i=0;i<=L;i++)
        {
            l[i]=0;
        }

        for(int j=M;j>0;j--)
        {
            int a,b;

            cin >> a >> b;
            for(int i=a;i<=b;i++)
            {
                l[i]=1;
            }
        }

        for(int i=0;i<=L;i++)
        {
            if(l[i]==0)
            sum+=1;
        }

        cout << sum << endl;
    }
    return 0;
}
```

## d212 東東爬階梯

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    unsigned long long int f[100]={1,1};

    for(int i=2;i<100;i++)
        f[i]=f[i-1]+f[i-2];

    while(cin >> n)
        cout << f[n] << endl;

    return 0;
}
```

## c067 Box of Bricks

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n,Set=1;

    while(cin >> n && n>0)
    {
        int h[n],sum=0,ave,mov=0;

        for(int i=0;i<n;i++)
        {
            cin >> h[i];
            sum+=h[i];
        }

        ave=sum/n;

        for(int i=0;i<n;i++)
        {
            if(h[i]>ave)
            {
                mov+=(h[i]-ave);
            }
        }

        cout << "Set #" << Set++ << endl << "The minimum number of moves is " << mov << ".\n" << endl;
    }
    return 0;
}
```

## a104 排序

:::info

+ [insertion sort插入排序法](https://kopu.chat/2017/06/22/插入排序insertion-sort/)

:::

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;

    while(cin >> n)
    {
        int s[n];

        for(int i=0;i<n;i++)
            cin >> s[i];

        for(int i=1;i<n;i++)
        {
            int k=s[i],j=i-1;

            while(k<s[j] && j>=0)
            {
                s[j+1]=s[j];
                j--;
            }
            s[j+1]=k;
        }
        for(int i=0;i<n;i++)
            cout << s[i] << " ";

        cout << endl;
    }

    return 0;
}
```

## d452 二、直線最小距離和

:::info

+ 排序後求中位數，中位數與數組各數的距離和最小

```cpp=
1. #include <cmath>
2. swap():兩個值交換
```

:::

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

using namespace std;

int main()
{
    int N,m;

    cin >> N;
    while(N--)
    {
        cin >> m;
        int M[m],sum=0;

        for(int i=0;i<m;i++)
            cin >> M[i];

        for(int i=0;i<m-1;i++)
        {
            int minIdx=i;

            for(int j=i+1;j<m;j++)
            {
                if(M[j]<M[minIdx])
                    minIdx=j;
            }
            swap(M[minIdx],M[i]);
        }

        for(int i=0;i<m;i++)
            sum+=abs(M[m/2]-M[i]);
        cout << sum << endl;
    }
    return 0;
}
```

## d153 六、智力测验

:::info

```cpp=
1. #include <algorithm>
2. sort( , ):排序
```

:::

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

using namespace std;

int main()
{
    int T,N;

    cin >> T;

    while(T--)
    {
        cin >> N;
        int s[N];

        for(int i=0;i<N;i++)
            cin >> s[i];

        sort(s,s+N);

        cout << (N%2==1?s[N/2]:s[N/2-1]) << endl;
    }
    return 0;
}
```

## a694 吞食天地二

:::info

+ C++ IO加速

```cpp=
ios::sync_with_stdio(false);
cin.tie(0);
```

:::

```cpp=
#include <iostream>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n,m;
    while(cin >> n >> m)
    {
        int a[n+1][n+1];

        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cin >> a[i][j];

        while(m--)
        {
            int x1,y1,x2,y2,sum=0;
            cin >> x1 >> y1 >> x2 >> y2;

            for(int i=x1;i<=x2;i++)
                for(int j=y1;j<=y2;j++)
                    sum+=a[i][j];

            cout << sum << endl;
        }
    }
    return 0;
}
```

# *V 字元陣列、字串*

:::warning

+ [演算法筆記-字串](https://archive.is/u1lQL)

:::

## a149 乘乘樂 (2)

:::info

```cpp=
#include <cstring>
strlen( )測字串長度
```

:::

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

using namespace std;

int main()
{
    int T;
    cin >> T;

    while(T--)
    {
        int sum=1;
        char n[11];

        cin >> n;
        for (int i=0;i<strlen(n);i++)
            sum*=(int)n[i]-48;

        cout << sum << endl;
    }
    return 0;
}
```

## d124 3的倍数

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

using namespace std;

int main()
{

    char n[10001];
    while(cin >> n)
    {
        int sum=0;

        for (int i=0;i<strlen(n);i++)
            sum+=(int)n[i]-48;

        cout << (sum%3==0?"yes":"no") << endl;
    }
    return 0;
}
```

## a782 4. Redundant Acronym Syndrome Syndrome

:::info

+ toupper()將字元變大寫

:::

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int last;
    string line;

    while(getline(cin,line)&& line!="END")
    {
        cout << (char)toupper(line[0]);
        for(int i=1;i<line.size();i++)
        {
            if(line[i]==' ')
            {
                cout << (char)toupper(line[i+1]);
                last=i+1;
            }
        }
        cout << " ";
        for(int i=last;i<line.size();i++)
            cout << line[i];
        cout << endl;
    }
    return 0;
}
```

## a011 00494 - Kindergarten Counting Game

:::info

+ isalpha()判斷是否為字元

:::

```cpp=
#include <iostream>

using namespace std;

int main()
{
    string line;

    while(getline(cin,line))
    {
        int sum=0;
        
        for(int i=0;i<line.size();i++)
            if(isalpha(line[i])&&!isalpha(line[i-1]))
                sum++;

        cout << sum <<endl;
    }
    return 0;
}
```

## a022 迴文

:::info

```cpp=
#include<algorithm>
reverse(str.begin(),str.end())
```

:::

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

using namespace std;

int main()
{
    string s;

    while(cin >> s)
    {
        string rev=s;

        reverse(s.begin(),s.end());

        cout << (s==rev?"yes":"no") <<endl;
    }
    return 0;
}
```

## a130 12015 - Google is Feeling Lucky

:::info

```cpp=
struct
{
  
  };封裝一組資料
```

:::

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

using namespace std;

int main()
{
    struct www
    {
        string name;
        int v;
    }web[10];

    int t,cs=1;
    cin >> t;

    while(t--)
    {
        int maxx=0;
        for(int i=0;i<10;i++)
        {
            cin >> web[i].name >> web[i].v;

            if(web[i].v>maxx)
                maxx=web[i].v;
        }

        printf("Case #%d:\n",cs++);

        for(int i=0;i<10;i++)
        {
            if(web[i].v==maxx)
                cout << web[i].name << endl;
        }

    }
    return 0;
}
```

# *VI 函式*

## EX_13 高中運動會

```cpp=
#include <iostream>

using namespace std;

int gcd(int a,int b)
{
    int r;
    do
    {
        r=a%b;
        a=b;
        b=r;
    }while(r>0);

    return a;
}

int main()
{
    int a=400, b=200, c=150, d=625, g;

    g=gcd(a,b);
    g=gcd(g,c);
    g=gcd(g,d);
    cout << g;
    return 0;
}
```

## EX_14 寫一函式可以計算n!，並以之求 C(n,k)=n!/(n−k)!∗k!

```cpp=
#include <iostream>

using namespace std;

int f(int n)
{
    int sum=1;
    for(int i=0;i<n;i++)
    {
        sum*=i+1;
    }
    return sum;
}
int main()
{
    int n ,k;
    cin >> n >> k;
    cout << f(n)/(f(n-k)*f(k)) << endl;
    return 0;
}
```

## EX_15 求費式數列第n項

```cpp=
#include <iostream>

using namespace std;

int f(int n)
{
    if(n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return f(n-1)+f(n-2);
}

int main()
{
    int n;
    cin >> n;
    cout << f(n);
    return 0;
}
```

## EX_16 以遞迴改寫 EX_13 高中運動會中的gcd函式

```cpp=
#include <iostream>

using namespace std;

int gcd(int a, int b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

int main()
{
    int num[8] = {400, 200, 150, 625, 600, 100, 15, 20};

    int g=gcd(num[0],num[1]);
    for(int i=2;i<8;i++)
        g=gcd(g,num[i]);
    cout << g;

    return 0;
}
```

## EX_17 以遞迴函式計算 n 層香檳塔，總共有幾個杯子

:::info

+ 遞迴式 : f(n)=1 ,if n==1
　　　　f(n)=f(n-1)+？ ,if n>1

:::

```cpp=
#include <iostream>

using namespace std;

int f(int n)
      {
          if(n==1)
            return 1;
          else
            return f(n-1)+n;
      }

int main()
{
    int n;
    cin >> n;
    cout << f(n);

    return 0;
}
```

## EX_18 (Bonus)：計算 n 層「三角錐形」香檳塔，總共有幾個杯子

:::info

+ ( 第1層1個、2->3、3->6、4->10、5->15、第n層 n*(n+1)/2 個杯子 )
2017的世界紀錄總共杯子數量為50116，疊了幾層?

:::

```cpp=
#include <iostream>

using namespace std;

int f(int n)
      {
          if(n==1)
            return 1;
          else
            return f(n-1)+n*(n+1)/2;
      }

int main()
{
    int n;
    cin >> n;
    cout << f(n);

    return 0;
}
```

## a623 Combination

```cpp=
#include <iostream>

using namespace std;

int f(int k)
{
    int sum=1;
    for(int i=k;i>0;i--)
    {
        sum*=i;
    }
    return sum;
}

int main()
{
    int n,m;

    while(cin >> n >> m)
    {
        cout << f(n)/(f(m)*f(n-m)) << endl;
    }
    return 0;
}
```

## c813 11332 - Summing Digits

```cpp=
#include <iostream>

using namespace std;

int f(int k)
{
    int sum=0;

    while(k>0)
    {
        sum+=k%10;
        k=k/10;
    }
    return sum;
}

int g(int n)
{
    int l=f(n);

    while(l/10!=0)
    {
        l=f(l);
    }
    return l;
}

int main()
{
    int n;
    while(cin >> n && n>0)
    {
        cout << g(n) << endl;
    }
    return 0;
}
```

## b112 5.高中運動會

```cpp=
#include <iostream>

using namespace std;

int gcd(int a,int b)
{
    int r;
    do
    {
        r=a%b;
        a=b;
        b=r;
    }while(r>0);

    return a;
}

int main()
{
    int N,n;

    while(cin >> N)
        {
            int g=0,k,l;

            cin >> k >> l;
            g=gcd(k,l);

            for(int i=2;i<N;i++)
            {
                int x;

                cin >> x;
                g=gcd(x,g);
            }
                cout << g << endl;
        }
    return 0;
}
```

## c002 10696 - f91

```cpp=
#include <iostream>

using namespace std;

int f91(int N)
{
    int res;

    if(N>=101)
    {
        res=N-10;
    }
    else if(N<=100)
    {
        res=f91(f91(N+11));
    }
    return res;
}

int main()
{
    int N;

    while(cin >> N && N>0)
        cout << "f91(" << N << ") = " << f91(N) << endl;
    return 0;
}
```

## d171 飛蛾撲火(二)

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

using namespace std;

int main()
{
    int n,m;
    while(cin >> n >> m)
        {
            cout << floor(m*log10(n)+1) << endl;
        }
    return 0;
}
```

## c039 00100 - The 3n + 1 problem

```cpp=
#include <iostream>

using namespace std;

int cl(int n)
{
    int i=1;

    while(n!=1)
    {
        if(n%2==1)
            n=3*n+1;
        else
            n=n/2;
        i++;
    }
    return i;
}
int main()
{
    int a,b;
    while(cin >> a >> b)
    {
        int x=-1;

        for(int i=min(a,b); i<=max(a,b); i++)
            x=max(x,cl(i));
        cout << a << " " << b << " " << x <<endl;
    }

    return 0;
}
```

## a216 數數愛明明

```cpp=
#include <iostream>

using namespace std;

unsigned long long int f(int n)
{   
    if (n==1)
        return 1;
    else 
        return (n+1)*n/2;
}

unsigned long long int g(int n)
{
    if(n==1)
        return 1;
    else
        return f(n)+g(n-1);
}

int main()
{
    long long int n;

    while(cin >> n)
    {
        cout << f(n) << " " << g(n) << endl;
    }
    return 0;
}
```

# *VII其他*

## a002 簡易加法

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

int main()
{
    int a,b;

    while(cin >> a >> b)
    {
        cout << a+b << endl;
    }

    return 0;
}
```

## a003 兩光法師占卜術

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

using namespace std;

int main(int argc, char *argv[])
{
    int m,d,s;

    while(cin >> m >>d )
    {
        s=(m*2+d)%3;

        if(s==0)
            printf("普通");
        else if(s==1)
            printf("吉");
        else
            printf("大吉");
    }
    return 0;
}
```

## a009 解碼器

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

using namespace std;

int main(int argc, char *argv[])
{
    string a;

    while(getline(cin,a))
    {
        if(a=="EOF")
            break;

        for(int i=0; i<a.size(); i++)
            a[i]=(int)a[i]-7;

        for(int i=0; i<a.size(); i++)
            printf("%c",(char)a[i]);

        cout << endl;
    }
    return 0;
}
```

## d219 00374 - Big Mod

```cpp=
#include <iostream>

using namespace std;

long long int dc(long long int b,long long int p,long long int m)
{
    if(p==1)
        return b%m;
    else if(p==0)
        return 1;
    else
        {
            int half=dc(b,p/2,m);

            if(p%2==0)
                return (half*half%m);
            else
                return (half*half*b%m);
        }
}

int main()
{
    long long int b,p,m;

    while(cin >> b >> p >> m)
    {
        cout << dc(b,p,m) << endl;
    }
    return 0;
}
```

## d984 棄保效應

```cpp=
#include <iostream>

using namespace std;

int main()
{
    unsigned int A,B,C;

    while(cin >> A >> B >> C)
    {
        if(A>B+C || C>A && A>B && A+B>C || B>A && A>C && A+C>B)
        {
            cout << "A" << endl;
        }
        else if(B>A+C || A>B && B>C && B+C>A || C>B && B>A && B+A>C)
        {
            cout << "B" << endl;
        }
        else
        {
            cout << "C" << endl;
        }
    }

    return 0;
}
```
