题解 | #手机键盘#

手机键盘

https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd

#include<iostream>
using namespace std;

string str[] = {"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

int find_key(char c)
{
	int ans = -1;
	for(int i = 1;i < 9;i++)
	{
		if(str[i].find(c,0) != -1) ans = i;
	}
	return ans;
}

int find_pos(char c)
{
	int ans = -1;
	for(int i = 1;i < 9;i++)
	{
		int pos = str[i].find(c,0);
		if(pos != -1 )ans = pos + 1;
	}
	return ans;
}

int main(void)
{
	string s;
	while(cin >> s)
	{
		int time = 0;
		int preKey = -1;
		for(int i = 0;i < s.size();i++)
		{
			int key = find_key(s[i]);
			int pos = find_pos(s[i]);
			if(preKey != key){
				preKey = key;
				time += pos;
			}else{
				time += 2;
				time += pos;
			}
		}
		cout << time << endl;
	}
	
	
	return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 18:54
点赞 评论 收藏
分享
想去夏威夷的小哥哥在度假:5和6才是重点
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务