uva11530 SMS Typing

uva11530

uDebug11530

题意是说,老式手机因为键盘布局原因,有些26个字母,有些字母只需要按一下键盘就可以打出来,有些字母却需要连续按2下、3下甚至4下键盘才能打出来。现给定T句话,每句话的长度在1-100个字符之间,且字符仅有字母和空格组成,要求输出每句话的键盘敲击总次数。

思路也很简单,将26个字母的键盘按键次数记录到一个数组,这样只需要对相应的字母进行查表,累加键盘敲击次数即可。

python版本AC代码

freq = [1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4]

testcase = int(input())
for i in range(testcase+1):
	if i == 0:
		continue
	LineList = input()
	strlen = len(LineList)
	cnt = 0
	for j in range(strlen):
		if LineList[j] == ' ':
			cnt += 1
		else:
			dif = ord(LineList[j]) - ord('a')
			cnt += freq[dif]
	print('Case #{}: {}'.format(i,cnt))

C++版本AC代码

#include <iostream>
#include<cstdio>
using namespace std;

//#define ZANGFONG
int fre[] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};


int main()
{
    #ifdef ZANGFONG
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // ZANGFONG
    int testcase,i,j,len,cnt;
    string line;
    scanf("%d",&testcase);
    getchar();
    for(i = 1; i <= testcase; i++)
    {
        getline(cin,line);
        len = line.length();
        cnt = 0;
        for(j = 0; j < len; j++)
        {
            if(line[j] == ' ') cnt++;
            else cnt += fre[line[j]-'a'];
        }
        printf("Case #%d: %d\n",i,cnt);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zangfong/article/details/82929692