Skip to content

Instantly share code, notes, and snippets.

@dproject21
Created April 6, 2013 10:30
Show Gist options
  • Save dproject21/5325673 to your computer and use it in GitHub Desktop.
Save dproject21/5325673 to your computer and use it in GitHub Desktop.
第9回オフラインリアルタイムどう書くの解答 問題はこれ http://nabetani.sakura.ne.jp/hena/ord9busfare/
using System;
using System.Text.RegularExpressions;
namespace yhpg9
{
public class MyClass
{
public MyClass ()
{
}
public decimal CalcUnchin (string Inputs)
{
MatchCollection payMatchs = Regex.Matches (Inputs, "[0-9]+:");
decimal pay = 0;
foreach (Match payItem in payMatchs) {
pay += Convert.ToDecimal(payItem.Value.Replace(":", ""));
}
MatchCollection personMatchs = Regex.Matches (Inputs, "[A-Za-z]{2}");
decimal payAll = 0;
int youjiNormal = 0;
int youjiTeiki = 0;
int youjiFukushi = 0;
int otonaCount = 0;
foreach (Match person in personMatchs) {
decimal personPay = pay;
string personStr = person.Value;
string personType = personStr.Substring (0, 1);
string payType = personStr.Substring (1, 1);
if (personType == "I")
{
switch (payType) {
case "w":
youjiFukushi++;
continue;
case "p":
youjiTeiki++;
continue;
default :
youjiNormal++;
continue;
}
}
if (personType == "C")
{
personPay = (Math.Ceiling(personPay / 10 / 2)) * 10;
}
if (personType == "A")
{
otonaCount++;
}
switch (payType) {
case "w":
personPay = (Math.Ceiling(personPay / 10 / 2))* 10;
break;
case "p":
personPay = 0;
break;
default:
break;
}
payAll += personPay;
}
int youjiCount = 0;
decimal youjiPay = (Math.Ceiling(pay / 10 / 2))* 10;
decimal fukushiYoujiPay = (Math.Ceiling(youjiPay / 10 / 2)) * 10;
if (youjiNormal > otonaCount) {
int payedYouji = youjiNormal - 2 * otonaCount;
payAll += payedYouji * youjiPay;
}
youjiCount += youjiNormal;
if (youjiCount > 2 * otonaCount) {
payAll += youjiFukushi * fukushiYoujiPay;
} else if (youjiCount + youjiFukushi > 2 * otonaCount) {
youjiCount += youjiFukushi;
int payedYouji = youjiCount - 2 * otonaCount;
payAll += payedYouji * fukushiYoujiPay;
}
return payAll;
}
}
}
using System;
using NUnit.Framework;
using System.Collections.Generic;
namespace yhpg9
{
[TestFixture()]
public class Test
{
[Test()]
public void TestCase ()
{
MyClass myClass = new MyClass ();
Dictionary<string,string> testCases = TestCases ();
foreach (KeyValuePair<string,string> testCase in testCases) {
decimal result = myClass.CalcUnchin(testCase.Key);
Assert.AreEqual(Convert.ToDecimal(testCase.Value),result,"Wrong :" + testCase.Key);
Console.WriteLine(String.Concat(testCase.Key, " : ", result.ToString()));
}
}
Dictionary<string,string> TestCases()
{
Dictionary<string,string> test = new Dictionary<string, string>();
test.Add("210:Cn,In,Iw,Ap,Iw","170");
test.Add("220:Cp,In","110");
test.Add("230:Cw,In,Iw","240");
test.Add("240:In,An,In","240");
test.Add("250:In,In,Aw,In","260");
test.Add("260:In,In,In,In,Ap","260");
test.Add("270:In,An,In,In,Ip","410");
test.Add("280:Aw,In,Iw,In","210");
test.Add("200:An","200");
test.Add("210:Iw","60");
test.Add("220:Ap","0");
test.Add("230:Cp","0");
test.Add("240:Cw","60");
test.Add("250:In","130");
test.Add("260:Cn","130");
test.Add("270:Ip","0");
test.Add("280:Aw","140");
test.Add("1480:In,An,In,In,In,Iw,Cp,Cw,In,Aw,In,In,Iw,Cn,Aw,Iw","5920");
test.Add("630:Aw,Cw,Iw,An,An","1740");
test.Add("340:Cn,Cn,Ip,Ap","340");
test.Add("240:Iw,Ap,In,Iw,Aw","120");
test.Add("800:Cw,An,Cn,Aw,Ap","1800");
test.Add("1210:An,Ip,In,Iw,An,Iw,Iw,An,Iw,Iw","3630");
test.Add("530:An,Cw,Cw","810");
test.Add("170:Aw,Iw,Ip","90");
test.Add("150:In,Ip,Ip,Iw,In,Iw,Iw,In,An,Iw,Aw,Cw,Iw,Cw,An,Cp,Iw","580");
test.Add("420:Cn,Cw,Cp","320");
test.Add("690:Cw,In,An,Cp,Cn,In","1220");
test.Add("590:Iw,Iw,Cn,Iw,Aw,In,In,Ip,Iw,Ip,Aw","1200");
test.Add("790:Cw,Cn,Cn","1000");
test.Add("1220:In,In,An,An,In,Iw,Iw,In,In,Ip,In,An,Iw","4590");
test.Add("570:Cw,Cn,Cp","440");
test.Add("310:Cn,Cw,An,An,Iw,Cp,Cw,Cn,Iw","1100");
test.Add("910:Aw,In,Iw,Iw,Iw,Iw,Iw,An,Cw,In","2290");
test.Add("460:Iw,Cw,Cw,Cn","590");
test.Add("240:Iw,Iw,In,Iw,In,In,Cn,In,An","780");
test.Add("1240:In,In,In,Ap,In,Cw,Iw,Iw,Iw,Aw,Cw","2170");
test.Add("1000:Iw,Ip,In,An,In,In,In,An,In,Iw,In,In,Iw,In,Iw,Iw,Iw,An","5500");
test.Add("180:In,Aw,Ip,Iw,In,Aw,In,Iw,Iw,In","330");
test.Add("440:In,Ip,Cp,Aw,Iw,In,An","660");
test.Add("1270:Ap,In,An,Ip,In,Ip,Ip","1270");
return test;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment