rqn 224 手机(常量数组)

来源:互联网 发布:2015年淘宝网店数量 编辑:程序博客网 时间:2024/05/21 07:00

题目连接:http://www.rqnoj.cn/Problem_224.html

解题思路:将对应按键存成常量数组。

#include <stdio.h>#include <string.h>const int sign[] = {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};const int N = 205;int main() {    char str[N];    while (gets(str)) {int len = strlen(str), cnt = 0;for (int i = 0; i < len; i++)    if (str[i] == ' ')cnt++;    elsecnt += sign[str[i] - 'a'];printf("%d\n", cnt);    }    return 0;}