UVA11530 SMS Typing【模拟+map+查表】

Cell phones have become an essential part of modern life. In addition tomaking voice calls, cell phones can be used to send text messages, whichare known as SMS for short. Unlike computer keyboards, most cell phoneshave limited number of keys. To accommodate all alphabets, letters arecompacted into single key. Therefore, to type certain characters, a keymust be repeatedly pressed until that character is shown on the displaypanel.

  In this problem we are interested in finding out the number of times keys on a cell phone must bepressed to type a particular message.

  In this problem we will assume that the key pad of our cell phone is arranged as follows.


  In the above grid each cell represents one key. Here means a space. In order to type the letter‘a’, we must press that key once, however to type ‘b’ the same key must be repeatedly pressed twiceand for ‘c’ three times. In the same manner, one key press for ‘d’, two for ‘e’ and three for ‘f’. This isalso applicable for the remaining keys and letters. Note that it takes a single press to type a space.

Input

The first line of input will be a positive integer T where T denotes the number of test cases. T lineswill then follow each containing only spaces and lower case letters. Each line will contain at least 1 andat most 100 characters.

Output

For every case of input there will be one line of output. It will first contain the case number followedby the number of key presses required to type the message of that case. Look at the sample output forexact formatting.

Sample Input

2

welcome to ulab

good luck and have fun

Sample Output

Case #1: 29

Case #2: 41


问题链接UVA11530 SMS Typing

问题简述:(略)

问题分析:(略)

程序说明

  这个问题直接模拟实现。

  功能手机键盘的各个字母按键次数是固定,使用map来存储各个字符的按键次数是一种比较简单的做法。

  然而用数组来存储各个字符的按键次数,效率更高。

题记:离散表通常用map来实现,连续的表则用数组来实现,效率很重要。

参考链接:(略)


AC的C语言程序如下:

/* UVA11530 SMS Typing */

#include <stdio.h>
#include <ctype.h>

#define N 100
char s[N + 1];

int m[] = {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(void)
{
    int t, caseno=0, sum;

    scanf("%d", &t);
    getchar();
    while(t--) {
        gets(s);

        sum = 0;
        for(int i=0; s[i]; i++)
            if(islower(s[i]))
                sum += m[s[i] - 'a'];
            else if(s[i] == ' ')
                sum += 1;

        printf("Case #%d: %d\n", ++caseno, sum);
    }

    return 0;
}

 

AC的C++语言程序如下:

/* UVA11530 SMS Typing */

#include <iostream>
#include <map>
#include <stdio.h>

using namespace std;

const int N = 100;
char s[N + 1];

map <char, int> m;

int main()
{
    int t, caseno=0, sum;

    m['a'] = m['d'] = m['g'] = m['j'] = m['m'] = m['p'] = m['t'] = m['w'] = m[' '] = 1;
    m['b'] = m['e'] = m['h'] = m['k'] = m['n'] = m['q'] = m['u'] = m['x'] = 2;
    m['c'] = m['f'] = m['i'] = m['l'] = m['o'] = m['r'] = m['v'] = m['y'] = 3;
    m['s'] = m['z'] = 4;

    scanf("%d", &t);
    getchar();
    while(t--) {
        gets(s);

        sum = 0;
        for(int i=0; s[i]; i++)
            sum += m[s[i]];

        printf("Case #%d: %d\n", ++caseno, sum);
    }

    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值