当前位置:网站首页>L1-025 正整数A+B(Lua)
L1-025 正整数A+B(Lua)
2022-07-07 15:38:00 【有趣就行】
题目
题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000]。稍微有点麻烦的是,输入并不保证是两个正整数。
输入格式:
输入在一行给出A和B,其间以空格分开。问题是A和B不一定是满足要求的正整数,有时候可能是超出范围的数字、负数、带小数点的实数、甚至是一堆乱码。
注意:我们把输入中出现的第1个空格认为是A和B的分隔。题目保证至少存在一个空格,并且B不是一个空字符串。
输出格式:
如果输入的确是两个正整数,则按格式A + B = 和输出。如果某个输入不合要求,则在相应位置输出?,显然此时和也是?。
输入样例1:
123 456
输出样例1:
123 + 456 = 579
输入样例2:
22. 18
输出样例2:
? + 18 = ?
输入样例3:
-100 blabla bla…33
输出样例3:
? + ? = ?
代码
function io.read2value()
local s = io.read()
local n = #s
for i = 1, n do
if s:sub(i, i) == " " then
return s:sub(1, i - 1), s:sub(i + 1)
end
end
end
local s1, s2 = io.read2value()
local flag1, flag2 = true, true
if tostring(tonumber(s1)) ~= s1 or s1:find(".", 1, true) ~= nil then
flag1 = false
end
if tostring(tonumber(s2)) ~= s2 or s2:find(".", 1, true) ~= nil then
flag2 = false
end
local s = ""
local n1, n2 = tonumber(s1), tonumber(s2)
if n1 and (n1 <= 0 or n1 > 1000) then flag1 = false end
if n2 and (n2 <= 0 or n2 > 1000) then flag2 = false end
s = s .. (flag1 and n1 or "?")
s = s .. " + "
s = s .. (flag2 and n2 or "?")
s = s .. " = "
s = s .. ((flag1 and flag2) and (n1 + n2) or "?")
print(s)
边栏推荐
- 麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
- Seaborn data visualization
- LeetCode 120. 三角形最小路径和 每日一题
- [video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial
- Arduino 控制的双足机器人
- LeetCode 1043. Separate the array to get the maximum and daily questions
- QML初学
- Module VI
- The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
- LeetCode 213. 打家劫舍 II 每日一题
猜你喜欢

PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight

麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会

Module VI

SlashData开发者工具榜首等你而定!!!

Arduino 控制的双足机器人

How to add aplayer music player in blog

LeetCode刷题day49

QML初学

Sator a lancé le jeu web 3 "satorspace" et a lancé huobi

Sator推出Web3游戏“Satorspace” ,并上线Huobi
随机推荐
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
Test case management tool recommendation
LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
NeRF:DeepFake的最终替代者?
LeetCode 1049. Weight of the last stone II daily question
QML beginner
Leetcode brush questions day49
mysql实现两个字段合并成一个字段查询
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
Flask搭建api服务-生成API文档
[image sensor] correlated double sampling CDs
LeetCode 120. 三角形最小路径和 每日一题
The computer cannot add a domain, and the Ping domain name is displayed as the public IP. What is the problem? How to solve it?
智慧物流平台:让海外仓更聪明
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
DNS 系列(一):为什么更新了 DNS 记录不生效?
skimage学习(1)
The top of slashdata developer tool is up to you!!!
Shallow understanding Net core routing