ZJANS

d086. 態度之重要的證明

Easy Last Update: 2026/01/22
字串

給一個單字 s
根據 a=1, b=2, c=3 … z=26 的規則
將 s 裏的每個文字依照規則轉換成數字再相加
如果 s 中間參雜 a~z 以外的字元,輸出 “Fail”

※ 輸入包含大小寫


解法一

✅ 完整代碼

評分結果(參考) : AC (1ms, 356KB)

#include<bits/stdc++.h>
#define int long long
using namespace std;

int getScore(string& s){
    int sum = 0;
    for(char c : s){
        char ch = toupper(c);
        if(!(ch>='A' && ch<='Z')) return -1;
        sum += ch-'A'+1;
    }

    return sum;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    while(true){
        cin >> s;
        if(s == "0") break;
        
        int score = getScore(s);
        if(score < 0) cout << "Fail\n";
        else cout << score << "\n";
    }

    return 0;
}