csharp 预定Cricket Mr. Hacker Earth Challenge

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 预定Cricket Mr. Hacker Earth Challenge相关的知识,希望对你有一定的参考价值。

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;

namespace ConsoleApplication13
{
    public class Program
    {
        static void Main(string[] args)
        {
            TestCases();
        }

        private static void TestCases()
        {
            var numberOfTestCases = ConsoleHelper.ReadInt();

            for (int i = 0; i < numberOfTestCases; i++)
            {
                GamePlay(i);
            }
        }

        private static void GamePlay(int game)
        {
            var inputInts = ConsoleHelper.ReadInts(2);
            var scoreInOver = Console.ReadLine().ToCharArray();

            var numberOfOvers = inputInts[0];
            var numberOfPlayers = inputInts[1];

            var numberOfBalls = numberOfOvers * 6;

            int[] players = new int[numberOfPlayers];

            for (int i = 0; i < numberOfPlayers; i++)
            {
                players[i] = -1;
            }

            var strickerIndex = 0;
            var nonStrikerIndex = 1;
            var nextPlayerIndex = 2;

            players[strickerIndex] = 0;
            players[nonStrikerIndex] = 0;

            for (int ball = 0; ball < numberOfBalls; ball++)
            {
                if (scoreInOver[ball] == 'W' || scoreInOver[ball] == 'w')
                {
                    strickerIndex = nextPlayerIndex;
                    players[strickerIndex] = 0;
                    ++nextPlayerIndex;
                }

                if (scoreInOver[ball] == '1')
                {
                    players[strickerIndex] = players[strickerIndex] + 1;

                    // Switch the Strikers.
                    var temp = nonStrikerIndex;
                    nonStrikerIndex = strickerIndex;
                    strickerIndex = temp;
                }

                if (scoreInOver[ball] == '2')
                {
                    players[strickerIndex] = players[strickerIndex] + 2;
                }

                if (scoreInOver[ball] == '4')
                {
                    players[strickerIndex] = players[strickerIndex] + 4;
                }

                if (scoreInOver[ball] == '6')
                {
                    players[strickerIndex] = players[strickerIndex] + 6;
                }

                if ((ball + 1)%6 == 0)
                {
                    // Switch the Strikers.
                    var temp = nonStrikerIndex;
                    nonStrikerIndex = strickerIndex;
                    strickerIndex = temp;
                }
            }

            Console.WriteLine("Case {0}:", game + 1);

            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] == -1)
                {
                    Console.WriteLine("Player {0}: {1}", i + 1, "DNB");
                    continue;
                }

                if (i == strickerIndex || i == nonStrikerIndex)
                {
                    Console.WriteLine("Player {0}: {1}{2}", i +1,  players[i], "*");
                }

                else
                {
                    Console.WriteLine("Player {0}: {1}", i + 1, players[i]);
                }
            }
        }
    }

    /// <summary>
    /// Fast Console IO helper
    /// </summary>
    internal sealed class ConsoleHelper
    {
        public static int ReadInt()
        {
            string input = Console.ReadLine();
            int value;

            while (!int.TryParse(input, out value))
            {
                input = Console.ReadLine();
            }

            return value;
        }

        public static int[] ReadInts(int n)
        {
            string input = Console.ReadLine();

            var inputStrings = input.Split(' ');
            
            int[] array = new int[n];
            for (int i = 0; i < n; i++)
            {
                int value;
                int.TryParse(inputStrings[i], out value);
                array[i] = value;
            }

            return array;
        }
    }
}

以上是关于csharp 预定Cricket Mr. Hacker Earth Challenge的主要内容,如果未能解决你的问题,请参考以下文章

UVA 11982 Fantasy Cricket

习题 8-19 UVA-1312Cricket Field

uva 1312Cricket Field(算法效率--技巧枚举)

即时窗口中的动态导致“Microsoft.CSharp.RuntimeBinder.Binder”未定义或导入错误

跨域 jquery ajax 请求

将对象数组的数组简化为对象数组