public class Kata
{
public static string WhatCentury(string year)
{
var century = (int.Parse(year) - 1) / 100 + 1;
return century.ToString() + GetSuffix(century);
}
private static string GetSuffix(int century)
{
if ((century / 10) % 10 != 1)
{
switch (century % 10)
{
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
}
}
return "th";
}
}
答案2:
using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static string WhatCentury(string year)
{
int century = Int32.Parse(year) / 100;
if (Int32.Parse(year) % 100 > 0)
{
century += 1;
}
return century.ToString() + WhatIsOrdinal(century);
}
public static string WhatIsOrdinal(int century)
{
if (century == 1 || century == 21)
{
return "st";
}
else if (century == 2 || century == 22)
{
return "nd";
}
else if (century == 3 || century == 23)
{
return "rd";
}
return "th";
}
}
答案3:
using System.Linq;
public class Kata
{
public static string WhatCentury(string year)
{
int cl = year.Length - 2;
int rest = (year.Remove(0,cl).Count(x => x == '0') >= 2) ? 1 : 0;
year = (int.Parse(year.Remove(cl)) + 1 - rest).ToString();
if (year[0] == '1') return year + "th";
else
switch (year[year.Length - 1])
{
case '1':
return year + "st";
case '2':
return year + "nd";
case '3':
return year + "rd";
default:
return year + "th";
}
}
}
答案4:
public class Kata
{
public static string WhatCentury(string year)
{
int century = (int.Parse(year) / 100);
century = (int.Parse(year) % 100) == 0 ? century : century + 1;
string terminator = "th";
terminator = century % 10 == 1 && century != 11 ? "st" : terminator;
terminator = century % 10 == 2 && century != 12 ? "nd" : terminator;
terminator = century % 10 == 3 && century != 13 ? "rd" : terminator;
return century + terminator;
}
}
答案5:
using System;
public class Kata
{
public static string WhatCentury(string year)
{
int century = (int)Math.Ceiling(int.Parse(year) / 100.0f);
string ending = "th";
switch(century % 10)
{
case 1:
ending = "st";
break;
case 2:
ending = "nd";
break;
case 3:
ending = "rd";
break;
}
switch (century % 100)
{
case 11:
case 12:
case 13:
ending = "th";
break;
}
return century + ending;
}
}
答案6:
public class Kata
{
public static string WhatCentury(string century)
{
int c = CalculateCentury(int.Parse(century));
string cent = c.ToString();
if (c > 10 && c < 14)
{
cent = cent + "th";
}
else
{
switch ((cent.ToString())[cent.Length - 1])
{
case '1':
cent = cent + "st";
break;
case '2':
cent = cent + "nd";
break;
case '3':
cent = cent + "rd";
break;
default:
cent = cent + "th";
break;
}
}
return cent;
}
private static int CalculateCentury(int year)
{
int century = 0;
if (year % 100 == 0)
{
century = year / 100;
}
else
{
century = year / 100 + 1;
}
return century;
}
}