我一直在尝试解决这个黑客排名练习题(比较三元组),但我不知道我错在哪里。 我的输出是正确的,但它没有通过 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);
}
}
我想我看到了问题,你需要比较每个数据点并计算每个数据点的分数。因此,比较数据点 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++;
}
GER 提供了解决问题背后的逻辑步骤。 理想情况下,您的解决方案应该能够满足 Alice 或 Bob 数组中提供的任何输入。 假设我们有两个 int 类型的列表。
var a = new List<int> {10, 11, 12, 14, 45 };
var b = new List<int> {8, 15, 10, 12, 55 };
假设列表“a”代表 Alice 的得分,列表“b”代表 Bob。 该解决方案需要比较任一列表中相同索引位置的条目,即从左到右列表“a”中的数字 10 位于索引 0 处,列表“b”中的数字 8 也位于索引 0 处。 处理比较任务的函数将接受两个列表作为输入,然后返回包含 2 个条目的列表。 该函数的返回结果将是 Alice 的分数和 Bob 的分数。 请参阅下面的示例解决方案
void Main()
{
var a = new List<int> { 10, 11, 12, 14, 45 };
var b = new List<int> { 8, 15, 10, 12, 55 };
var result = CompareScores(a, b);
//Expected result is :
// Alice: 3
// Bob: 2
Console.WriteLine($"Alice score: {result.ElementAt(0)} Bob score: {result.ElementAt(1)}");
}
public List<int> CompareScores(List<int> alice, List<int> bob)
{
var ScoreAlice = 0;
var ScoreBob = 0;
var i = 0;
alice.ForEach(scoreAlice => {
if(scoreAlice > bob.ElementAt(i))
{
ScoreAlice ++;
}
if(scoreAlice < bob.ElementAt(i))
{
ScoreBob ++;
}
i++;
});
return new List<int> {ScoreAlice, ScoreBob };
}
在我的解决方案通知中,我使用 Linq 迭代代表 Alice 的列表,并且对于每个元素,我与 Bob 的列表进行比较,使用“i”的值识别该元素。
Alice 和 Bob 各自为 HackerRank 创建了一个编码问题。 每个问题都会根据清晰度、原创性和难度进行审查和评级。 数据存储在两个整数数组或列表 a 和 b 中。 挑战是比较每个分数,并根据 Alice 和 Bob 的总分将最高分奖励一分。 然后将总分数添加到整数数组或 2 个元素的列表中,并从方法或函数compareTriplets 返回。
解决方案:
var a = new List<int> { 17, 28, 30 };
var b = new List<int> { 99, 16, 8 };
var result = Basic.compareTriplets(a, b);
Console.WriteLine(string.Join(" ", result));
static List<int> compareTriplets(List<int> a, List<int> b)
{
int[] result = new int[2];
for (int i = 0; i < a.Count; i++)
{
if (a[i] > b[i])
{
result[0]++;
}
else if (a[i] < b[i])
{
result[1]++;
}
}
return result.ToList();
}
结果:2 1
此解决方案可插入当前版本的 HackerRank 并通过所有测试用例。
工作解决方案:
public static List<int> compareTriplets(List<int> a, List<int> b)
{
int alice = 0;
int bob = 0;
for(int j =0 ; j < a.Count; j++){
if (a[j] > b[j]) {
alice +=1;
}else if(b[j] > a[j]){
bob +=1;
}else{
continue;
}
}
return new List<int> {alice, bob};
}
我认为这不是解决这个问题的正确方法。你应该遵循这些 步骤如下。
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;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the compareTriplets function below.
static List<int> compareTriplets(List<int> a, List<int> b) {
int alice=0,bob=0;
for(int i=0;i<a.Count;i++)
{
if(a.ElementAt(i)>b.ElementAt(i))
{
alice=alice+1;
}
else if(a.ElementAt(i)<b.ElementAt(i))
{
bob=bob+1;
}
}
return new List<int> {alice,bob};
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
List<int> a = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(aTemp => Convert.ToInt32(aTemp)).ToList();
List<int> b = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(bTemp => Convert.ToInt32(bTemp)).ToList();
List<int> result = compareTriplets(a, b);
textWriter.WriteLine(String.Join(" ", result));
textWriter.Flush();
textWriter.Close();
}
}
类解决方案{
// Complete the compareTriplets function below.
static List<int> compareTriplets(List<int> a, List<int> b) {
int A = 0;
int B = 0;
for(int i=0; i < a.Count; i++){
if(a[i] > b[i]){
A++;
}else if(a[i] < b[i]){
B++;
}
}
return new List<int> {A,B};
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
List<int> a = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(aTemp => Convert.ToInt32(aTemp)).ToList();
List<int> b = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(bTemp => Convert.ToInt32(bTemp)).ToList();
List<int> result = compareTriplets(a, b);
textWriter.WriteLine(String.Join(" ", result));
textWriter.Flush();
textWriter.Close();
}
}
我认为这会很好用。
List<int> res = new() { 0, 0 };
for (int i = 0; i < a.Count; i++)
{
if (a[i] > b[i]) res[0]++;
else if (a[i] < b[i]) res[1]++;
}
vector<int> compareTriplets(vector<int> a, vector<int> b)
{
int testsubjects=0;
int s=0;
int d=0;
for(int i=0;i<testsubjects;i++)
{
s+= a[i]>b[i] ? 1:0;
d+=a[i]<b[i] ? 1:0;
}
vector<int> vect = vector<int> {s,d};
return vect;
}
你可以尝试一下:
int alice = 0;
int bob = 0;
int totalTest = 3;
List<int> result = new List<int>();
for(var i=0; i<totalTest; i++)
{
if(a[i] > b[i]){
alice++;
}else if(a[i] < b[i]){
bob++;
}
}
result.Add(alice);
result.Add(bob);
return result;
var res = new List<int>{0,0};
var count = a.Count > b.Count ? a :b;
for(int i =0;i<count.Count;i++){
if(a[i]>b[i]){
res[0]+=1;
}
else if(b[i]>a[i]){
res[1]+=1;
}
else{
}
}
return res;
}