【基础训练-查表+map】SMS Typing

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语言程序如下:

[cpp]  view plain  copy
  1. /* UVA11530 SMS Typing */  
  2.   
  3. #include <stdio.h>  
  4. #include <ctype.h>  
  5.   
  6. #define N 100  
  7. char s[N + 1];  
  8.   
  9. 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};  
  10.   
  11. int main(void)  
  12. {  
  13.     int t, caseno=0, sum;  
  14.   
  15.     scanf("%d", &t);  
  16.     getchar();  
  17.     while(t--) {  
  18.         gets(s);  
  19.   
  20.         sum = 0;  
  21.         for(int i=0; s[i]; i++)  
  22.             if(islower(s[i]))  
  23.                 sum += m[s[i] - 'a'];  
  24.             else if(s[i] == ' ')  
  25.                 sum += 1;  
  26.   
  27.         printf("Case #%d: %d\n", ++caseno, sum);  
  28.     }  
  29.   
  30.     return 0;  
  31. }  

 

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

[cpp]  view plain  copy
  1. /* UVA11530 SMS Typing */  
  2.   
  3. #include <iostream>  
  4. #include <map>  
  5. #include <stdio.h>  
  6.   
  7. using namespace std;  
  8.   
  9. const int N = 100;  
  10. char s[N + 1];  
  11.   
  12. map <charint> m;  
  13.   
  14. int main()  
  15. {  
  16.     int t, caseno=0, sum;  
  17.   
  18.     m['a'] = m['d'] = m['g'] = m['j'] = m['m'] = m['p'] = m['t'] = m['w'] = m[' '] = 1;  
  19.     m['b'] = m['e'] = m['h'] = m['k'] = m['n'] = m['q'] = m['u'] = m['x'] = 2;  
  20.     m['c'] = m['f'] = m['i'] = m['l'] = m['o'] = m['r'] = m['v'] = m['y'] = 3;  
  21.     m['s'] = m['z'] = 4;  
  22.   
  23.     scanf("%d", &t);  
  24.     getchar();  
  25.     while(t--) {  
  26.         gets(s);  
  27.   
  28.         sum = 0;  
  29.         for(int i=0; s[i]; i++)  
  30.             sum += m[s[i]];  
  31.   
  32.         printf("Case #%d: %d\n", ++caseno, sum);  
  33.     }  
  34.   
  35.     return 0;  

  1. }  




学到的点:
①借数组用查表法是应该熟稔于心的。
②map的定义和使用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值