Created
February 1, 2014 11:07
-
-
Save dproject21/8750831 to your computer and use it in GitHub Desktop.
第18回オフラインリアルタイムどう書くの解答@ C# ref: http://qiita.com/dproject21/items/2ac35231595a47e9eae7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment