当前位置:网站首页>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)
边栏推荐
- LeetCode 1986. 完成任务的最少工作时间段 每日一题
- Nerf: the ultimate replacement for deepfake?
- QT picture background color pixel processing method
- QML初学
- Sator launched Web3 game "satorspace" and launched hoobi
- LeetCode 1049. 最后一块石头的重量 II 每日一题
- 测试用例管理工具推荐
- LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
- Solid function learning
- mysql实现两个字段合并成一个字段查询
猜你喜欢
What is cloud computing?
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
Reflections on "product managers must read: five classic innovative thinking models"
Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination
Seaborn数据可视化
测试用例管理工具推荐
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
Sator推出Web3遊戲“Satorspace” ,並上線Huobi
SlashData开发者工具榜首等你而定!!!
QT 图片背景色像素处理法
随机推荐
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
Flask搭建api服务-SQL配置文件
Repair method of firewall system crash and file loss, material cost 0 yuan
rpcms获取指定分类下的文章的方法
LeetCode 1043. 分隔数组以得到最大和 每日一题
浅浅理解.net core的路由
MySQL implements the query of merging two fields into one field
如何选择合适的自动化测试工具?
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
DNS series (I): why does the updated DNS record not take effect?
Sator launched Web3 game "satorspace" and launched hoobi
[Fantan] how to design a test platform?
LeetCode 312. Poke balloon daily
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Direct dry goods, 100% praise
从DevOps到MLOps:IT工具怎样向AI工具进化?
What is cloud computing?
MRS离线数据分析:通过Flink作业处理OBS数据
Flask build API service SQL configuration file