当前位置:网站首页>Monkey eating peach problem (loop, recursion)
Monkey eating peach problem (loop, recursion)
2022-04-22 08:35:00 【True question OK】
7-17 h0158. The problem of monkeys eating peaches (10 branch )
There is a pile of peaches , The monkey ate half of it on the first day , And one more ! Later, every day monkeys eat half of them , And then one more . When it comes to the third n days , When you want to eat again ( Haven't eaten yet ), Only found 1 A peach . problem : How many peaches are there in the first place ?
Input format :
Give... In one line 1 No more than one. 30 The positive integer n.
Output format :
Output the initial number of peaches in one line .
sample input :
10
sample output :
Peach =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(" Peach ="+sum);
}
}
import scala.io.StdIn
object Main {
def main(args: Array[String]): Unit = {
var n = StdIn.readInt();
val num = f(1,1,n);
print(" Peach ="+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);
}
}
版权声明
本文为[True question OK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220743122782.html
边栏推荐
- 华为机试题——HJ53 杨辉三角的变形
- Experiment 2: mathematical basis in Data Science
- 空心字母金字塔
- Machine learning notes - determinant
- The industrialization of SCRM has accelerated, and the manufacturing industry has begun to play with private traffic
- 字符串替换相关题目(合并数组)
- 面试题 04.03. 特定深度节点链表(Medium)
- 129. 求根节点到叶节点数字之和(Medium)
- cesium坐标转换:不同坐标系之间的相互转换
- leaflet、cesium加载百度地图,加载自定义样式百度地图
猜你喜欢

kubernetes—实战入门

The collection palette in LabVIEW 2012 is imported into LabVIEW 2013

Redis non relational database - redis high availability, persistence and performance management

Spark SQL gets the element at an index of the array

机器学习笔记 - 主成分分析中的数学

The domestic cloud security market has exceeded 10 billion yuan. What is the future development trend?

Viewpager comprehensive summary

Cesium加载地形数据(cesiumlab制作地形数据),从源数据到地形服务

Flutter GridView Demo

Make the airtest ide into a script and make the script run
随机推荐
[Dahua cloud native] micro service chapter - service mode of five-star hotels
shell脚本中ps -ef查询进程PID一直返回异常
101. 对称二叉树(Easy)
微服务(分布式架构)
Flutter GridView Demo
指针和字符串
SmartTabLayout 简介
kubernetes学习笔记
129. 求根节点到叶节点数字之和(Medium)
微信公众号——网页授权
wepy学习记录
【论文阅读】【3d目标检测】pvgnet
Redis数据服务器/数据库/缓存(2022)
机器学习笔记 - 行列式
js再学习
算法--删除链表的倒数第 N 个结点(Kotlin)
cesium中实现楼层分解动画
CentOS 安裝 MySQL
OSPF class IV, class V and class VII LSA details
进程和线程