LoginSignup
0
0

More than 1 year has passed since last update.

第18回オフラインリアルタイムどう書くの解答@ C#

Last updated at Posted at 2014-02-01

解答発表直前でテストが通りました(ほっ)。

public class Register
{
    public int CustomerCount;
    public List<string> CustomerGroups;
    public bool IsStop;
    public int Minus;

    public Register(int minus)
    {
        CustomerCount = 0;
        CustomerGroups = new List<string>();
        IsStop = false;
        Minus = minus;
    }

    public void Shori()
    {
        int customerIndex = 0;
        int last = Minus;
        for (int i = 0; i < CustomerGroups.Count(); i++)
        {
            string customer = CustomerGroups[i];
            if (IsStop) break;

            if (customer == "x")
            {
                IsStop = true;
                break;
            }
            else
            {
                int customerNumber = int.Parse(customer);
                if (customerNumber <= last)
                {
                    CustomerCount -= customerNumber;
                    last -= customerNumber;
                    customerIndex++;
                }
                else
                {
                    CustomerCount -= last;
                    CustomerGroups[customerIndex] = (customerNumber - last).ToString();
                    break;
                }
            }
        }
        if (CustomerCount < 0)
        {
            CustomerCount = 0;
        }
        CustomerGroups.RemoveRange(0, customerIndex);
    }
}
public string Solve(string input) {
    List<Register> registers = new List<Register> { new Register(2), new Register(7), new Register(3), new Register(5), new Register(2) };

    char[] chars = input.ToCharArray();
    foreach (char c in chars) {
        string s = c.ToString();
        if (s == ".") {
            foreach(var register in registers) {
                register.Shori();
            }
        }
        else if (s == "x")
        {
            registers[registers.FindIndex(x => x.CustomerCount == registers.Min<Register>(y => y.CustomerCount))].CustomerGroups.Add(s);
            registers[registers.FindIndex(x => x.CustomerCount == registers.Min<Register>(y => y.CustomerCount))].CustomerCount += 1;
        }
        else
        {
            registers[registers.FindIndex(x => x.CustomerCount == registers.Min<Register>(y => y.CustomerCount))].CustomerGroups.Add(s);
            registers[registers.FindIndex(x => x.CustomerCount == registers.Min<Register>(y => y.CustomerCount))].CustomerCount += int.Parse(s);
        }
    }
    string result = null;
    registers.ForEach(x => result = result + x.CustomerCount.ToString() + ",");
    return result.Remove(result.Length - 1);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0