当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
动态规划总括
How to dynamically add script dependent scripts
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
某母婴小程序加密参数解密
渗透测试(PenTest)基础指南
FPGA学习笔记——知识点总结
使用Patroni回调脚本绑定VIP的坑
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
3000字,一文带你搞懂机器学习!
DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
Resolved error: npm WARN config global `--global`, `--local` are deprecated
应届生软件测试薪资大概多少?
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
Shocked, 99.9% of the students didn't really understand the immutability of strings
Use Patroni callback script to bind VIP pit
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.5 数组和指针的其他区别
结构体指针知识要点总结
el-Select 选择器 底部固定
想低成本保障软件安全?5大安全任务值得考虑