Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

Idriz1983 avatar Idriz1983 5 Точки

Не можах да разбера точно какво искат от тази задача! Ще може ли някой да ми го обясни? на C# е задачата.

1.Cards Game

You will be given two hands of cards, which will be integer numbers. Assume that you have two players. You have to find out the winning deck and respectively the winner.

You start from the beginning of both hands. Compare the cards from the first deck to the cards from the second deck. The player, who has the bigger card, takes both cards and puts them at the back of his hand - the second player's card is last, and the first person's card (the winning one) is before it (second to last) and the player with the smaller card must remove the card from his deck. If both players’ cards have the same values - no one wins, and the two cards must be removed from the decks. The game is over, when one of the decks is left without any cards. You have to print the winner on the console and the sum of the left cards: "{First/Second} player wins! Sum: {sum}".

Examples

Input

Output

20 30 40 50

10 20 30 40

First player wins! Sum: 240

10 20 30 40 50

50 40 30 30 10

Second player wins! Sum: 50

Тагове:
Axiomatik avatar Axiomatik 2422 Точки

;-), Demo:

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

namespace cardsGame
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> player1 = Console.ReadLine().Split().Select(int.Parse).ToList();
            List<int> player2 = Console.ReadLine().Split().Select(int.Parse).ToList();

            // The game is over, when one of the decks is left without any cards.
            while (player1.Count > 0 && player2.Count > 0)
            {
                //The player, who has the bigger card, takes both cards and puts 
                //them at the back of his hand - the second player's card is last, 
                //and the first person's card (the winning one) is before it 
                //(second to last) and the player with the smaller card must remove 
                //the card from his deck.
                if (player1[0] > player2[0])
                {
                    player1.Add(player1[0]);
                    player1.Add(player2[0]);
                    player1.RemoveAt(0);
                    player2.RemoveAt(0);
                }
                else if (player1[0] < player2[0])
                {
                    player2.Add(player2[0]);
                    player2.Add(player1[0]);
                    player2.RemoveAt(0);
                    player1.RemoveAt(0);
                }
                else if (player1[0] == player2[0])
                {
                    // If both players’ cards have the same values - no one wins, 
                    //and the two cards must be removed from the decks.
                    player1.RemoveAt(0);
                    player2.RemoveAt(0);
                }
            }

            //You have to print the winner on the console and the sum of the left cards:
            int totalSum = 0;
            if (player1.Count == 0)
            {
                foreach (var item in player2)
                {
                    totalSum += item;
                }
                Console.WriteLine($"Second player wins! Sum: {totalSum}");
            }
            else if (player2.Count == 0)
            {
                foreach (var item in player1)
                {
                    totalSum += item;
                }
                Console.WriteLine($"First player wins! Sum: {totalSum}");
            }
        }
    }
}

 

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.