Swift 很难让readLine()在HackerRank上按预期工作
我正试图提交30天的代码 我能够在Xcode操场上毫无问题地完成任务,但是HackerRank的网站说我的方法没有输出。我昨天遇到了一个问题,因为浏览器片状,但清理缓存,从Safari切换到Chrome等,似乎并不能解决我在这里遇到的问题。我认为我的问题在于Swift 很难让readLine()在HackerRank上按预期工作,swift,readline,Swift,Readline,我正试图提交30天的代码 我能够在Xcode操场上毫无问题地完成任务,但是HackerRank的网站说我的方法没有输出。我昨天遇到了一个问题,因为浏览器片状,但清理缓存,从Safari切换到Chrome等,似乎并不能解决我在这里遇到的问题。我认为我的问题在于inputString 任务 给定长度为N、索引范围为0到N-1的字符串S,将其偶数索引字符和奇数索引字符作为两个空格分隔的字符串打印在一行上(有关详细信息,请参见下面的示例) 输入格式 第一行包含一个整数(测试用例的数量)。 后续行的每一行
inputString
任务
给定长度为N、索引范围为0到N-1的字符串S,将其偶数索引字符和奇数索引字符作为两个空格分隔的字符串打印在一行上(有关详细信息,请参见下面的示例)
输入格式
第一行包含一个整数(测试用例的数量)。
后续行的每一行都包含一个字符串
约束条件
- 1
从标准输入中读取一行,其中 表示您的readLine()
仅包含 输入数据。您必须在循环中调用inputString
,才能获得 剩余的输入数据 因此,您的程序可以如下所示:readLine()
(此处可接受强制展开,因为func tweakString(string: String) -> String { // For a single input string, compute the output string according to the challenge rules ... return result } let N = Int(readLine()!)! // Number of test cases // For each test case: for _ in 1...N { let input = readLine()! let output = tweakString(string: input) print(output) }
输入数据记录在质询描述中。)你好,Adrian,您应该调用readLine()代码>每行。这是一个应对这一挑战的范例
<代码>导入基础 func letsReview(str:String){ var evenCharacters=“” var oddCharacters=“” var指数=0 对于str.characters中的char{ 如果索引%2==0{ evenCharacters+=字符串(字符) } 否则{ oddCharacters+=字符串(字符) } 指数+=1 } 打印(偶数字符+“”+奇数字符) } 让rowCount=Int(readLine()!)!import Foundation func letsReview(str:String){ var evenCharacters = "" var oddCharacters = "" var index = 0 for char in str.characters{ if index % 2 == 0 { evenCharacters += String(char) } else{ oddCharacters += String(char) } index += 1 } print (evenCharacters + " " + oddCharacters) } let rowCount = Int(readLine()!)! for _ in 0..<rowCount { letsReview(str:String(readLine()!)!) }
谢谢你!接下来,我将知道如何处理readLine()。我以为人力资源部在喂一根绳子,我必须把它拆开。import Foundation func letsReview(str:String){ var evenCharacters = "" var oddCharacters = "" var index = 0 for char in str.characters{ if index % 2 == 0 { evenCharacters += String(char) } else{ oddCharacters += String(char) } index += 1 } print (evenCharacters + " " + oddCharacters) } let rowCount = Int(readLine()!)! for _ in 0..<rowCount { letsReview(str:String(readLine()!)!) }