我正在尝试完成此
a = [5,6,7] b = [3,6,10]
现在,让我们比较每个人的得分:
a[0] > b[0], so Alice receives point.
a[1] == b[1], so nobody receives a point.
a[2] < b[2] , so Bob receives point.
Alice的比较得分为1,而Bob的比较得分为1.因此,我们必须在一行上打印1 1(Alice的比较得分,然后是Bob的比较得分)。
我是这样做的:
var a = [5, 6, 7]
var b = [3, 6, 10]
let pointsAlice = (a[0] > b[0] ? 1: 0) + (a[1] > b[1] ? 1: 0) + (a[2] > b[2] ? 1: 0)
let pointsBob = (a[0] < b[0] ? 1: 0) + (a[1] < b[1] ? 1: 0) + (a[2] < b[2] ? 1: 0)
print("\(pointsAlice) \(pointsBob)")
但它显示为不正确的答案(Hackerrank)。 我的方式是正确的还是有其他方法可以解决这个问题?
答案 0 :(得分:2)
请试试这个......
let a = readLine()!.components(separatedBy: " ").flatMap { Int($0) }
let b = readLine()!.components(separatedBy: " ").flatMap { Int($0) }
let aResult = zip(a, b).filter(>).count
let bResult = zip(b, a).filter(>).count
//Final Result Status
print(aResult, bResult)
答案 1 :(得分:1)
我想你更专注于解释:p 试试这个:
let arrA = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let arrB = readLine()!.components(separatedBy: " ").map{ Int($0)! }
var aliceScore = 0
var bobScore = 0
for index in 0...2{
if (arrA[index] > arrB[index]){
aliceScore += 1
}
if (arrA[index] < arrB[index]){
bobScore += 1
}
}
print("\(aliceScore)" + " " + "\(bobScore)")
答案 2 :(得分:0)
为什么不使用像reduce这样的高阶函数呢?
let a = [5, 6, 7]
let b = [3, 6, 10]
let pointsAlice = zip(a, b).reduce(0, combine: { $0 + ($1.0 > $1.1 ? 1 : 0) } )
let pointsBob = zip(b, a).reduce(0, combine: { $0 + ($1.0 > $1.1 ? 1 : 0) } )
print("\(pointsAlice) \(pointsBob)") // 1 1
答案 3 :(得分:0)
不要像这样硬编码
(a[0] > b[0] ? 1: 0) + (a[1] > b[1] ? 1: 0) + (a[2] > b[2] ? 1: 0)
想象一下,如果数组有1000个或更多元素!!!
我认为最简单的解决方案是:
var alicePoints = 0
var bobPoints = 0
for i in 0..<A.count {
if A[i] > B[i] {
alicePoints += 1
} else if A[i] < B[i] {
bobPoints += 1
}
}
print("\(alicePoints) \(bobPoints)")
更优雅的解决方案是上面的@koropok(我为Swift 3.x提升了)
let bobPoints = zip(B, A).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) }
let alicePoints = zip(A, B).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) }
print("\(alicePoints) \(bobPoints)")
答案 4 :(得分:0)
这是更新三胞胎的答案
如果我们有
var a = [5, 6, 7]
var b = [3, 6, 10]
然后
// compareTriplets function
func compareTriplets(a: [Int], b: [Int]) -> [Int] {
let bobPoints = zip(b, a).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) }
let alicePoints = zip(a, b).reduce(0) { $0 + ($1.0 > $1.1 ? 1 : 0) }
print("Points earned by Alice and Bob : \(alicePoints) \(bobPoints)")
return [alicePoints, bobPoints]
}
答案 5 :(得分:0)
这是我的解决方案。试图了解reduce函数的工作原理。
func compareTriplets(a: [Int], b: [Int]) -> [Int] { var total = [0,0] for x in 0..<a.count { if a[x] > b[x] { total[0] += 1 } else if a[x] < b[x] { total[1] += 1 } } return aTotal }
答案 6 :(得分:0)
尝试一下:
return [zip(a, b).reduce(0) {$0 + ($1.0 > $1.1 ? 1 : 0)},
zip(b, a).reduce(0) {$0 + ($1.0 > $1.1 ? 1 : 0)}]
答案 7 :(得分:0)
在python中,不考虑提出的约束(ab >= 1 & ab<=100):
def compareTriplets(a, b):
i = 0
score = [0,0]
while i < 3:
if a[i] > b[i]:
score[0] = score[0] + 1
elif a[i] < b[i]:
score[1] = score[1] + 1
i = i + 1
return score
答案 8 :(得分:-1)
import java.util.*.;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int klice=0;
int bob=0;
int a[]=new int[n];
int b[]=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
b[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
if(a[i]>b[i]){
klice++;
} else if(b[i]>a[i]){
bob++;
}
}
System.out.println(klice +" "+ bob);
}
}