每日一九度之 题目1079:手机键盘
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:2543
解决:1401
- 题目描述:
-
按照手机键盘输入字母的方式,计算所花费的时间如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。现在给出一串字符,需要计算出它所需要花费的时间。
- 输入:
-
一个长度不大于100的字符串,其中只有手机按键上有的小写字母
- 输出:
-
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
- 样例输入:
-
bob www
- 样例输出:
-
7 7
模拟题,把手机键盘一打开,然后按着题目意思直接模拟就好。
//Asimple #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <cctype> #include <cstdlib> #include <stack> #include <cmath> #include <set> #include <map> #include <string> #include <queue> #include <limits.h> #define INF 0x7fffffff using namespace std; const int maxn = 105; typedef long long ll; char str[maxn]; int num[] = {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}; int cnt[] = {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}; bool isOne(char a, char b) { int t1 = num[a-'a']; int t2 = num[b-'a']; if(t1 == t2) { return true; } return false; } int main(){ while(scanf("%s",str) != EOF) { int len = strlen(str); int wait = cnt[(str[0] - 'a')]; for(int i = 1; i < len; i++) { if(isOne(str[i-1], str[i])) { wait = wait + 2; } wait = wait + cnt[(str[i] - 'a')]; } printf("%d\n",wait); } return 0; }
低调做人,高调做事。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理