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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值