---
# System prepended metadata

title: C++第二週練習題目參考解答

---

---
title: C++第二週練習題目參考解答
---

# C++第二週練習題目參考解答

## b013: 閏年問題

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int year;
    cin >> year;
    if(year%4==0){
        if(year%100==0){
            if(year%400==0){
                cout << "閏年";
            }else{
                cout << "平年";
            }
        }else{
            cout << "閏年";
        }
    }else{
        cout << "平年";
    }
}
```

## b014: 串串的三角形

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    if(a+b>c){
        if(a*a+b*b==c*c){
            cout << "Right Triangle";
        }else if(a*a+b*b>c*c){
            cout << "Acute Triangle";
        }else{
            cout << "Obtuse Triangle";
        }
    }else{
        cout << "NULL";
    }
}
```

## b017: 病毒的增生

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int m;
    cin >> m;
    int ans=1;
    for(int i=0;i<m;i++){
        ans*=2;
    }
    cout << ans;
}
```

## b021: 電電找因數

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){
        if(n%i==0){
            cout << i << " ";
        }
    }
}
```

## b020: 慎思湖的倒影

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int l;
    cin >> l;
    for(int i=1;i<=l;i++){
        for(int j=1;j<=i;j++){
            cout << "*";
        }
        cout << "\n";
    }
    for(int i=1;i<=l;i++){
        for(int j=l-i;j>=0;j--){
            cout << "*";
        }
        cout << "\n";
    }
}
```

## c003: Ⅲ.相親相愛

```cpp=
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int n,m;
    cin >> m >> n;
    int msum=0;
    for(int i=1;i<=(int)sqrt(m);i++){
        if(m%i==0){
            msum+=i;
            msum+=m/i;
        }
    }
    int nsum=0;
    for(int i=1;i<=(int)sqrt(n);i++){
        if(n%i==0){
            nsum+=i;
            nsum+=n/i;
        }
    }
    if(msum==nsum){
        cout << "Yes";
    }else{
        cout << "No";
    }
}
```