题解 | #手机键盘#

手机键盘

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;
}

全部评论

相关推荐

狄文君:多段项目经历 + 专业技能拉满,这简历很能打,坐等好消息!
点赞 评论 收藏
分享
想踩缝纫机的小师弟练...:不理解你们这些人,要放记录就把对方公司名字放出来啊。不然怎么网暴他们
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务