---
# System prepended metadata

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

---

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

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

[b068: 蛇蛇矩陣](https://judge.tcirc.tw/ShowProblem?problemid=b068)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    int n;
    while(cin >> n){
        bool d=0;//0->,1<-
        int num=1;
        for(int row=0;row<n;row++){
            if(d==0){
                for(int col=0;col<n;col++){
                    cout << num << " ";
                    num++;
                }
                d=1;
            }else{
                for(int col=n-1;col>=0;col--){
                    cout << num+col << " ";
                }
                num+=n;
                d=0;
            }
            cout << "\n";
        }
    }
}
```

[b030: 健康檢查（Ⅰ）](https://judge.tcirc.tw/ShowProblem?problemid=b030)



[b069: 字串串串串串接](https://judge.tcirc.tw/ShowProblem?problemid=b069)

```cpp=
#include<bits/stdc++.h>
using namespace std;
const int max_l=1e4;
int main(){
	string ans,ss,s1,s2,s3,t;	
	int leng=0;
	cin>>s1>>s2>>s3>>t;
	for(int i=0;i<t.length();i+=1){
		if(t[i]-'0'==1) ss=s1;
		if(t[i]-'0'==2) ss=s2;
		if(t[i]-'0'==3) ss=s3;
		if(i==0) ans=ss;
		if(i>0) ans+=ss;	
		}
	cout<<ans<<'\n';			
} 
```

[b053: 正確的數字](https://judge.tcirc.tw/ShowProblem?problemid=b053)



[b071: 電梯 (Elevator)](https://judge.tcirc.tw/ShowProblem?problemid=b071)

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

using namespace std;

int main()
{
    int now=1;
    int n;
    scanf("%d", &n);
    int ans=0;
    for(int i=0;i<n;i++){
        int temp;
        scanf("%d", &temp);
        int d=temp-now;
        now=temp;
        if(d>0){
            ans+=3*d;
        }else{
            ans-=2*d;
        }
    }
    printf("%d", ans);
}
```

[b070: 回文數](https://judge.tcirc.tw/ShowProblem?problemid=b070)

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

using namespace std;

int main()
{
    string num;
    cin >> num;
    bool flag=1;
    for(int i=0;i<=num.size()/2;i++){
        if(num[i]!=num[num.size()-1-i]){
            cout << "NO";
            flag=0;
        }
    }
    if(flag==1){
        cout << "YES";
    }
}
```

[b075: 一元一次方程式](https://judge.tcirc.tw/ShowProblem?problemid=b075)

```cpp=
#include <iostream>

using namespace std;

int main()
{
    string equation;
    cin >> equation;
    int leftx=0,leftsum=0,rightx=0,rightsum=0;
    bool lr=0;
    string temp="";
    for(int i=0;i<equation.size();i++){
        if(equation[i]=='='){
            if(temp!=""){
                if(lr==0){
                        leftsum+=stoi(temp);
                }else{
                    rightsum+=stoi(temp);
                }
                temp="";
            }
            lr=1;
        }else if(equation[i]=='x'){
            if(temp!=""&&temp!="+"&&temp!="-"){
                if(lr==0){
                    leftx+=stoi(temp);
                }else{
                    rightx+=stoi(temp);
                }
            }else if(temp=="-"){
                if(lr==0){
                    leftx--;
                }else{
                    rightx--;
                }
            }else{
                if(lr==0){
                    leftx++;
                }else{
                    rightx++;
                }
            }
            temp="";
        }else if(equation[i]=='+'||equation[i]=='-'){
            if(temp!=""){
                if(lr==0){
                    leftsum+=stoi(temp);
                }else{
                    rightsum+=stoi(temp);
                }
            }
            temp="";
            temp+=equation[i];
        }else{
            temp+=equation[i];
        }
    }
    if(temp!=""){
        if(lr==0){
            leftsum+=stoi(temp);
        }else{
            rightsum+=stoi(temp);
        }
    }
    int x,sum;
    x=leftx-rightx;
    sum=rightsum-leftsum;
    if(x==0&&sum==0){
        cout << "Infinite solutions";
    }else if(x==0){
        cout << "No solution";
    }else{
        int ans=sum/x;
        string out="x=";
        out+=to_string(ans);
        cout << out;
    }
}
```