C#期中卡牌操作程序仅获80分,无测试用例求错误排查
C#卡牌操作题目调试问题
我帮朋友完成C#期中试题,其他题目都已解决,但最后一道卡牌操作题仅拿到80分,系统仅显示"Incorrect Answer"错误,没有类似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))
转换后的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"; } }
可能的错误点及修正建议
Insert操作的索引逻辑不一致
Python版本中,Insert要求索引必须满足0 <= index < len(deck),而C#版本允许index == deck.Count(即插入到列表末尾),这会导致两种代码逻辑差异。若题目要求Insert不能插入到末尾,C#代码就会出错。Insert操作的卡片存在性判断冗余
C#的Insert方法中,判断卡片存在的条件额外加了index >=0 && index < deck.Count,导致当index == deck.Count时,即使卡片已存在也会执行插入,违反题目要求。
修正后的C# Insert方法
static string Insert(List<string> deck, string[] command) { int index = int.Parse(command[1]); string cardName = command[2]; // 和Python逻辑对齐,索引范围限制为0到deck.Count-1 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"; }
内容的提问来源于stack exchange,提问作者Peter
相关产品推荐
相关产品推荐

