1 public class fs_5 {
2 public static void main(String[] args) {
3 int[] eachtime = {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};//每一个字母对应需要按下的次数
4 int[] group = {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, 9, 9};//一共八组,用来判断是否在一个按钮上,故意设置一个9初始化pre为{
5
6 Scanner scanner = new Scanner(System.in);
7 while (scanner.hasNext()) {
8 int sum = 0;
9 String string = scanner.next();
10 char pre = '{';//是z后面一个字母
11 char[] arr = string.toCharArray();//char 数组
12 for (int i = 0; i < arr.length; i++) {
13 int cha = arr[i] - 'a';
14 int cha_1 = pre - 'a';
15 if (group[arr[i] - 'a'] == group[pre - 'a']) {
16 sum += 2;
17 }
18 sum += eachtime[arr[i] - 'a'];
19 pre = arr[i];
20
21
22 }
23 System.out.println(sum);
24
25
26 }
27 }
1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6 int key[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};
7 string str;
8 while(cin>>str)
9 {
10 int count = key[str[0]-'a'];
11 for(int i=1;i<str.size();++i)
12 {
13 count += key[str[i]-'a'];
14 if(key[str[i]-'a']-key[str[i-1]-'a']==str[i]-str[i-1])//判断是否在同一个按键上
15 count+=2;
16 }
17 cout<<count<<endl;
18 }
19 }