当前位置:网站首页>LCP 17. 速算机器人
LCP 17. 速算机器人
2022-08-04 05:18:00 【Mr Gao】
LCP 17. 速算机器人
小扣在秋日市集发现了一款速算机器人。店家对机器人说出两个数字(记作 x 和 y),请小扣说出计算指令:
"A" 运算:使 x = 2 * x + y;
"B" 运算:使 y = 2 * y + x。
在本次游戏中,店家说出的数字为 x = 1 和 y = 0,小扣说出的计算指令记作仅由大写字母 A、B 组成的字符串 s,字符串中字符的顺序表示计算顺序,请返回最终 x 与 y 的和为多少。
示例 1:
输入:s = "AB"
输出:4
解释:
经过一次 A 运算后,x = 2, y = 0。
再经过一次 B 运算,x = 2, y = 2。
最终 x 与 y 之和为 4。
这题其实就很简单了,解题代码如下:
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'){
x=fA(x,y);
}
else{
y=fB(x,y);
}
}
return x+y;
}
边栏推荐
- 入坑软件测试的经验与建议
- OpenGL绘制圆
- 力扣:62.不同路径
- 结构体函数练习
- [21 Days Learning Challenge] Image rotation problem (two-dimensional array)
- 编程大杂烩(四)
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
- 你以为border-radius只是圆角吗?【各种角度】
- Teenage Achievement Hackers Need These Skills
- 力扣题解8/3
猜你喜欢
随机推荐
static在不同位置定义变量居然还有不同的含义?
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.1 Arrays are not pointers
备份工具pg_dump的使用《postgres》
动态规划总括
编程大杂烩(三)
Dynamic programming of the division of numbers
DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
The Road to Ad Monetization for Uni-app Mini Program Apps: Full Screen Video Ads
What is the salary of a software testing student?
心余力绌:企业面临的软件供应链安全困境
System design. How to design a spike system (full version transfer)
触觉智能分享-SSD20X实现升级显示进度条
[SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
编程大杂烩(四)
Converts XML tags to TXT format (voc conversion for yolo convenient training)
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.4 使声明与定义相匹配
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
go module的介绍与应用
应届生软件测试薪资大概多少?