当前位置:网站首页>到底有多二(Lua)
到底有多二(Lua)
2022-07-07 15:38:00 【有趣就行】
题目
一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。
输入格式:
输入第一行给出一个不超过50位的整数N。
输出格式:
在一行中输出N犯二的程度,保留小数点后两位。
输入样例:
-13142223336
输出样例:
81.82%
代码
local n = io.read()
local ans= 0
for i = 1, #n do
if n:sub(i, i) == "2" then
ans = ans + 1
end
end
if n:sub(1, 1) == "-" then
ans = ans / (#n - 1) * 1.5
else
ans = ans / #n
end
if tonumber(n:sub(#n, #n)) % 2 == 0 then
ans = ans * 2
end
print(string.format("%.2f%%", ans * 100))
边栏推荐
- LeetCode 1696. 跳跃游戏 VI 每日一题
- 【黄啊码】为什么我建议您选择go,而不选择php?
- centos7安装mysql笔记
- LeetCode 1986. 完成任务的最少工作时间段 每日一题
- LeetCode 1043. 分隔数组以得到最大和 每日一题
- Notes on installing MySQL in centos7
- 【饭谈】如何设计好一款测试平台?
- 麒麟信安加入宁夏商用密码协会
- Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination
- 【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
猜你喜欢
redis主从、哨兵主备切换搭建一步一步图解实现
Seaborn数据可视化
A tour of grpc:03 - proto serialization / deserialization
状态模式 - Unity(有限状态机)
Pycharm IDE下载
Seaborn data visualization
科普达人丨一文弄懂什么是云计算?
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
麒麟信安加入宁夏商用密码协会
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
随机推荐
99% 用户在 Power BI 云端报表常犯错误
LeetCode 1654. 到家的最少跳跃次数 每日一题
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
How to add aplayer music player in blog
rpcms获取指定分类下的文章的方法
状态模式 - Unity(有限状态机)
Solidity函数学习
LeetCode 213. 打家劫舍 II 每日一题
LeetCode 152. Product maximum subarray daily question
麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
LeetCode 1186. Delete once to get the sub array maximum and daily question
Smart logistics platform: make overseas warehouses smarter
Matplotlib绘图界面设置
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
LeetCode 1986. 完成任务的最少工作时间段 每日一题
QT picture background color pixel processing method
Nerf: the ultimate replacement for deepfake?
Repair method of firewall system crash and file loss, material cost 0 yuan
Linux 安装mysql8.X超详细图文教程
LeetCode 1696. Jumping game VI daily question