03. Memory game
Loading...
+ Нов въпрос
PetarNeshkov5360 avatar PetarNeshkov5360 0 Точки

03. Memory game-Programming Fundamentals Mid Exam Retake - 12 August 2020

Здравейте! В джъдж ми дава 83/100 и не мога да си открия грешката. Ще бъда благодраен, ако някой я открие. Благодаря предварително!

https://pastebin.com/wY5U9vMu

линк към judge:https://judge.softuni.bg/Contests/Practice/Index/2517#2

03. Memory game

Write a program, which receives a sequence of elements. Each element in the sequence will have a twin. Until the player receives "end" from the console, he will receive strings with two integers separated by space, which represent the indexes of elements in the sequence. 

If the player tries to cheat and enters two equal indexes or indexes which are out of bounds of the sequence you should add two matching elements in the following format "-{number of moves until now}a" at the middle of the sequence and print this message on the console:

"Invalid input! Adding additional elements to the board"

Input

  • On the first line you will receive sequence of elements.

Output

  • Every time the player hit two matching elements you should remove them from the sequence and print on the console the following message:

"Congrats! You have found matching elements - ${element}!"

  • If the player hit two different elements, you should print on the console the following message:

"Try again!"

  • If the player hit all matching elements before he receives "end" from the console, you should print on the console the following message:

"You have won in {number of moves until now} turns!"

  • If the player receives "end" before he hits all matching elements, you should print on the console the following message:

"Sorry you lose :(

              {the current sequence's state}"

Constraints

  • All elements in the sequence will always have a matching element.

 

Examples

Input

Output

1 1 2 2 3 3 4 4 5 5

1 0

-1 0

1 0

1 0

1 0

end

Congrats! You have found matching elements - 1!

Invalid input! Adding additional elements to the board

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 3!

Congrats! You have found matching elements - -2a!

Sorry you lose :(

4 4 5 5

Comment

1)

1 0

1 1 2 2 3 3 4 4 5 5 –> 1 = 1, equal elements, so remove them. Moves: 1

2)

-1 0

-1 is invalid index so we add additional elements

2 2 3 3 -2а -2а 4 4 5 5, Moves: 2

3)

1 0

2 2 3 3 -2а -2а 4 4 5 5 -> 2 = 2, equal elements, so remove them. Moves: 3

4)

1 0

3 3 -2а -2а 4 4 5 5 -> 3 = 3, equal elements, so remove them. Moves: 4

5)

1 0

-2а -2а 4 4 5 5 -> -2а = -2а, equal elements, so remove them. Moves: 5

6)

We receive end command.

There are still elements in the sequence, so we loose the game.

Final state - 4 4 5 5

a 2 4 a 2 4

0 3

0 2

0 1

0 1

end

Congrats! You have found matching elements - a!

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 4!

You have won in 3 turns!

a 2 4 a 2 4

4 0

0 2

0 1

0 1

end

Try again!

Try again!

Try again!

Try again!

Sorry you lose :(

a 2 4 a 2 4

 

 

Input JavaScript

Output

 [

"1 1 2 2 3 3 4 4 5 5",

"1 0",

"-1 0",

"1 0",

"1 0",

"1 0",

"end"

]

Congrats! You have found matching elements - 1!

Invalid input! Adding additional elements to the board

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 3!

Congrats! You have found matching elements - -1a!

Sorry you lose :(

4 4 5 5

Comment

1)

1 0

1 1 2 2 3 3 4 4 5 5 –> 1 = 1, equal elements, so remove them. Moves: 1

2)

-1 0

-1 is invalid index so we add additional elements

2 2 3 3 -2а -2а 4 4 5 5, Moves: 2

3)

1 0

2 2 3 3 -2а -2а 4 4 5 5 -> 2 = 2, equal elements, so remove them. Moves: 3

4)

1 0

3 3 -2а -2а 4 4 5 5 -> 3 = 3, equal elements, so remove them. Moves: 4

5)

1 0

-2а -2а 4 4 5 5 -> -2а = -2а, equal elements, so remove them. Moves: 5

6)

We receive end command.

There are still elements in the sequence, so we loose the game.

Final state - 4 4 5 5

[

"a 2 4 a 2 4",

"0 3",

"0 2",

"0 1",

"0 1",

"end"

]

Congrats! You have found matching elements - a!

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 4!

You have won in 3 turns!

[

"a 2 4 a 2 4",

"4 0",

"0 2",

"0 1",

"0 1",

"end"

]

Try again!

Try again!

Try again!

Try again!

Sorry you lose :(

a 2 4 a 2 4

 

Тагове:
0
C# Fundamentals 27/08/2020 01:25:29
Axiomatik avatar Axiomatik 431 Точки
Best Answer

Rewrote your code a bit, but main-problem was most likely your if(end == false) validation which I moved to the end of the while loop at line 99.

New code(100%):

using System;
using System.Linq;
using System.Collections.Generic;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = Console.ReadLine().Split().ToList();

            string input = string.Empty;
            int counter = 0;
            bool end = false;
            while ((input = Console.ReadLine()) != "end")
            {
                string[] command = input.Split();
                //string firstIndex = command[0];
                //string secondIndex = command[1];
                int startIndex = int.Parse(command[0]);
                int endIndex = int.Parse(command[1]);
                bool sameIndex = startIndex == endIndex;

                //if (list.Count == 0 || list.Count == 1)
                //{
                //    end = true;
                //    Console.WriteLine($"You have won in {counter} turns!");
                //    break;
                //}

                if (startIndex < 0 || startIndex >= list.Count || sameIndex)
                {
                    Console.WriteLine("Invalid input! Adding additional elements to the board");
                    counter++;
                    string added = "-" + counter + "a";
                    list.Insert(list.Count / 2, added);
                    list.Insert(list.Count / 2, added);

                }
                else if (endIndex < 0 || endIndex >= list.Count)
                {
                    Console.WriteLine("Invalid input! Adding additional elements to the board");
                    counter++;
                    string added = "-" + counter + "a";
                    list.Insert(list.Count / 2, added);
                    list.Insert(list.Count / 2, added);

                }
                //else if (startIndex == endIndex)
                //{
                //    Console.WriteLine("Invalid input! Adding additional elements to the board");
                //    counter++;
                //    string added = "-" + counter + "a";
                //    list.Insert(list.Count / 2, (added + added));
                //    //list.Insert(list.Count / 2, added);
                //}
                else
                {
                    counter++;
                    if (list[startIndex] == list[endIndex])
                    {
                        Console.WriteLine($"Congrats! You have found matching elements - {list[startIndex]}!");
                        string target1 = list[startIndex];
                        string target2 = list[endIndex];

                        int targetIndex1 = list.FindIndex(x => x == target1);
                        list.RemoveAt(targetIndex1);

                        int targetIndex2 = list.FindIndex(x => x == target2);
                        list.RemoveAt(targetIndex2);

                        //if (firstIndex == "0")
                        //{
                        //    list.Remove(list[startIndex]);
                        //    list.Remove(list[endIndex - 1]);
                        //}
                        //else
                        //{
                        //    list.Remove(list[startIndex]);
                        //    list.Remove(list[endIndex]);
                        //}

                    }
                    else
                    {
                        Console.WriteLine("Try again!");
                    }
                }

                if (list.Count == 0 || list.Count == 1)
                {
                    end = true;
                    Console.WriteLine($"You have won in {counter} turns!");
                    break;
                }
            }

            if (end == false)
            {
                Console.WriteLine($"Sorry you lose :(");
                Console.WriteLine(String.Join(" ", list));
            }
        }
    }
}

0
krum_43 avatar krum_43 413 Точки

Здравей колега,

При тази задача има нещо странно в поведението на Judjesystem.

Написах код,който взема 83/100.

Странното е,че когато пусна нулевите тестове локално кода ми работи правилно,а джъджа се опитва да ме заблуди че извеждам грешни неща ?!?

Ето и линк към кода:

https://pastebin.com/M2AdMfww

 

0
31/08/2020 09:31:06