当前位置:网站首页>#yyds干货盘点# 解决名企真题:扭蛋机
#yyds干货盘点# 解决名企真题:扭蛋机
2022-07-01 21:44:00 【51CTO】
1.简述:
描述
22娘和33娘接到了小电视君的扭蛋任务:
一共有两台扭蛋机,编号分别为扭蛋机2号和扭蛋机3号,22娘使用扭蛋机2号,33娘使用扭蛋机3号。
扭蛋机都不需要投币,但有一项特殊能力:
扭蛋机2号:如果塞x(x范围为>=0整数)个扭蛋进去,然后就可以扭到2x+1个
扭蛋机3号:如果塞x(x范围为>=0整数)个扭蛋进去,然后就可以扭到2x+2个
22娘和33娘手中没有扭蛋,需要你帮她们设计一个方案,两人“轮流扭”(谁先开始不限,扭到的蛋可以交给对方使用),用“最少”的次数,使她们能够最后恰好扭到N个交给小电视君。
输入描述:
输入一个正整数,表示小电视君需要的N个扭蛋。
输出描述:
输出一个字符串,每个字符表示扭蛋机,字符只能包含"2"和"3"。
示例1
输入:
输出:
2.代码实现:
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner(new BufferedInputStream(System.in));
int n = cin.nextInt();
StringBuilder ans = new StringBuilder();
while(n!=0){
if((n&1)==1){
n=(n-1)/2;
ans.append('2');
}else{
n=(n-2)/2;
ans.append('3');
}
}
System.out.println(ans.reverse());
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
边栏推荐
- String type conversion BigDecimal, date type
- I received a letter from CTO inviting me to interview machine learning engineer
- 指标陷阱:IT领导者易犯的七个KPI错误
- Manually implement function isinstanceof (child, parent)
- NIO与传统IO的区别
- Burpsuite simple packet capturing tutorial [easy to understand]
- 基于K-means的用户画像聚类模型
- 月入1W+的自媒体达人都会用到的运营工具
- EasyExcel 复杂数据导出
- Separate the letters and numbers in the string so that the letters come first and the array comes last
猜你喜欢
Flume面试题
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
MySQL系列之事务日志Redo log学习笔记
Application of real estate management based on 3D GIS
上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
News classification based on LSTM model
[noip2013] building block competition [noip2018] road laying greed / difference
List announced | outstanding intellectual property service team in China in 2021
从MLPerf谈起:如何引领AI加速器的下一波浪潮
随机推荐
Can you get a raise? Analysis on gold content of PMP certificate
MySQL learning notes - SQL optimization of optimization
为什么数字化转型战略必须包括持续测试?
AirServer手机第三方投屏电脑软件
Medium pen test questions: flip the string, such as ABCD, print out DCBA
PCB线路板塞孔工艺的那些事儿~
burpsuite简单抓包教程[通俗易懂]
Smart micro mm32 multi-channel adc-dma configuration
News classification based on LSTM model
EasyExcel 复杂数据导出
Spark面试题
【深度学习】利用深度学习监控女朋友的微信聊天?
【MySQL】explain的基本使用以及各列的作用
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
Using closures to switch toggle by clicking a button
TOPS,处理器运算能力单位、每秒钟可进行一万亿次
91.(cesium篇)cesium火箭发射模拟
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
【juc学习之路第9天】屏障衍生工具
MySQL series transaction log redo log learning notes