题目:
alt text
代码:

include

include

using namespace std;

int main(){
string str;
int num = 0;
getline(cin,str);
for(int i = 0; i < str.size(); i++){
if(str[i] == ‘a’ ||str[i] == ‘d’ ||str[i] == ‘g’ ||str[i] == ‘j’ ||str[i] == ‘m’ ||str[i] == ‘p’||str[i] == ‘t’ ||str[i] == ‘w’){
num++;
}
else if(str[i] == ‘b’ ||str[i] == ‘e’ ||str[i] == ‘h’ ||str[i] == ‘k’ ||str[i] == ‘n’ ||str[i] == ‘q’ ||str[i] == ‘u’ ||str[i] == ‘x’){
num = num + 2;
}
else if(str[i] == ‘c’ ||str[i] == ‘f’ ||str[i] == ‘i’ ||str[i] == ‘l’ ||str[i] == ‘o’ ||str[i] == ‘r’ ||str[i] == ‘v’ ||str[i] == ‘y’){
num = num + 3;
}
else if(str[i] == ‘s’ || str[i] == ‘z’){
num = num + 4;
}
else num++;
}
cout<<num;
return 0;
}
更好的思路:

include”iostream”

include”cstdio”

include”cstring”//为用strchr查找函数

using namespace std;
char s1[]={“ adgjmptw”},s2[]={“behknqux”},s3[]={“cfilorvy”},s4[]={“sz”};//可以望文生义,分别开按1~4次键盘的常量数组
int main()
{
char c;
int s=0;
while((c=getchar())!=EOF) //!=EOF表示还没输入结束
{
//在s1~s4中依次找,找到就加上1~4
if(strchr(s1,c)) s++;
else if(strchr(s2,c)) s+=2;
else if(strchr(s3,c)) s+=3;
else if(strchr(s4,c)) s+=4; //注意这里不能写成else,因为还会有\n等字符
}
cout<<s<<endl;
r