基本上,我的朋友让我帮助他的c#期中考试。我同意。他向我发送了所有问题,我们解决了除最后一个之外的所有问题。我们一直得到 80/100 分,但不知道哪里出了问题。这不像 leetcode 那样,他们给出最后执行的输入,这样我们就可以用它来调试问题。只是“答案不正确”错误。
这就是我首先用Python编码的方式
deck = input().split(', ')
n = int(input())
def add(deck,command):
cardname = command[1]
if cardname in deck:
return 'Card is already in the deck'
deck.append(cardname)
return 'Card successfully added'
def remove(deck,command):
cardname = command[1]
if cardname in deck:
deck.remove(cardname)
return 'Card successfully removed'
return 'Card not found'
def removeat(deck,command):
index = int(command[1])
if index not in range(len(deck)):
return 'Index out of range'
deck.pop(index)
return 'Card successfully removed'
def insert(deck,command):
index = int(command[1])
cardname = command[2]
if index not in range(len(deck)):
return 'Index out of range'
if cardname in deck and index in range(len(deck)):
return 'Card is already in the deck'
deck.insert(index,cardname)
return 'Card successfully added'
for i in range(n):
command = input().split(', ')
if command[0]== 'Add' and len(command) ==2:
print(add(deck,command))
elif command[0]== 'Remove' and len(command) ==2:
print(remove(deck,command))
elif command[0]== 'Remove At'and len(command) ==2:
print(removeat(deck,command))
elif command[0]== 'Insert'and len(command) ==3:
print(insert(deck,command))
print(', '.join(deck))
然后我使用 chatGPT 将其翻译为 c#,因为我不懂这种语言
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> deck = new List<string>(Console.ReadLine().Split(", "));
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
string[] command = Console.ReadLine().Split(", ");
if (command.Length == 2 && command[0] == "Add")
{
Console.WriteLine(Add(deck, command));
}
else if (command.Length == 2 && command[0] == "Remove")
{
Console.WriteLine(Remove(deck, command));
}
else if (command.Length == 2 && command[0] == "Remove At")
{
Console.WriteLine(RemoveAt(deck, command));
}
else if (command.Length == 3 && command[0] == "Insert")
{
Console.WriteLine(Insert(deck, command));
}
}
Console.WriteLine(string.Join(", ", deck));
}
static string Add(List<string> deck, string[] command)
{
string cardName = command[1];
if (deck.Contains(cardName))
{
return "Card is already in the deck";
}
deck.Add(cardName);
return "Card successfully added";
}
static string Remove(List<string> deck, string[] command)
{
string cardName = command[1];
if (deck.Contains(cardName))
{
deck.Remove(cardName);
return "Card successfully removed";
}
return "Card not found";
}
static string RemoveAt(List<string> deck, string[] command)
{
int index = int.Parse(command[1]);
if (index >= 0 && index < deck.Count)
{
deck.RemoveAt(index);
return "Card successfully removed";
}
return "Index out of range";
}
static string Insert(List<string> deck, string[] command)
{
int index = int.Parse(command[1]);
string cardName = command[2];
if (index >= 0 && index <= deck.Count)
{
if (deck.Contains(cardName) && index >= 0 && index < deck.Count)
{
return "Card is already in the deck";
}
deck.Insert(index, cardName);
return "Card successfully added";
}
return "Index out of range";
}
}
if (deck.Contains(cardName) && index >= 0 && index < deck.Count)
- 这看起来很可疑,因为上面的子句使用了 <=
并且你不需要再次检查它。
输入以下内容:
Ace of Diamonds
1
Insert, 1, Ace of Diamonds
它将输出:
Card successfully added
Ace of Diamonds, Ace of Diamonds
这是不正确的。我相信
insert at
操作中的索引检查应该是index >= 0 && index < deck.Count
。所以完整的方法:
static string Insert(List<string> deck, string[] command)
{
int index = int.Parse(command[1]);
string cardName = command[2];
if (index >= 0 && index < deck.Count)
{
if (deck.Contains(cardName))
{
return "Card is already in the deck";
}
deck.Insert(index, cardName);
return "Card successfully added";
}
return "Index out of range";
}
}