#40703: 拯救世界


4567891230 (unknown)

學校 : 不指定學校
編號 : 274147
來源 : [46.165.250.81]
最後登入時間 :
2024-06-07 16:59:27
e600. Unicode (萬國碼) -- 트와이스 | From: [122.100.75.70] | 發表日期 : 2024-06-07 01:53

1  c039
// P01 3N+1問題
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int i, j;
while(cin >>i >>j){
int m = min(i, j);int M = max(i,j);int cycle = 0;
for (int k = m; k <= M; k++){
int n= k;int t =0;while (1){
t++;
if (n ==1) break;
if(n%2) n=3*n+1;
else n=n/2;
}
cycle = max(cycle, t);
}
cout <<i<<" "<< j <<" "<< cycle << endl;
}
return 0; 
}

2   c007
//PO2TEX的引號
#include <iostream>
using namespace std;
int main(){
char c;
bool flag = true;
while (cin.get(c)){
if(c =='"'){
cout << (flag ? "``" : "''");
flag = !flag;
}
else cout << c;
}
return 0;
}

3   c054
// P03 WERTYU
#include <iostream>
using namespace std;//查韵表七宣告
string t ="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
int main(){
char c;
while (cin.get(c)){
if (c ==' '|| c=='\n')
cout << c;
else {
int p=t.find(c);
cout << t[p -1];
}
}
return 0;
}

4   e543
//PO4 迴文與鏡像詞
#include <iostream>
#include <algorithm>
#include <string>
#define ALL(x) x.begin(), x.end()
using namespace std;

string nt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
string mt = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
bool isP(string s);
bool isM(string s);

int main(){
    string s;
    while (cin >>s){
        bool p = isP(s);
        bool m = isM(s);
        cout <<s<<" -- is ";
        if (p && m) cout << "a mirrored palindrome.";
        else if (p) cout << "a regular palindrome.";
        else if (m) cout << "a mirrored string.";
        else cout << "not a palindrome.";
        cout <<"\n\n";
    }
    return 0;
}
bool isP(string s) {
    string rs = s;
    reverse(ALL(rs));
    return rs == s;
}
bool isM(string s){
    string rs = s;
    reverse(ALL(rs));
    for (int i =0; i< s.size();i++){
        int p= nt.find(rs[i]);
        rs[i] = mt[p];
    }
    return rs == s;
}

5   d132
// PO5 猜數字遊戲
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int game = 0;
    int n;
    while (cin >>n && n != 0) {
        printf("Game %d:\n",++game);
        vector<int> s(n);
        for (int i=0; i < n; i++) cin >>s[i];
     
        vector<int>g(n);
        while (true){
    //讀猜題順便检查A
            int A=0,B=0,z=0;
            for (int i = 0; i <n; i++) {
                cin >>g[i];
                if (s[i] == g[i]) A++;
                if (g[i] == 0)    z++;
    // 检查 日的數量是否達到n
            }
            if (z == n) break;
    //检查 B:先检查出题&猜题1-9出現次数,再扣除A
            for (int d = 1; d <= 9; d++) {
                int sc = 0, gc = 0;
                for (int i = 0; i<n; i++){
                    if (s[i] ==d) sc++;
                    if (g[i]== d) gc++;
                }
                B=B+ min(sc, gc);
            }
            B=B-A;
            printf("   (%d,%d)\n",A,B);
        }
    }
    return 0;
}

6   e558
//p06
#include <iostream>
using namespace std;
#define SIZE 100001

int counts[SIZE];

void Initialize() {
    int sum;
    for (int i = SIZE - 1, j; i; --i) {
        j = i, sum = i;
        while (j)
            sum += j % 10, j /= 10;
        if (sum < SIZE)
            counts[sum] = i;
    }
}

int main() {
    cin.sync_with_stdio(false), cin.tie(nullptr);
    int times, number;
    Initialize();
    cin >> times;
    while (times--) 
        cin >> number, cout << counts[number] << '\n';
}

7   b123
//p07
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main() {
    cin.sync_with_stdio(false); cin.tie(nullptr);
    int rows, columns, pointer, maximum, area, index;
    int counts[101][101] = {}, xs[101], ys[101];
    while (cin >> rows >> columns) {
        for (int i = 1; i <= rows; ++i)
            for (int j = 1; j <= columns; ++j) {
                cin >> counts[i][j];
                if (counts[i][j])
                    counts[i][j] += counts[i - 1][j];
            }
        maximum = pointer = 0;
        for (int i = rows; i >= 1; --i) {
            for (int j = 1; j <= columns; ++j) {
                index = j;
                while (pointer && counts[i][j] < ys[pointer - 1]) {
                    --pointer; area = (j - xs[pointer]) * ys[pointer];
                    maximum = max(maximum, area);  index = xs[pointer];
                }
                if (!pointer || counts[i][j] > ys[pointer - 1])
                    xs[pointer] = index, ys[pointer] = counts[i][j], ++pointer;
            }
            while (pointer) {
                --pointer;  area = (columns - xs[pointer] + 1) * ys[pointer];
                maximum = max(maximum, area);
            }
        }
        cout << maximum << '\n';
    }
}

8   j121
//P08
#include <iostream>
using namespace std;
 
int a[26], b[26], n[105], m[105], l;
string s1, s2;
 
int main() {
    while (cin >> s1 >> s2){
        l = s1.length();
        for (int i = 0; i < 26; i++){
            a[i] = 0;
            b[i] = 0;
        }
        for (int i = 0; i < l; i++){
            a[s1[i]-'A']++;
            b[s2[i]-'A']++;
            n[i] = 0;
            m[i] = 0;
        }
        for (int i = 0; i < 26; i++){
            n[a[i]]++;
            m[b[i]]++;
        }
        bool flag = true;
        for (int i = 0; i < l; i++){
            if (n[i] != m[i]){
                flag = false;
                break;
            }
        }
        if (flag) cout << "YES\n";
        else cout << "NO\n";
    }
}

9   e541
// Pe9 大理石在哪裡
#include <iostream>
#include <vector>
#include <algorithm>
#define ALL(x)x.begin(),x.end()
using namespace std;

int main() {
    int kase = 0;
    int N, Q;
    while (cin >> N >> Q && N !=0) {
        printf("CASE# %d:\n",++kase);
        //讀大理石並升幂排列
        vector<int> v(N);
        for (int n = 0; n < N; n++) cin >>v[n];
        sort(ALL(v));
        //回答Q個問題
        while (Q--) {
            int f;
            cin >>f;
            // lower_bound輸出為指操·减去v.begin()位址轉為int
            int p = lower_bound(ALL(v),f) - v.begin();
            if (p<N && v[p] ==f)
                printf("%d found at %d\n",f,(p +1));
            else
                printf("%d not found\n",f);
        }
    }    
    return 0;   
}

10 c073
#include <iostream>
 #include <vector>
 using namespace std;
 
 void find_block(int a, int &pa, int &ha);
 void clear_above(int p, int h);
 void pipe_over(int p0, int h0, int p1);
 void show(void);
 
 int n;
 vector<int> pipe[25];
 
 int main(void){
 int i;
 string s1, s2;
 int a, b;
 int pa, pb; 
 int ha, hb; 


 ios::sync_with_stdio(0);
 cin.tie(0);

 while(cin >> n){
 for(i=0; i<n; i++){
 pipe[i].clear();
 pipe[i].push_back(i);
 }
 
 while(1){
 cin >> s1;
 if(s1 == "quit") break;
 
 cin >> a >> s2 >> b;
 find_block(a, pa, ha);
 find_block(b, pb, hb);
 
 if(pa == pb) continue; 

if(s1 == "move") clear_above(pa, ha);
 if(s2 == "onto") clear_above(pb, hb);
 pipe_over(pa, ha, pb);
 } 
 show();
 }
 
 return 0;
 }

 void find_block(int a, int &p, int &h){
 for(p=0; p<n; p++){
 for(h=0; h<pipe[p].size(); h++){
 if(pipe[p][h] == a) return;
}
 }
 }
 
 
 void clear_above(int p, int h){
 for(int i=pipe[p].size()-1; i>h; i--){
 pipe[pipe[p][i]].push_back(pipe[p][i]);
 pipe[p].pop_back();
 }
 }
 
 
 void pipe_over(int p0, int h0, int p1){
 pipe[p1].insert(pipe[p1].end(),
 pipe[p0].begin()+h0,
 pipe[p0].end());
 
 pipe[p0].erase(pipe[p0].begin()+h0, pipe[p0].end());
 }
 
 
 void show(void){
 int i, j;
 
 for(i=0; i<n; i++){
 cout << i << ":";
 for(j=0; j<pipe[i].size(); j++){
 cout << " " << pipe[i][j];
 }
 cout << '\n';
 }
 }

11 e155
// P11丢掉卡片
#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n;
    bool first;
    while (cin >>n && n !=0){
        queue<int> q;
        for (int i =1; i <= n; i++)q.push(i);
        
        cout << "Discarded cards: ";
        
        first = true;
        while(q.size()>= 2){
        //丢掉最上面的那張牌
            if (first) first = false;
            else cout <<", ";
            cout << q.front();q.pop();
            //把目前最上面的牌放到牌堆最下面
            q.push(q.front());q.pop();
        }    
        cout << endl;
        cout << "Remaining card: "<< q.front()<< endl;
    }        
    return 0;
}

12 f166
//P12傅送點
#include <iostream>
#include <queue>
using namespace std;

int S[1000000];//傳送點記錄矩陣
int dis[1000000];//移動記錄

int main(){
    int n,P,L, R;
    queue<int> q;
    int s, sL, sR;
    //輸入傳送座標
    cin >>n >>P>>L >>R;
    for (int i =0; i<n; i++) cin >>S[i];
    
    q.push(0);//加入起始位置(座標0)
    while (true) {
        if(q.empty()){
            cout << -1;
            break;
        }    
        s = q.front();q.pop();
        if(s == P){
            cout << dis[s];
            break;
        }    
    // 检查當下位置是否為目的地
        sL=s-L;
        if (sL >= 0) {
            sL = S[sL];
   
            if (sL >= 0 && sL < n && dis[sL] == 0) {
                dis[sL] = dis[s] + 1;
                q.push(sL);
            }
        }    
        sR = s + R ;
        if (sR < n){
            sR = S[sR];
            if (sR >= 0 && sR < n && dis[sR] == 0) {
                dis[sR] = dis[s] + 1;
                q.push(sR);
            }
        }
    }

    return 0;  
}

13 b838
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
int t;
cin >>t;//讀取問題數量

while (t--){
    string s;
    cin >> s;
    stack<char> up;// 储存上引號出現次數的容器
    bool valid = true;//检验括號组合是否還有效
    int ans = 0;

    for (auto c :s) {
        if (c == '(')up.push('('); //讀取到(
        else{ 
            if (!up.empty()&& up.top() == '('){
                ans++;
                up.pop(); 
            }    
            else{
//检查容器並計算合理括號
                valid = false;
                break;
            }
        }
    }

    if (!up.empty())valid =false;
    cout << (valid ? ans : 0)<< endl;
}
return 0;
}

14 h028
#include <iostream>
#include <stack>
#include <algorithm>
#include <climits>
using namespace std;

pair<int, int> T[100002];// 1st 存座標 2nd 存橱高 TC188082]右侧强多地

int main(){
    int N, L;
    int total = 0, height = 0;
    stack<pair<int, int>> s;//
    
    cin >>N >> L;
//開始繪製林場:左端標記
    T[0].first =0;
//讀取 樹木数量和林場最右邊單位座標
    T[0].second = INT_MAX;

    for (int i = 1 ; i <= N; i++)cin >> T[i].first;
    for (int i = 1 ; i <= N; i++)cin >> T[i].second;
// 右端標記
    T[N + 1].first = L;
    s.push(T[0]);
    for (int i =1; i <= N; i++) {
// 检查往左右砍
        bool left = T[i].first - T[i].second >= s.top().first;
        bool right = T[i].first + T[i].second <= T[i + 1].first;
        if (left || right){
            total++;
            height = max(height,T[i].second);//更新砍倒最高樹木
            
            while (s.top().first + s.top().second <= T[i +1].first)
{        
// 检查前一棵樹往右砍是否不會壓到1+ 1的樹
                total++;
                height = max(height,s.top().second);
                s.pop();//前面樹木倒下
            }
        }
        else{        
           s.push(T[i]);
        }
    }    
    cout << total << endl << height;
    return 0;
}

15 d217
#include <iostream>
#include <string>
#include <set>
using namespace std;
#define ALL(x)x.begin(),x.end()
int main() {
int round;
while (cin >> round && round != -1){

string s,g;
int lc = 7;
cin >>s >>g;
// 敗北條件 lc: loss condition
printf("Round %d\n",round);
// 删除重複字元(題目)uc:unique characten
set<char> uc(ALL(s));

set<char> ag;
for (int i = 0; i<g.size(); i++){

if (ag.count(g[i]))  continue;
ag.insert(g[i]);



if (uc.count(g[i])) uc.erase(g[i]);
else    lc--;

//没有則畫一筆
// !lc:lc為0時成立 | uc.empty():題目被猜完
if (!lc || uc.empty()) break;
}

if (uc.empty())         cout << "You win.\n";
else if (!lc)        cout << "You lose.\n";
else    cout << "You chickened out.\n";
}
return 0;
}

16 f698
//P16 後序運算式
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main(){
    // 運算符號
    string ops ="+-*/";
    // 數字記錄(stack)
    stack<int> num;
    // 讀取資料
    string s;
    while(cin >>s){
        int op = ops.find(s);
        if(op ==-1)
            num.push(stoi(s));//否則將資料轉數字储存
        else {//讀到運算就彈出2數字開始計算
            int n2 = num.top(); num.pop();
            int n1 = num.top(); num.pop();
            switch (op){           
            case 0:
               num.push(n1 + n2); break;
            case 1:
               num.push(n1 - n2); break;
            case 2:
               num.push(n1 *n2); break;
            default:
               num.push(n1 /n2);
            }
        }
    }   
    cout << num.top();
    return 0;
}

17 d244

// P17 一堆石頭
#include <iostream>
#include <map>
using namespace std;

int main(){
    map<int,int>st;// map<石頭编號(索引值),個數(储存值)>
    int n;
//讀取所有存在石頭的編號與個數
    while (cin >>n) st[n]++;
// 找尋不足 3颗的石頭
    for (auto p :st) {
//餘數為日代表刚好 3颗·日為FALSE 故不印出任何资訊
        if(p.second % 3){
            cout << p.first;break;
        }
    }

return 0;
}

18 e564
//P18團體佇列
#include <iostream>
#include <string>
#include <map>
#include <queue>
using namespace std;

int main(){
    int t,n,x;
    int t0;
    int kase = 0;
// t:團隊數量;n:團隊成員數;×:成員编號;
//處理當下成員的團隊编號
    while (cin >>t && t){ //團隊數量日為結束程式
// map<成員(索引值),所屬團隊(储存值)>;
        map<int, int> team;
        for (int i = 0; i<t; i++){
            cin >>n;
            while (n--){
                cin >>x;
                team[x] = i; //給予成員團隊的編號
            }
        }    
        queue<int> tQueue;// tQueue:正在排隊隊伍的團隊編號
        queue<int> eQueue[1000]; // eQueue[i]:正在排隊隊伍的團隊成員
        printf("Scenario #%d\n",++kase);
        string cmd;
        while (cin >> cmd && cmd  != "STOP" ){//字串STOP為停止處理命令
            if (cmd =="ENQUEUE"){ // ENQUEUE:加入排隊隊伍
                cin >> x;
                t0 = team[x];// 取得進入排隊成員的 團隊編號
                if (eQueue[t0].empty())//成員 團隊编號 不在排隊隊伍時
                    tQueue.push(t0);
                eQueue[t0].push(x);// 團隊編號 加入排隊隊伍
            }
            else {// 成員編號 加入排隊隊伍
                t0 = tQueue.front();// DEQUEUE:離開排隊隊伍
// 取得即將離隊成員的 團隊編號
                cout << eQueue[t0].front()<<"\n";
                eQueue[t0].pop();
                if (eQueue[t0].empty())
                    tQueue.pop();
            }
        }            

        cout <<"\n";
    }    
return 0;
}

19 e548
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
    int main(){
        int n;
        while (cin >>n){
// ans[ol; stack; ans[1]: queue;,ans[2]: pq
        bool ans[] = { true, true, true };
        stack <int> s;
        queue <int> q;
        priority_queue<int> pq;
        while (n--){
// t==1:input; t==2:output;
            int t, x;
            cin >> t>>x;
            if (t==1){
                s.push(x);
                q.push(x);
                pq.push(x);
            }    
            else {
                if (ans[0]){
                    if (!s.empty()&& x== s.top())s.pop();
                    else ans[0]= false;
                }    
                if (ans[1]){
                    if (!q.empty()&& x== q.front())q.pop();
                    else ans[1] = false;
                }    
                if(ans[2]) {
                    if (!pq.empty() && x == pq.top()) pq.pop();
                    else ans[2] = false;
                } 
            }    
        }
        if ( ans[0] && !ans[1] && !ans[2])// 100
            cout << "stack\n";        
        else if (!ans[0] && ans[1] && !ans[2]) // 010 
            cout << "queue\n";
        else if (!ans[0] && !ans[1] && ans[2]) // 001 
            cout << "priority queue\n";
        else if (!ans[0] && !ans[1] && !ans[2]) // 000
            cout <<"impossible\n";
        else     
            cout << "not sure\n";
    }        
// 111 110 011
    return 0;
}

20 d221
// P20加媳
#include <iostream>
#include <queue>
using namespace std;

int main(){
    int N, d; // N:運算數字數量;d:運算數字
    while (cin >>N && N){
    // 讀取要運算數字※priority_queue 會自己排序
    // priority_queue<變數類別模板,容器類別模板,比較器模板>
    // 比較器:預設 less<int>降幕排列;本題 greater<int>為升幂
        priority_queue<int, vector<int>, greater<int>> pq;
        for (int i =0;i<N; i++){
            cin >> d;
            pq.push(d);
        }    
    // 運算處理 
        long long sum, cost = 0;
        while (pq.size()!= 1){
    // 計算加總
            sum = pq.top();            pq.pop();
            sum = sum + pq.top();      pq.pop();
       // 計算代價(cost)
            cost = cost + sum;
            pq.push(sum);
        }   
        cout << cost <<"\n";
    }    
    return 0;
}

21 h083
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define ALL(x)x.begin(),x.end()
int main(){
// 讀取所有的籤
int m;
vector<string> v;
 // 宣告籤筒
cin >>m;
while(m--){
string s;
cin >>s;
v.push_back(s);// 放入籤筒
}
sort(ALL(v));
//检查 聖篓的籤数(規则 1&2皆成立才會是聖篓)
int ans = 0;
for (auto s : v) {
int len = s.size();
for (int i= 1; i <= len / 2;i++){
string sa = s.substr(0,i);
string sc = s.substr(len - i,i);
if (sa == sc) {
string sb = s.substr(i, len - 2*i);//[B]段
if (binary_search(ALL(v),sb))
ans++;
}
}
}
cout << ans;
return 0;
}

22 f640
#include <iostream>
#include <string>
using namespace std;
int func();//本题使用到的遞迴副程式
int main(){
cout << func()<<"\n";
return 0;
}
int func(){
string token;
int x,y,z;
cin >> token;
// token =="f"
if (token == "f"){
x= func();
return 2 * x-3;
}
if (token == "g"){
x = func();
y = func();
return 2*x+y-7;
}
if(token =="h"){
x = func();
y= func();
z = func();
return 3*x-2*y+ z;
}
return stoi(token);
}

23 f637
// P23 DF-expression
#include <iostream>
#include <string>
using namespace std;
int decode(int n); // 遞迴用解碼副程式
string s;
int p;
int main(){
    int n;
    cin >>s;
    cin >> n;
    p = -1;// 初始值設定為[-1]位置的字元為初始值
    cout << decode(n)<< "\n";
    return 0;
}    
//解碼:0:白色;1:黑色;2:切成 4 等分;
int decode(int n) {
    int black = 0;
    p++;
    if (s[p] =='0')
        black = 0;
    else if (s[p] =='1')
        black =n * n;
    else {
        for (int i = 0; i <= 3; i++)
            black = black + decode(n / 2);
    }        
    return black;
}

24 k733
//P24磁軌移動序列
#include <iostream>
#include <string>
using namespace std;
long long loop(int &head, int &tail);//主要處理
string s0;
int tape = 0;

int main(){
    int h, t;// head & tail
    long long walk= 0;//記錄移動距離
    cin >> s0;
    walk = loop(h,t);//輪入列車命令
    walk= walk + abs(h - 10);
    cout << walk;
    return 0;
}   
long long loop(int &head, int &tail){
    //遞迴副程式資料處理初始化
    long long walk = 0, subwalk;
    int num,h, t;
    head = tail =-1;
while (1){
//結束點1:tape ==s.size()
    if (tape == s0.size())  return walk;
    if (s0[tape] == 'T'){

// 取得列車起點位置
// 一般命令處理
        num = stoi(s0.substr(tape + 1,2));
        tape = tape + 3;
        if (tail ==-1)
            head = num;
        else
            walk = walk + abs(num - tail);//計算移動距離walk 
        tail = num;
    }    
//最末端設為 num
    else if (s0[tape] =='L'){ //迴圈命令處理(開端)
        num = stoi(s0.substr(tape + 1,1));
        tape = tape + 2;
        subwalk = loop(h, t);
        if (tail ==-1)//移動命令讀取指標
            head = h;//透過遞迴處理 Lx...E
        else// 迴圈命令處理(末端)
            walk = walk + abs(h - tail);
        tail = t;
// walk + subwalk*num 為LX...E的移動距離
// abs(t - h)*(num - 1)為 TxxE 回到 LxTxx時的移動距離
        walk = walk + subwalk * num + abs(t - h) * (num - 1);
    }    // Ex:L2T21T23E->21>23>21>23->移動距離6 
    else {// se[tape] == 'E
       tape++;
//移動命令讀取指標
       return walk; // 回傳 walk
    }
    }
    }
 
ZeroJudge Forum