题解:P10566 「Daily OI Round 4」Analysis

· · 题解

P10566 「Daily OI Round 4」Analysis 题解

前置芝士:

字符串操作,字符的 ASCII 码。

思路

由于输入只包含大写字母,小写字母和数字,又因为数字的 ASCII 码 小于 大写字母的 ASCII 码 小于 小写字母的 ASCII 码。

为了使花费最少,我们使用就近原则。

最后累加即可。

Code

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    long long ans=0;
    for(int i=0; i<s.size(); i++){
        if(s[i]>='a'&&s[i]<='z')
            ans=ans+s[i]-'Z';
        else if(s[i]>='0'&&s[i]<='9')
            ans=ans+'A'-s[i];
    }
    cout<<ans<<endl;
    return 0;
}