我有SPOJ的问题, 在我的所有解决方案中,我遇到了NZEC问题。我已经读过这个问题(非零退出代码)。
SPOJ链接:http://www.spoj.com/problems/TEST/
我的代码如下:
for i in 1...100000{
let number = Int(readLine()!)!
if number != 42{
print(number)
}
else
{
break
}
}
PS:在Xcode中我创建了命令行项目,在控制台中我有信息:程序以退出代码结束:0
这个问题出现在Swift中。
答案 0 :(得分:1)
以下是Swift中Spoj的TEST问题中处理IO的正确方法:
while let input = readLine(), Int(input) != 42 {
if !input.isEmpty {
print(input)
} else {
print("Input cannot be empty")
continue
}
}
它读取IO数据,直到要读取的内容或输入数据中出现 42 的数字。
此示例可用作Ideone上的Swift语言的示例代码:http://ideone.com/samples#sample_lang_85。您还可以在此处查看此代码:http://ideone.com/lH6iZS。
答案 1 :(得分:0)
如果您不想在号码42后退出:
for i in 1...100
{
let number = Int(readLine()!)!
if number != 42
{
print(number)
}
else
{
continue
}
}
但在这种情况下,else部分没用!我真的不明白这个问题。对不起,如果这是我的误解。