Luogu brush questions C++ language | P1765 mobile phone

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

The keyboard of a general mobile phone is like this:

To press the English letters, it is necessary to press the number keys more. For example, if you want to press x, you have to press 9 twice. The first press will produce w, and the second press will change w to x. Pressing the 0 key will produce a space.

Your task is to read several sentences that only contain English lowercase letters and spaces, and find out how many keystrokes are required to type this sentence on the mobile phone.

【enter】

A line of sentences, containing only English lowercase letters and spaces, and no more than 200 characters.

【Output】

One integer per line, representing the total number of key presses.

【Input example】

i have a dream

【Example of output】

23

【Code Explanation】

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

int main()
{
    string s;
    int ans=0, m[300], mark=1;
    getline(cin, s);
    //对m进行赋值
    for (int i='a'; i<='r'; i++) {
        m[i] = mark;
        mark++;
        if (mark == 4) mark = 1;
    }
    m['s'] = 4;
    m['z'] = 4;
    m[' '] = 1;
    mark = 1;
    for (int i='t'; i<='y'; i++) {
        m[i] = mark;
        mark++;
        if (mark == 4) mark = 1;
    }
    //遍历字符串,将每个字符串对应的数值相加
    for (int i=0; i<s.length(); i++) {
        ans += m[s[i]];
    }
    cout << ans;
    return 0;
}

【operation result】

i have a dream
23

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132713979