---
# System prepended metadata

title: 新北高中第一屆校內程式競賽 官方解答

---

### pA 泡芙阿姨的程式作業（Coding Homework） 
```c++
#include <bits/stdc++.h>
#define whitebear ios_base::sync_with_stdio(0), cin.tie(0) , cout.tie(0)
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
 
/*pA Coding Homework*/
inline signed solve(){
    string str;
    cin >> str;
    cout << "Hello, " << str <<".\n";
    return 0;
}

signed main(){
   whitebear; 
   /*int t = 0;
    t = nextint();
    while(t--)*/
        solve();
   return 0;
}
```
### pB 分科，啟動！（Fuck Ceec）
```c++
#include <bits/stdc++.h>
#define whitebear ios_base::sync_with_stdio(0), cin.tie(0) , cout.tie(0)
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define f first
#define s second
#define SP "You can do it!\n"
using namespace std;
 
/*
The Tips of the days
January 31
February 29
March 31
April 30
May 31
June 30
*/

/*pB Fuck Ceec*/
inline signed solve(){
    int m = 0, d = 0;
    cin >> m >> d;
    if ( m == 7 and d == 12 ) cout << SP; //special case, can speed up!
    else{
        if ( m == 7 ){
            cout << 12-d <<"\n";
        }else{
            int days = 12;
            if ( m == 2 ) days += 29-d;
            else (m%2 == 0 ? days += 30-d : days += 31-d);
            for( int i = m+1 ; i < 7 ; i++){
                if ( i == 2 ) days += 29;
                else (i%2 == 0 ? days += 30 : days += 31);
            }
            cout << days << "\n";
        }
    }
    return 0;
}

signed main(){
   whitebear;
   /*int t = 0;
    t = nextint();
    while(t--)*/
        solve();
   return 0;
}
```
### pC 艾恩葛朗密碼機（SAO Password Machine）
```c++
#include <bits/stdc++.h>
#define whitebear ios_base::sync_with_stdio(0), cin.tie(0) , cout.tie(0)
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
 
/*pC SAO Password Machine*/
/*
Tips: ASCII code table
0:48 / 9:57
A:65 / Z:90
a:97 / z:122
*/
inline signed solve(){
    int x = 0, k = 0;
    string str;
    cin >> x >> k >> str;
    int l = str.length();
    vector<char> ans;
    if( x == 1 ){   //encode
        for( int i = 0 ; i < l ; i++ ){
            int now_check = (int)str[i];
            if( now_check >= 48 and now_check <= 57 ){  // is number
                for ( int j = 0 ; j < k ; j++ ){
                    if( now_check > 57 ){
                        now_check = 48; 
                    }else ++now_check;
                }
                if( now_check > 57 ) now_check = 48;
                ans.push_back((char)now_check);
            }else if( now_check >= 65 and now_check <= 90 ){ // is upper
                for ( int j = 0 ; j < k ; j++ ){
                    if( now_check > 90 ){
                        now_check = 65; 
                    }else ++now_check;
                }
                if( now_check > 90 ) now_check = 65;
                ans.push_back((char)now_check);
            }else if( now_check >= 97 and now_check <= 122 ){ // is lower
                for ( int j = 0 ; j < k ; j++ ){
                    if( now_check > 122 ){
                        now_check = 97; 
                    }else ++now_check;
                }
                if( now_check > 122 ) now_check = 97;
                ans.push_back((char)now_check);
            }
        }
    }else if( x == 2 ){ //decode
        for( int i = 0 ; i < l ; i++ ){
            int now_check = (int)str[i];
            if( now_check >= 48 and now_check <= 57 ){  // is number
                for ( int j = 0 ; j < k ; j++ ){
                    if( now_check < 48 ){
                        now_check = 57; 
                    }else --now_check;
                }
                if( now_check < 48 ) now_check = 57;
                ans.push_back((char)now_check);
            }else if( now_check >= 65 and now_check <= 90 ){ // is upper
                for ( int j = 0 ; j < k ; j++ ){
                    if( now_check < 65 ){
                        now_check = 90; 
                    }else --now_check;
                }
                if( now_check < 65 ) now_check = 90;
                ans.push_back((char)now_check);
            }else if( now_check >= 97 and now_check <= 122 ){ // is lower
                for ( int j = 0 ; j < k ; j++ ){
                    if( now_check < 97 ){
                        now_check = 122; 
                    }else --now_check;
                }
                if( now_check < 97 ) now_check = 122;
                ans.push_back((char)now_check);
            }
        }
    }
    for ( auto i : ans ){
        cout << i;
    }cout << "\n";
    return 0;
}

signed main(){
   whitebear;
   /*int t = 0;
    t = nextint();
    while(t--)*/
        solve();
   return 0;
}
```
### pD 2048 滑塊遊戲（2048 Games）
```c++
#include <bits/stdc++.h>
#define int long long
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define f first
#define s second
#define whitebear ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
using namespace std;

/*pD 2048 Games*/
//1 up 2 down 3 left 4 right

inline signed solve(){
    vii board(4,vector<int>(4,0));
    int move = 0;
    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 4;j++){
            cin >> board[i][j];
        }
    }
    cin >> move;
    if(move == 1){
        for(int i = 0;i < 4;i++){
            deque<int> temp;
            bool flag = 1;
            for(int t = 0;t < 4;t++) temp.push_back(board[t][i]);
            for(int i : temp){
                if(i == 0){
                    flag = 0;
                    break;
                }
            }
            if(!flag){
                int t = 4;
                while(t--){
                    for(int i = 0;i < 3;i++){
                        if(temp[i] == 0){
                            temp[i] = temp[i+1];
                            temp[i+1] = 0;
                        }
                    }
                }
            }
            for(int j = 0;j < 3;j++){
                if(temp[j] == temp[j+1]){
                    temp[j] = 0;
                    temp[j+1] *= 2;
                }
            }
            int t2 = 4;
            while(t2--){
                for(int i = 3;i > 0;i--){
                    if(temp[i-1] == 0){
                        temp[i-1] = temp[i];
                        temp[i] = 0;
                    }
                }
            }
            for(int t = 0;t < 4;t++){
                board[t][i] = temp[t];
            }
        }
    }else if(move == 2){
        for(int i = 0;i < 4;i++){
            deque<int> temp;
            bool flag = 1;
            for(int t = 0;t < 4;t++) temp.push_back(board[t][i]);
            for(int i : temp){
                if(i == 0){
                    flag = 0;
                    break;
                }
            }
            if(!flag){
                int t = 4;
                while(t--){
                    for(int i = 3;i > 0;i--){
                        if(temp[i] == 0){
                            temp[i] = temp[i-1];
                            temp[i-1] = 0;
                        }
                    }
                }
            }
            for(int j = 3;j > 0;j--){
                if(temp[j] == temp[j-1]){
                    temp[j - 1] = 0;
                    temp[j] *= 2;
                }
            }
            int t2 = 4;
            while(t2--){
                for(int i = 0;i < 3;i++){
                    if(temp[i+1] == 0){
                        temp[i+1] = temp[i];
                        temp[i] = 0;
                    }
                }
            }
            for(int t = 0;t < 4;t++){
                board[t][i] = temp[t];
            }
        }
    }else if(move == 3){
        for(int i = 0;i < 4;i++){
            deque<int> temp;
            bool flag = 1;
            for(int t = 0;t < 4;t++) temp.push_back(board[i][t]);
            for(int i : temp){
                if(i == 0){
                    flag = 0;
                    break;
                }
            }
            if(!flag){
                int t = 4;
                while(t--){
                    for(int i = 3;i > 0;i--){
                        if(temp[i-1] == 0){
                            temp[i-1] = temp[i];
                            temp[i] = 0;
                        }
                    }
                }
            }
            for(int j = 0;j < 3;j++){
                if(temp[j] == temp[j+1]){
                    temp[j+1] = 0;
                    temp[j] *= 2;
                }
            }
            int t2 = 4;
            while(t2--){
                for(int i = 3;i > 0;i--){
                    if(temp[i-1] == 0){
                        temp[i-1] = temp[i];
                        temp[i] = 0;
                    }
                }
            }
            for(int t = 0;t < 4;t++){
                board[i][t] = temp[t];
            }
        }
    }else if(move == 4){
        for(int i = 0;i < 4;i++){
            deque<int> temp;
            bool flag = 1;
            for(int t = 0;t < 4;t++) temp.push_back(board[i][t]);
            for(int i : temp){
                if(i == 0){
                    flag = 0;
                    break;
                }
            }
            if(!flag){
                int t = 4;
                while(t--){
                    for(int i = 0;i < 3;i++){
                        if(temp[i+1] == 0){
                            temp[i+1] = temp[i];
                            temp[i] = 0;
                        }
                    }
                }
            }
            for(int j = 3;j > 0;j--){
                if(temp[j] == temp[j-1]){
                    temp[j-1] = 0;
                    temp[j] *= 2;
                }
            }
            int t2 = 4;
            while(t2--){
                for(int i = 0;i < 3;i++){
                    if(temp[i+1] == 0){
                        temp[i+1] = temp[i];
                        temp[i] = 0;
                    }
                }
            }
            for(int t = 0;t < 4;t++){
                board[i][t] = temp[t];
            }
        }
    }

    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 4;j++){
            cout << board[i][j] << " ";
        }cout << "\n";
    }
    return 0;
}

signed main(){
    whitebear;
    /*
    int t = 0;
    cin >> t;
    while(t--)*/
        solve();
    return 0;
}
```
### pE 地震訊號偵測（Earthquake Detector） 
```c++
#include <bits/stdc++.h>
#define int long long
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define f first
#define s second
#define whitebear ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
using namespace std;

bool compare(pair<int,pii> a, pair<int,pii> b){
    return a.f > b.f;
}

/*pE Earthquake Detecter*/
inline signed solve(){
    int k = 0;
    cin >> k;
    pair<int,pii> fix[k-1];
    vi data(k,0);
    for(int i = 0;i < k;i++) cin >> data[i];
    int cnt = 0;
    for(int i = 0;i < k-1;i++){
        int temp = abs(data[i+1] - data[i]);
        fix[cnt].f = temp;
        fix[cnt].s.f = i+1;
        fix[cnt].s.s = i+2;
        cnt++;
    }
    stable_sort(fix,fix+k-1,compare);
    for(int i = 0;i < 3;i++){
        cout << fix[i].f <<" "<< fix[i].s.f <<" "<< fix[i].s.s <<" ";
        cout << "\n";
    }
    return 0;
}

signed main(){
    whitebear;
    /*
    int t = 0;
    cin >> t;
    while(t--)*/
        solve();
    return 0;
}
```
### pF 括號對對碰（Brackets Offset）
```c++
#include <bits/stdc++.h>
#define whitebear ios_base::sync_with_stdio(0), cin.tie(0) , cout.tie(0)
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define f first
#define s second
#define N "No\n"
#define Y "Yes\n"
using namespace std;
 
/*pF Brackets Offset*/
inline signed solve(){
    string s;
    cin >> s;
    stack<int> ans;
    if ( s[0] == ')') cout << N; //special case, can speed up!
    else{
        bool flag = 0;
        for(char i : s){
            if ( i == '(' ) ans.push(1);
            else{
                if ( ans.empty() ){
                    flag = 1;
                    break;
                }
                ans.pop();
            }
        }
        if (flag == 1 or !ans.empty()) cout << N;
        else cout << Y;
    }
    return 0;
}

signed main(){
    whitebear;
    int t = 0;
    cin >> t;
    while(t--)
        solve();
   return 0;
}
```