#36014: 需要答案的可以看一下


samlin961112@gmail.com (林哲甫)

學校 : 新北市私立南山高級中學
編號 : 220506
來源 : [123.252.121.18]
最後登入時間 :
2024-11-21 19:33:28
k899. 走格子飛行棋遊戲 -- 三國迷李牧粉題集 | From: [219.70.213.92] | 發表日期 : 2023-06-29 23:54

其實題目不難

只是很雜

以下是我的code

#include <bits/stdc++.h>

using namespace std;
#define int long long
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<string> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<string> b(n + 1); //格子
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    b[c] = a[i];
  }
  int coin;
  cin >> coin;
  int hp;
  cin >> hp;
  int s;
  cin >> s;
  int nows = s;
  int maxhp = hp;
  int minhp = hp;
  int maxcoin = coin;
  int mincoin = coin;
  cout << "初始金幣值為" << coin << "\n";
  cout << "初始血量值為" << hp << "\n";
  cout << "初始位置為" << s << "\n";
  cout << "初始名稱位置為" << b[s] << "\n";
  string use;
  cin.ignore();
  while (getline(cin, use)) {
    if (use.substr(0, 4) == "left") {
      nows -= stoi(use.substr(5));
      if (nows < 1 || nows > n) {
        cout << -1 << "\n";
        nows += stoi(use.substr(5));
        continue;
      }
      cout << b[nows] << "\n";
    } else if (use.substr(0, 4) == "righ") {
      nows += stoi(use.substr(5));
      if (nows < 1 || nows > n) {
        cout << -1 << "\n";
        nows -= stoi(use.substr(5));
        continue;
      }
      cout << b[nows] << "\n";
    } else if (use.substr(0, 3) == "get" &&
               use.substr(use.length() - 1, 1) == "s") {
      coin += stoi(use.substr(4,use.length()-10));
      cout << coin << "\n";
      maxcoin = max(maxcoin, coin);
    } else if (use.substr(0, 3) == "get" &&
               use.substr(use.length() - 1, 1) == "P") {
      hp += stoi(use.substr(4,use.length()-7));
      cout << hp << "\n";
      maxhp = max(maxhp, hp);
    } else if (use.substr(0, 4) == "lose" &&
               use.substr(use.length() - 1, 1) == "s") {
      coin -= stoi(use.substr(5,use.length()-11));
      cout << coin << "\n";
      mincoin = min(mincoin, coin);
    } else if (use.substr(0, 4) == "lose" &&
               use.substr(use.length() - 1, 1) == "P") {
      hp -= stoi(use.substr(5,use.length()-8));
      minhp = min(minhp, hp);
      if (hp <= 0) {
        cout << "遊戲結束\n";
        cout << "金幣值為" << coin << "\n";
        cout << "金幣值最多時為" << maxcoin << "\n";
        cout << "金幣值最少時為" << mincoin << "\n";
        cout << "血量值為" << 0 << "\n";
        cout << "血量值最多時為" << maxhp << "\n";
        cout << "血量值最少時為" << 0 << "\n";
        cout << "目前所在的格子編號為" << nows << "\n";
        cout << "目前所在的格子名稱為" << b[nows] << "\n";
        break;
      }
      cout << hp << "\n";
      
    } else if (use == "BOOM") {
      hp -= 150;
      minhp = min(minhp, hp);
      cout << hp << ' ' << nows << ' ' << b[nows] << "\n";
      if (hp <= 0) {
        cout << "遊戲結束\n";
        cout << "金幣值為" << coin << "\n";
        cout << "金幣值最多時為" << maxcoin << "\n";
        cout << "金幣值最少時為" << mincoin << "\n";
        cout << "血量值為" << 0 << "\n";
        cout << "血量值最多時為" << maxhp << "\n";
        cout << "血量值最少時為" << 0 << "\n";
        cout << "目前所在的格子編號為" << nows << "\n";
        cout << "目前所在的格子名稱為" << b[nows] << "\n";
        return 0;
      }
      
      nows = s;
    } else {
      cout << "遊戲結束\n";
      cout << "金幣值為" << coin << "\n";
      cout << "金幣值最多時為" << maxcoin << "\n";
      cout << "金幣值最少時為" << mincoin << "\n";
      cout << "血量值為" << hp << "\n";
      cout << "血量值最多時為" << maxhp << "\n";
      cout << "血量值最少時為" << minhp << "\n";
      cout << "目前所在的格子編號為" << nows << "\n";
      cout << "目前所在的格子名稱為" << b[nows] << "\n";
      break;
    }
  }
}

 
ZeroJudge Forum