a197簡單字串、字元與格式解析

TOI 2020/04 新手組:身分驗證機制

保留九位字串,檢查最大兩位平方和是否等於末三位。

前往題目練習 →回報教學錯誤九位固定:時間 O(1),空間 O(1)。

這題在學什麼

將代號以字串讀入,保留前導零;九個字元全部參與最大兩位的選取。

複製後排序,取末兩個數字,重複值只要來自不同位置就可同時使用。

末三位轉成整數後比對平方和,依結果輸出 Good Morning! 或 SPY!。

Step-by-step animation

身分驗證機制・全九位取前兩大

回報動畫錯誤
步驟 1 / 3

九位全部參與

代號是 980000145,末三位 145 也在選取範圍內。

9
8
0
0
0
0
1
4
5
末三位
145

Reference solution

回報程式錯誤

完整程式

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;
    string t = s;
    sort(t.begin(), t.end());
    int a = t[8] - '0', b = t[7] - '0';
    cout << (a * a + b * b == stoi(s.substr(6)) ? "Good Morning!" : "SPY!") << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多