一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
bob www
7 7
#include<stdio.h> #include<string.h> int fun(char a){ switch(a){ case 'a': return 1; case 'b': return 2; case 'c': return 3; case 'd': return 1; case 'e': return 2; case 'f': return 3; case 'g': return 1; case 'h': return 2; case 'i': return 3; case 'j': return 1; case 'k': return 2; case 'l': return 3; case 'm': return 1; case 'n': return 2; case 'o': return 3; case 'p': return 1; case 'q': return 2; case 'r': return 3; case 's': return 4; case 't': return 1; case 'u': return 2; case 'v': return 3; case 'w': return 1; case 'x': return 2; case 'y': return 3; case 'z': return 4; } return 0; } int main(){ char str[100],i; while(scanf("%s",&str)!=EOF){ int length=strlen(str); int sum=0; for(i=0;i<length;i++){ sum=sum+fun(str[i]); if(i!=0&& str[i]-str[i-1]==fun(str[i])-fun(str[i-1])){ sum+=2; } } printf("%d\n",sum); } }
#include <stdio.h> #include <string.h> char arr[9][3]; int abc[26] = {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() { char str[101]; // 字符串 while (scanf("%s", str) != EOF) { int time = 0; // 时间 for (int i = 0; i < strlen(str); ++i) { // 遍历字符串 time += abc[str[i] - 'a']; // 先加上按到当前字母的次数(即时间) if (i != 0 && str[i] - str[i - 1] == abc[str[i] - 'a'] - abc[str[i - 1] - 'a']) { // 若当前字母与上一个字母在一个按键上,在原来的时间基础上+2 time += 2; } } printf("%d\n", time); } return 0; }
#include <stdio.h> #include <string.h> int main() { char str[100]; int keyTab[26] = {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}; //字符ascci值 int hash[26] = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,6,7,7,7,8,8,8,8}; while (scanf("%s", str) != EOF) { int time = 0; for (int i = 0; i < strlen(str); i++) { time += keyTab[str[i] - 'a']; if (hash[str[i]-'a']==hash[str[i+1]-'a']){ time+=2; } } printf("%d\n", time); } return 0; }
#include <stdio.h> #include <string.h> int main() { char s[101]; int time[] = {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 type[] = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7}; while (scanf("%s", s) != EOF) { int len = strlen(s); int sum = time[s[0] - 'a']; for (int i = 1; i < len; i ++) { if (type[s[i]-'a'] == type[s[i - 1]-'a']) { sum += 2; } sum += (time[s[i] - 'a']); } printf("%d\n", sum); } return 0; }
#include<stdio.h> int KeyValue(char k)//判断上次是否为同一按键 { static int two = 0,three = 0,four = 0,five = 0,six = 0,seven = 0,eight = 0,nine = 0; switch(k) { case 'a':;case 'b':;case 'c': { if(two != 0) { return 2; break; } two = 1;three = 0,four = 0,five = 0,six = 0,seven = 0,eight = 0;nine = 0; return 0; break; } case 'd':;case 'e':;case 'f': { if(three != 0) { return 2; break; } two = 0;three = 1,four = 0,five = 0,six = 0,seven = 0,eight = 0;nine = 0; return 0; break; } case 'g':;case 'h':;case 'i': { if(four != 0) { return 2; break; } two = 0;three = 0,four = 1,five = 0,six = 0,seven = 0,eight = 0;nine = 0; return 0; break; } case 'j':;case 'k':;case 'l': { if(five != 0) { return 2; break; } two = 0;three = 0,four = 0,five = 1,six = 0,seven = 0,eight = 0;nine = 0; return 0; break; } case 'm':;case 'n':;case 'o': { if(six != 0) { return 2; break; } two = 0;three = 0,four = 0,five = 0,six = 1,seven = 0,eight = 0;nine = 0; return 0; break; } case 'p':;case 'q':;case 'r':;case 's': { if(seven != 0) { return 2; break; } two = 0;three = 0,four = 0,five = 0,six = 0,seven = 1,eight = 0;nine = 0; return 0; break; } case 't':;case 'u':;case 'v': { if(eight != 0) { return 2; break; } two = 0;three = 0,four = 0,five = 0,six = 0,seven = 0,eight = 1;nine = 0; return 0; break; } case 'w':;case 'x':;case 'y':;case 'z': { if(nine != 0) { return 2; break; } two = 0;three = 0,four = 0,five = 0,six = 0,seven = 0,eight = 0;nine = 1; return 0; break; } default:two = 0;three = 0,four = 0,five = 0,six = 0,seven = 0,eight = 0;nine = 0; } return 0; } int Time(char k)//按键时间 { switch(k) { case 'a':;case 'd':;case 'g':;case 'j':;case 'm':;case 'p':;case 't':;case 'w': return 1 + KeyValue(k); break; case 'b':;case 'e':;case 'h':;case 'k':;case 'n':;case 'q':;case 'u':;case 'x': return 2 + KeyValue(k); break; case 'c':;case 'f':;case 'i':;case 'l':;case 'o':;case 'r':;case 'v':;case 'y': return 3 + KeyValue(k); break; case 's':;case 'z': return 4 + KeyValue(k); break; } return 0; } int main(void) { char key[100]; int s = 0,l; scanf("%s",key); l = strlen(key); for(int i = 0;i < l; i++) { s += Time(key[i]); } printf("%d\n",s); KeyValue(0); return 0; }