一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
bob www
7 7
#include<iostream> #include<cstdio> using namespace std; 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 }; int countTheTime(string str){ int sum=0; sum+=keytab[(str[0]-'a')]; for(int i=1;i<str.size();i++){ sum+=keytab[(str[i]-'a')]; if((str[i]-str[i-1])==(keytab[(str[i]-'a')]-keytab[(str[i-1]-'a')])){ sum+=2; } } return sum; } int main(){ string str; while(cin>>str){ cout<<countTheTime(str)<<endl; } }
#include<iostream> #include<string> using namespace std; int main() { 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 }; string input; while (cin >> input) { int sum = key[input[0]-'a']; for (int i = 1; i <input.size();i++){ sum += key[input[i] - 'a']; if (input[i] - input[i-1]== key[input[i] - 'a'] - key[input[i-1] - 'a']) sum += 2; } cout << sum << endl; } }
#把键盘贴出来,一个记录结果,一个记录上一个键盘位的下标(初始为-1) #如果在键盘里找到了该字符,则结果加上该字符在该键的下标+1, #如果该字符所在键下标和上一个相等,结果+2,记录当前键下标 while True: try: keyboard = ['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz'] string = input() result,lastIndex = 0,-1 for i in range(len(string)): for j in range(len(keyboard)): if keyboard[j].find(string[i]) != -1: result += keyboard[j].find(string[i]) + 1 if lastIndex == j: #如果和上一个字符在同一个键盘位+2 result += 2 lastIndex = j break print(result) except Exception: break
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String orig = sc.next(); int total = 0; for (int i = 0; i < orig.length(); i++) { char current = orig.charAt(i); int button = toNum(current) / 10; int times = toNum(current) % 10; total += times; if (i < orig.length() - 1 && toNum(orig.charAt(i + 1)) / 10 == button) total += 2; } System.out.println(total); } sc.close(); } public static int toNum(char c) { switch(c) { case 'a': return 21; case 'b': return 22; case 'c': return 23; case 'd': return 31; case 'e': return 32; case 'f': return 33; case 'g': return 41; case 'h': return 42; case 'i': return 43; case 'j': return 51; case 'k': return 52; case 'l': return 53; case 'm': return 61; case 'n': return 62; case 'o': return 63; case 'p': return 71; case 'q': return 72; case 'r': return 73; case 's': return 74; case 't': return 81; case 'u': return 82; case 'v': return 83; case 'w': return 91; case 'x': return 92; case 'y': return 93; case 'z': return 94; } return 250+38+2; // 纯搞笑 } }
//此方法思路为,创建String数组把按键枚举出来,利用String.indexOf() //构造方法计算时间和判断是否在同一按键上 import java.util.Scanner; public class Main{ //枚举按键,装入数组 public static String[] str = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ char[] ch = sc.nextLine().toCharArray(); //接收键盘输入 int times = cal(ch[0]); //用tims接收计算第一个字符所需时间段 for(int i=1;i<ch.length;i++){ //从第二个字符开始计算时间段 if(judge(ch[i-1],ch[i]) > 0){ //判断连续两个字符是否在同一按键上 times+=2; //同一按键则时间段加2 } times =cal(ch[i])+times; //计算字符所需时间段 } System.out.println(times); //打印最终结果 } } //创建cal()方法,计算按char c按键所需时间,返回index+1为按键所需时间段 public static int cal(char c){ int index = -1; for(int i=0;i<str.length;i++){ index = str[i].indexOf(c); if(index != -1){ return index+1; } } return 0; } //判断字符char a和字符char b是否在同一按键上,是则返回1,否则返回-1 public static int judge(char a,char b){ int i=0,j=0; //用i记录a字符的按键位置,j记录b字符的按键位置 for (; i < str.length; i++) { if(str[i].indexOf(a) != -1){ break; } } for (; j < str.length; j++) { if(str[j].indexOf(b) != -1){ break; } } if(i==j){ //判断a字符和b字符是否在同一按键上 return 1; } return -1; } }
#include<iostream> #include<string> #include<math.h> using namespace std; //用数组来记录字母所处的按键,空间换时间 int keyboard[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9}; bool isOnSampleButton(int i, string str) { if(keyboard[str[i] - 'a'] == keyboard[str[i+1] - 'a']) return true; else return false; } int main() { string str; int count = 0; while(cin >> str) { count = 0; for(int i = 0; i < str.length(); i++) { char c = str[i]; if(c == 'a' || c == 'd' || c == 'g' || c == 'j' || c == 'm' || c == 'p' || c == 't' || c == 'w') count++; else if(c == 'b' || c == 'e' || c == 'h' || c == 'k' || c == 'n' || c == 'q' || c == 'u' || c == 'x') count+=2; else if(c == 'c' || c == 'f' || c == 'i' || c == 'l' || c == 'o' || c == 'r' || c == 'v' || c == 'y') count+=3; else if(c == 's' || c == 'z') count+=4; if((i < str.length() - 1) && isOnSampleButton(i, str))//此处判断是否在同一按键上,与前几个if不是并列关系 count+=2; } cout << count << endl; } return 0; }
#include<iostream> #include<string.h> using namespace std; int main(){ string str; int pos[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}; int pos1[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}; while(cin>>str){ int len=str.size(); int sum=pos1[str[0]-'a']; for(int i=1;i<len;i++){ if(pos[str[i]-'a']==pos[str[i-1]-'a']){ sum=sum+pos1[str[i]-'a']+2; } else{ sum=sum+pos1[str[i]-'a']; } } cout<<sum<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int[] a= new int[]{ 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[] keys=new int[]{ 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 (in.hasNext()) { String str=in.nextLine(); int times=0; char pre; times+=a[str.charAt(0)-'a']; pre=str.charAt(0); for (int i=1;i<str.length();i++){ if (keys[str.charAt(i)-'a']==keys[pre-'a']) times+=2; times+=a[str.charAt(i)-'a']; pre=str.charAt(i); } System.out.println(times); } in.close(); } }
#include <iostream> using namespace std; int main(){ string a; while(cin>> a) { int time=0; int lastword=26;//上个按键记录 int word=0; int ay[27]={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}; //字母对应按键,第27位的9用于表示第一次“上个按键”没有记录 for(int i=0;i<a.size();i++) { word=(int)a.at(i)-97; if(ay[word]==ay[lastword])time+=2;//按相同键 for(int i=0;word-i>=0&&ay[word-i]==ay[word];i++)time++;//按键所需次数 lastword=word; } cout<< time << endl; } return 0; } //清晰简明
其实只需要一个数组就够用了啊。用key顺序记录26个字母按键次数, 然后判断两个字母是否在同一个按键上,如果在同一个按键上,那么下标差(字母间距) 就等于按键次数差。#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
intmain() {
intalpha[26] = {
1, 2, 3, // a, b, c
1, 2, 3, // d, e, f
1, 2, 3, // g, h, i
1, 2, 3, // j, k, l
1, 2, 3, // m, n, o
1, 2, 3, 4, // p, q, r, s
1, 2, 3, // t, u, v
1, 2, 3, 4 // w, x, y, z
}; //存储输入每个小写字母时需要的时间
intkeys[26] = {
1, 1, 1, // a, b, c
2, 2, 2, // d, e, f
3, 3, 3, // g, h, i
4, 4, 4, // j, k, l
5, 5, 5, // m, n, o
6, 6, 6, 6, // p, q, r, s
7, 7, 7, // t, u, v
8, 8, 8, 8// w, x, y, z
}; // 对字母进行分组,以确定在一个按键上面的字母
charpre; // 存储前一个字母
charstr[110];
inttimes; //所需要的输入次数
while(scanf("%s", str) != EOF) {
times = 0;
inti;
pre = '#';
for(i=0; i<strlen(str); i++) {
if(keys[pre-97] == keys[str[i]-97]) {
times += 2; //相同字母需要等待的时间
}
times += alpha[str[i] - 97];
pre = str[i];
}
printf("%d\n", times);
}
return0;
}
|
#inlcue<iostream> #include<map> #include<string> using namespace std; int main(){ map<char, int> myMap; int num = 1; char a = 'a'; while ('z'>a){ myMap[a++] = num*10+1; if(a!='z') myMap[a++] = num*10+2; if (a != 'z') myMap[a++] = num*10+3; if (a == 's'||a == 'z') myMap[a++] = num * 10 + 4; num++; } string temp; while (cin >> temp){ int sum = 0; int pre = -1; for (auto& e : temp){ int key = myMap[e] / 10; if (key!= pre)//前后在不同键位,需等待一次 sum += 0; else//同一键位,需等待多一次 sum += 2; sum += myMap[e] % 10; pre = key; } cout << sum << endl; } }
//话说辅助数组真的省很多事儿 #include<iostream> #include<cstring> using namespace std; int helper1[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 helper2[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 }; int main() { char ch[101]; while (cin >> ch) { int total = helper1[ch[0] - 'a'], last = helper2[ch[0] - 'a'];//总时间 for (int i = 1; i<strlen(ch); i++) { int temp = helper2[ch[i] - 'a']; total += helper1[ch[i] - 'a']; if (temp == last) total += 2; last = temp; } cout << total << endl; } }
#include<iostream> #include<cstdio> using namespace std; //数据预处理 const 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}; int main(){ string str; while(cin>>str){ int time = 0; for(int i=0; i<str.length(); i++){ time += keytab[str[i]-'a']; if(i!=0&&(str[i]-str[i-1]==keytab[str[i]-'a']-keytab[str[i-1]-'a'])){ time += 2; } } cout<<time<<endl; } return 0; }