当前位置:网站首页>猴子吃桃子问题(循环、递归)
猴子吃桃子问题(循环、递归)
2022-04-22 07:44:00 【真题OK撒】
7-17 h0158. 猴子吃桃子问题 (10 分)
有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个!以后每天猴子都吃其中的一半,然后再多吃一个。当到第n天时,想再吃时(还没吃),发现只有1个桃子了。问题:最初共多少个桃子?
输入格式:
在一行中给出1个不超过30的正整数n。
输出格式:
在一行中输出最初的桃子个数。
输入样例:
10
输出样例:
桃子=1534
import scala.io.StdIn;
object Main{
def main(args: Array[String]) : Unit = {
var n = StdIn.readInt();
var sum = 1;
for ( i <- 1 to n - 1) {
sum = (sum + 1) * 2;
}
println("桃子="+sum);
}
}
import scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
var n = StdIn.readInt();
val num = f(1,1,n);
print("桃子="+num);
}
def f(a : Int ,d : Int ,n : Int): Int = {
var ans = (a + 1) * 2;
var dcnt = d + 1;
if(dcnt == n) return ans;
else f(ans ,dcnt ,n);
}
}
版权声明
本文为[真题OK撒]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51916951/article/details/123797567
边栏推荐
- 129. 求根节点到叶节点数字之和(Medium)
- Nacos Foundation (3): open API configuration management test and close Nacos service
- 静态库修改初探
- cesium 采集地形高度,采集模型高度 (异步方法,适合数据较多的时候)
- 只有服务器,没有域名,怎么部署网站?
- MATLAB安装产品选择,如何选择需要安装的产品
- The industrialization of SCRM has accelerated, and the manufacturing industry has begun to play with private traffic
- Tita performance treasure: eight mistakes made by managers in performance appraisal
- qt designer 跳转,布局,样式
- 反转一个链表<难度系数>
猜你喜欢

磁盘管理—RAID创建

记录node中使用sequelize,自动生成表模型,链接操作数据库(以postgreSQL为例)

Make the airtest ide into a script and make the script run

每日一题冲刺大厂第十五天 终于结束的起点

【论文阅读】【3d目标检测】Voxel Set Transformer: A Set-to-Set Approach to 3D Object Detection from Point Clouds

js学习笔记

【无极低码】低代码平台开发日记,低代码平台之sql编程

Fresco简单的使用—SimpleDraweeView

sonic云真机入门教程

Detailed analysis of viewpager usage
随机推荐
指针和数组(操作详解)
The industrialization of SCRM has accelerated, and the manufacturing industry has begun to play with private traffic
Nacos Foundation (1): what is configuration center & introduction to Nacos
第2关:多态
秋招求职总结分享
226. 翻转二叉树(Easy)
客户端与服务器通信项目2
EFCore全局查询筛选器
Cloud computing learning 2 - keystone component operation and maintenance and testing
每日一题冲刺大厂第十五天提高组 模
111. 二叉树的最小深度
【论文阅读】【3d目标检测】Voxel Set Transformer: A Set-to-Set Approach to 3D Object Detection from Point Clouds
Tita performance treasure: eight mistakes made by managers in performance appraisal
Mapbox设置官方地图语言为中文
MySQL深入学习(三一):PowerDesigner的使用
MATLAB安装产品选择,如何选择需要安装的产品
ViewPager用法详细解析
Simple use of fresco - simpledraweeview
vscode的插件
Flutter GridView Demo