一个长度不大于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;
}
}
#把键盘贴出来,一个记录结果,一个记录上一个键盘位的下标(初始为-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;
} #include <stdio.h>
#include <stdbool.h>
#include <string.h>
int getSet(char c)
{ // 这个函数用于判断c属于哪个集合
if(c == 'a' || c == 'b' || c == 'c')
return 1;
if(c == 'd' || c == 'e' || c == 'f')
return 2;
if(c == 'g' || c == 'h' || c == 'i')
return 3;
if(c == 'j' || c == 'k' || c == 'l')
return 4;
if(c == 'm' || c == 'n' || c == 'o')
return 5;
if(c == 'p' || c == 'q' || c == 'r' || c == 's')
return 6;
if(c == 't' || c == 'u' || c == 'v')
return 7;
if(c == 'w' || c == 'x' || c == 'y' || c == 'z')
return 8;
}
bool onSameKey(char c1, char c2)
{ // 这个函数用于判断两个字符是不是这同一个按键上
int s1 = getSet(c1);
int s2 = getSet(c2);
if(s1 == s2)
return true;
else
return false;
}
int get_char_time(char c)
{ // 这个函数用于获得按下字符c所需的时间
//abc def ghi jkl mno PQRS TUV WXYZ
// a, d, g,j,m,p,t,
if(c == 'a' || c == 'd' || c== 'g' || c == 'j'
|| c == 'm' || c == 'p' || c == 't' || c == 'w')
return 1;
if(c == 'b' || c == 'e' || c== 'h' || c == 'k'
|| c == 'n' || c == 'q' || c == 'u' || c == 'x')
return 2;
if(c == 'c' || c == 'f' || c== 'i' || c == 'l'
|| c == 'o' || c == 'r' || c == 'v' || c == 'y')
return 3;
if(c == 's' || c == 'z')
return 4;
}
int get_time(char * s, int n)
{ // 传入n时,n只有1,和2两个数值,
// n == 1 代表只传入一个字符,没有下个字符,用于计算字符串的最后一个字符所需时间
// n == 2 代表传入两个字符,有下一个字符
//此函数用于判断按下字符*s所需的时间,以及是否需要等待时间
int time = get_char_time(*s);
if( (n == 1) || (onSameKey(s[0], s[1]) == false) ) // 当只有一个字符时,
return time; //或者后一个字符与当前字符不是在同一个按键上,直接返回时间
else // 代表下一个按键与当前按键在同一个按键上
return time + 2;
}
int main(void)
{
char s[120];
while(scanf("%s", s) != EOF)
{
int time = 0;
int len = strlen(s);
for(int i = 0; i < len; ++i)
{
if(i != len - 1)
time += get_time( &(s[i]), 2);
else
time += get_time( &(s[i]), 1);
}
printf("%d\n", time);
}
}
#include<stdio.h>
#include<vector>
#include<map>
#include<iostream>
#include<string>
using namespace std;
string arr[8]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
map<char,vector<int> > book;
int cnt=1;
void dfs(string,int);
int main(){
int i,j;
for(i=0;i<8;i++){
string x=arr[i];
for(j=0;j<x.length();j++){
vector<int> flag(2);
flag[0]=i,flag[1]=j+1;
book[x[j]]=flag;
}
}
string x;
//freopen("input.txt","r",stdin);
while(cin>>x){
cnt=book[x[0]][1];
dfs(x,1);
printf("%d\n",cnt);
}
}
void dfs(string x,int index){
if(index==x.length()) return;
vector<int> a=book[x[index-1]],b=book[x[index]];
if(a[0]==b[0]) cnt+=2;
cnt+=b[1];
dfs(x,index+1);
} #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>
int time[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 whichButton[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(){
int last_buttom = 0;
char c;
int count = 0;
while((c = getchar())!='\n'){
//每个字母考虑三步==》1.需要按几下 2.该字母所在位置是否和上个按键相同 3.记录刚刚按过的键位
count+=time[c - 'a'];
if(whichButton[c - 'a'] == last_buttom) count+=2;
last_buttom = whichButton[c - 'a'];
}
printf("%d\n",count);
return 0;
}