当前位置:网站首页>LCP 17. Quick Calculation Robot
LCP 17. Quick Calculation Robot
2022-08-04 05:35:00 【先生高】
LCP 17. Quick Calculation Robot
Xiaokou found a quick calculation robot at the autumn market.The store owner says two numbers (denoted as x and y) to the robot, please give the calculation instruction:
"A" operation: make x = 2 * x + y;"B" operation: make y = 2 * y + x.
In this game, the numbers spoken by the store owner are x = 1 and y = 0, and the calculation instructions spoken by Xiaokou are recorded as a string s consisting of capital letters A and B only.order indicates the calculation order, please return what the final sum of x and y is.
Example 1:
input: s = "AB"output: 4explain:After one A operation, x = 2, y = 0.After another B operation, x = 2, y = 2.The final sum of x and y is 4.
This problem is actually very simple. The code to solve the problem is as follows:
int fA(int x,int y){return 2*x+y;}int fB(int x,int y){return 2*y+x;}int calculate(char* s){int i;int x=1,y=0;for(i=0;s[i]!='\0';i++){if(s[i]='A')span>{x=fA(x,y);}else{y=fB(x,y);}}return x+y;}
边栏推荐
猜你喜欢
随机推荐
CentOS7 - yum install mysql
MySQL数据库(基础)
The difference between px, em, and rem
Get the selected content of the radio box
Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions
8款最佳实践,保护你的 IaC 安全!
canal实现mysql数据同步
[Cocos] cc.sys.browserType可能的属性
嵌入式系统驱动初级【4】——字符设备驱动基础下_并发控制
Gartner 权威预测未来4年网络安全的8大发展趋势
Delphi-C端有趣的菜单操作界面设计
注意!软件供应链安全挑战持续升级
idea设置识别.sql文件类型以及其他文件类型
The 2022 PMP exam has been delayed, should we be happy or worried?
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
梳理CamStyle、PTGAN、SPGAN、StarGAN
Sublime Text 3 2021.8.3 个人配置
力扣:63. 不同路径 II
Uni-app 小程序 App 的广告变现之路:全屏视频广告
【一步到位】Jenkins的安装、部署、启动(完整教程)