C#练习题答案: 这是几个世纪?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战
作者:互联网
这是几个世纪?【难度:2级】:
答案1:
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;
}
}
答案7:
public class Kata
{
public static string WhatCentury(string year)
{
var cent = ((int.Parse(year) - 1) / 100) + 1;
var sufix = "th";
if(cent % 10 == 1) sufix = "st";
if(cent % 10 == 2) sufix = "nd";
if(cent % 10 == 3) sufix = "rd";
if(cent / 10 % 10 == 1) sufix = "th";
return cent + sufix;
}
}
答案8:
public class Kata
{
public static string WhatCentury(string year)
{
string num = (int.Parse(year) % 100 == 0 ? int.Parse(year) / 100 : int.Parse(year) / 100 + 1).ToString();
string end = num == "11" || num == "12" || num == "13" ? "th" :
num.EndsWith("1") ? "st" : num.EndsWith("2") ? "nd" : num.EndsWith("3") ? "rd" : "th";
return num + end;
}
}
答案9:
using System;
public class Kata
{
static string yearshort(string year)
{
int ct;
if ((Convert.ToInt32(year) % 100 == 0))
{
ct = ((Convert.ToInt32(year) / 100));
}
else
{
ct = ((Convert.ToInt32(year) / 100)) + 1;
}
int A = Convert.ToInt32(ct.ToString());
if (A == 1) return ($"{ct}th");
else
{
if (A == 0) return ($"{ct}th");
else if (A == 1) return ($"{ct}st");
else if (A == 2) return ($"{ct}nd");
else if (A == 3) return ($"{ct}rd");
}
return ($"{ct}th");
}
static string yearlong(string year)
{
int ct;
if ((Convert.ToInt32(year) % 100 == 0))
{
ct = ((Convert.ToInt32(year) / 100));
}
else
{
ct = ((Convert.ToInt32(year) / 100)) + 1;
}
int A = Convert.ToInt32(ct.ToString().Substring(0, 1));
int B = Convert.ToInt32(ct.ToString().Substring(1, 1));
if (A == 1) return ($"{ct}th");
else
{
if (B == 0) return ($"{ct}th");
else if (B == 1) return ($"{ct}st");
else if (B == 2) return ($"{ct}nd");
else if (B == 3) return ($"{ct}rd");
}
return ($"{ct}th");
}
public static string WhatCentury(string year)
{
Console.WriteLine(year);
if (year.Length < 4) return yearshort(year);
else return yearlong( year);
}
}
答案10:
using System;
public class Kata
{
public static string WhatCentury(string year)
{
int century = (int)(Convert.ToInt32(year) / 100) + ((Convert.ToInt32(year) % 100 == 0) ? 0 : 1);
string end = century > 3 && century < 21 ? "th" : century % 10 == 1 ? "st" : century % 10 == 2 ? "nd" : century % 10 == 3 ? "rd" : "th";
return century + end;
}
}
标签:练习题,return,景越,century,C#,int,year,ct,string 来源: https://blog.csdn.net/weixin_45444821/article/details/100812071