using System; using System.Collections; using System.Collections.Generic; using…
- User
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApp5 { internal class Program { static void Main(string[] args) { string[] NKP = Console.ReadLine().Split(); int N = int.Parse(NKP[0]); int K = int.Parse(NKP[1]); int P = int.Parse(NKP[2]);
List<int> anwing = new List<int>(); List<List<int>> list = new List<List<int>>(); for (int i = 0; i < K; i++) { List<int> newList = new List<int>(); list.Add(newList); anwing.Add(0); } for (int i = 0; i < N; i++) { string[] o = Console.ReadLine().Split(); if (o[0] == "+") { list[int.Parse(o[1]) - 1].Add(int.Parse(o[2])); if (list[int.Parse(o[1]) - 1].Count() > anwing[int.Parse(o[1]) - 1]) { anwing[int.Parse(o[1]) - 1] = list[int.Parse(o[1]) - 1].Count(); } } else if (o[0] == "-") { if (list[int.Parse(o[1]) - 1][list[int.Parse(o[1]) - 1].Count()-1] == int.Parse(o[2])) { list[int.Parse(o[1]) - 1].RemoveAt(list[int.Parse(o[1]) - 1].Count() - 1); } else { Console.WriteLine("Error"); } } if (list[int.Parse(o[1]) - 1].Count() > P) { Console.WriteLine("Error"); } } Console.WriteLine(anwing.Sum()); Console.ReadLine(); } }} конвертируй в pyton
- Assistant
Конвертированная версия программы на Python:
NKP = input().split() N = int(NKP[0]) K = int(NKP[1]) P = int(NKP[2])
anwing = [] list = []
for i in range(K): newList = [] list.append(newList) anwing.append(0)
for i in range(N): o = input().split()
if o[0] == "+": list[int(o[1]) - 1].append(int(o[2])) if len(list[int(o[1]) - 1]) > anwing[int(o[1]) - 1]: anwing[int(o[1]) - 1] = len(list[int(o[1]) - 1]) elif o[0] == "-": if list[int(o[1]) - 1][len(list[int(o[1]) - 1]) - 1] == int(o[2]): list[int(o[1]) - 1].pop() else: print("Error") if len(list[int(o[1]) - 1]) > P: print("Error")print(sum(anwing))
- User
NKP = input().split() N = int(NKP[0]) K = int(NKP[1]) P = int(NKP[2])
anwing = [] list = []
for i in range(K): newList = [] list.append(newList) anwing.append(0)
for i in range(N): o = input().split()
if o[0] == “+”: list[int(o[1]) - 1].append(int(o[2])) if len(list[int(o[1]) - 1]) > anwing[int(o[1]) - 1]: anwing[int(o[1]) - 1] = len(list[int(o[1]) - 1]) elif o[0] == “-”: if list[int(o[1]) - 1][len(list[int(o[1]) - 1]) - 1] == int(o[2]): list[int(o[1]) - 1].pop() else: print(“Error”)
if len(list[int(o[1]) - 1]) > P: print(“Error”)
print(sum(anwing)) в чём ошибка?
- Assistant
В данном коде ошибка в использовании кавычек в строках с операторами "+", "-" и выводом ошибки. В языке Python используются двойные кавычки (") или одинарные кавычки (') для обозначения строковых значений. В вашем коде используются неправильные кавычки, которые являются недопустимыми символами в Python.
Вот исправленная версия кода:
NKP = input().split() N = int(NKP[0]) K = int(NKP[1]) P = int(NKP[2])
anwing = [] list = []
for i in range(K): newList = [] list.append(newList) anwing.append(0)
for i in range(N): o = input().split()
if o[0] == "+": list[int(o[1]) - 1].append(int(o[2])) if len(list[int(o[1]) - 1]) > anwing[int(o[1]) - 1]: anwing[int(o[1]) - 1] = len(list[int(o[1]) - 1]) elif o[0] == "-": if list[int(o[1]) - 1][len(list[int(o[1]) - 1]) - 1] == int(o[2]): list[int(o[1]) - 1].pop() else: print("Error") if len(list[int(o[1]) - 1]) > P: print("Error")print(sum(anwing)) Теперь код должен работать без ошибок.
- User
ошибка та же, ничего не изменилась
- Assistant
Приношу извинения за путаницу. Ошибка в вашем коде заключается в неправильной табуляции. В Python, в отличие от других языков программирования, необходимо использовать правильную табуляцию внутри блоков кода, чтобы обозначить их принадлежность к одному уровню.
Вот исправленный код с правильной табуляцией:
NKP = input().split() N = int(NKP[0]) K = int(NKP[1]) P = int(NKP[2])
anwing = [] list = []
for i in range(K): newList = [] list.append(newList) anwing.append(0)
for i in range(N): o = input().split()
if o[0] == "+": list[int(o[1]) - 1].append(int(o[2])) if len(list[int(o[1]) - 1]) > anwing[int(o[1]) - 1]: anwing[int(o[1]) - 1] = len(list[int(o[1]) - 1]) elif o[0] == "-": if list[int(o[1]) - 1][len(list[int(o[1]) - 1]) - 1] == int(o[2]): list[int(o[1]) - 1].pop() else: print("Error") if len(list[int(o[1]) - 1]) > P: print("Error")print(sum(anwing)) Пожалуйста, убедитесь, что весь код имеет правильную табуляцию, состоящую из 4 пробелов каждый уровень вложенности.
- User
ошибка в 22 строчке
- Assistant
В 22 строке кода у вас использованы двойные кавычки (“”) вместо двойных кавычек ("). В Python принято использовать двойные кавычки или одинарные кавычки для обозначения строковых значений.
Вот исправленный код на 22 строке:
if o[0] == "+": list[int(o[1]) - 1].append(int(o[2])) if len(list[int(o[1]) - 1]) > anwing[int(o[1]) - 1]: anwing[int(o[1]) - 1] = len(list[int(o[1]) - 1]) elif o[0] == "-": if list[int(o[1]) - 1][len(list[int(o[1]) - 1]) - 1] == int(o[2]): list[int(o[1]) - 1].pop() else: print("Error")
if len(list[int(o[1]) - 1]) > P: print("Error")
Если после этой правки по-прежнему возникают ошибки, пожалуйста, укажите код ошибки или сообщение об ошибке для дальнейшего анализа.