「Daily OI Round 4」Analysis 题解

· · 题解

一道字符串入门题。
记字符串 s 中每个字符的 ASCII 码为 x。不知道什么是 ASCII 的这边走。
当字符为数字时,它的 ASCII 码在 48 到 57 之间;当字符为大写字母时,它的 ASCII 码在 65 到 90 之间;当字符为小写字母时,它的 ASCII 码在 97 到 122 之间。
可以发现,当字符是小写字母时,最优方法是将其转换为大写字母 Z,需要 x-90 时间。当字符是数字时,最优方法是将其转换为大写字母 A,需要 65-x 时间。当字符是大写字母时,无需时间。
代码如下:

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