提问人:Peter 提问时间:10/22/2023 更新时间:10/22/2023 访问量:60
一副纸牌数组,用函数操作,打印出错误
Deck of Cards array, manipulated with functions, prints out error
问:
基本上,我的朋友让我帮助他的 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";
}
}
答:
0赞
Aleksei Petrov
10/22/2023
#1
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";
}
}
评论
0赞
Peter
10/22/2023
在这里提交问题之前,我尝试这样做,但没有用。现在。。。我尝试了完全相同的方法,它奏效了。多谢!!
0赞
Ineffable21
10/22/2023
#2
我认为由于使用了索引,您没有得到预期的结果。在 C# 中,数组从 0 开始。意思是,为了从牌组中取出第一张牌,您需要.要获得第 7 张卡,您需要 .您可以在输入和输出示例中看到这一点。在第一个例子中,我们可以看到,输入并没有给出你应该放入的数组的索引,而是给出了在甲板上的位置。index = 0
index = 6
Insert, 2, Jack of Spades
显然,在输出中,黑桃杰克位于甲板中的第二个位置,这将是阵列中的位置。[1]
因此,为了获得卡片的灵活性,您需要始终从索引号中取出 1。例:
int index = int.Parse(command[1]);
index--;
评论