2017-04-10 86 views
0

我一直在试图解决这个黑客练习题(Compare the Triplets),我不知道我错在哪里。我的输出是正确的,但它并没有通过hackerrank的所有测试用例。有什么建议?比较三胞胎C#hackerrank改进我的解决方案

问题:

using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Linq; 
    class Solution { 

static void Main(String[] args) { 
    string[] tokens_a0 = Console.ReadLine().Split(' '); 
    int a0 = Convert.ToInt32(tokens_a0[0]); 
    int a1 = Convert.ToInt32(tokens_a0[1]); 
    int a2 = Convert.ToInt32(tokens_a0[2]); 
    string[] tokens_b0 = Console.ReadLine().Split(' '); 
    int b0 = Convert.ToInt32(tokens_b0[0]); 
    int b1 = Convert.ToInt32(tokens_b0[1]); 
    int b2 = Convert.ToInt32(tokens_b0[2]); 
    // Write Your Code Here 
    int aliceScore = 0; 
    int bobScore = 0; 


    if(a0 > b0 || a1 > b1 || a2 > b2) 
    { 
     aliceScore++;  
    } 
    if(b0 > a0 || b1 > a1 || b2 > a2) 
    { 
     bobScore++; 
    } 
    if(a0 == b0 || a1 == b1 || a2 == b2) 
    { 
     aliceScore += 0; 
     bobScore += 0; 
    } 

    Console.WriteLine(aliceScore +" " + bobScore); 

} 

}

+2

我认为你的3rd if语句没有任何用处。 – GER

+1

在你的版本中,任何一个玩家只能有0或1分。这显然是错误的 – UnholySheep

+0

让您的代码在问题 – Alyafey

回答

0

我想我看到的问题,你需要比较每个数据点并计算出每一个分数。所以,比较数据点0,则数据点1个,则数据点2.

的伪码以下时,没有测试:

if(a0 > b0) 
    { 
     aliceScore++;  
    } 
    else if(b0 > a0) 
    { 
     bobScore++; 
    } 


    if(a1 > b1) 
    { 
     aliceScore++; 
    } 
    else if(b1 > a1) 
    { 
     bobScore++; 
    } 

    if(a2 > b2) 
    { 
     aliceScore++; 
    } 
    else if(b2 > a2) 
    { 
     bobScore++; 
    } 
+0

Ty GER中提供约束。我现在意识到在每个比较点上我都没有攻击这个问题。有没有描述我的方法的技术术语?我是否也可以问你在看问题时的思维过程? – xslipstream

+0

对于这个小问题,我把它分解在纸上(在我的脑海里),手动解决它,而不写任何代码。这更难以解决更大的难题和现实世界的问题。我不确定你的原始方法是否有一个术语。 – GER

0

我认为这不是解决这个问题的正确道路。您应该遵循以下步骤 。

 int[] Alice = { a0, a1, a2 }; 
     int[] Bob = { b0, b1, b2 }; 
     int alice = 0; 
     int bob = 0; 
     for (int i = 0; i < Alice.Length; i++) 
     { 
      if (Alice[i] > Bob[i]) 
      { 
       alice++; 
      } 
      if (Alice[i] < Bob[i]) 
      { 
       bob++; 
      } 
     } 
     int[] result = { alice, bob }; 
     return result;