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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章