目录

每个字母代表一个数字,输出字符串代表的数字之和

目录

【题目描述】

If we define each of the upper case letters a number, that is, 1 for A, 2 for B, 3for C, … 25 for Y, 26 for Z, we

can calculate a mark for any word or sentence.

We believe that 100 points is the full mark, so for those word with a mark larger than 100, we should just ignore

that, with a label “INVALID” .Then let’s see interesting results:

What is the most important for your success?

HARDWORK? H+A+R+D+W+O+R+K=8+1+18+4+23+15+18+11=98

KNOWLEDGE? K+N+O+W+L+E+D+G+E=11+14+15+23+12+5+4+7+5=96

MONEY? M+O+N+E+Y=13+15+14+5+25=72

LUCK? L+U+C+K=12+21+3+11=47

ATTITUDE! A+T+T+I+T+U+D+E=1+20+20+9+20+21+4+5=100

So just be with good attitude and you will be on your way to success.

【输入格式】

For each case of the input, it contains a single line. For each line, some characters will appear with maybe some of them being upper case letters. All characters are printable. Each line will have at most 127 characters.

【输出格式】

For each case, you should only pay attention to upper case letters and calculate the total points. If it is over 100, “INVALID” should be returned. Or you should print the exact point of that.

For example:

Input

Result

DELICIOUS FOOD
YY, and YY again!
did i lose anything?

INVALID
100
0

Answer

#include <iostream>

using namespace std;

int main()
{


    //输入1至26个字母
    char a[9999];
    int i,j,cnt,x=0;
    for(i=1;i<=26;i++)
    {
        a[i] = 'A' + i - 1;
    }

    //输入字符
    char b[9999];
    while(cin.getline(b,9999)) //这个while是后来加上去的,没有while就不能多次输入
    {

    cnt = 0;

    //逐个计数
    for(i=0;b[i]!='\0';i++)
    {
        for(j=1;j<=26;j++)
        {
            if(b[i] == a[j])
            {
                x = j;  
                break;
            }
            else
            {
                x = 0; //这里本来是没有这个else和x的,但是如果不加这个else,会出bug
            }
        }
        cnt = cnt + x;
    }

        if(cnt > 100)
        {
            cout << "INVALID" <<endl;
        }
        else
        {
            cout << cnt << endl;
        }
    }
    return 0;
}

这个吊题目不难,但是需要细心,写出算法用了十分钟,改bug用三十分钟。还问了恺毅同学,他是真的强。