C#练习题答案: 这是几个世纪?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#练习题答案: 这是几个世纪?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4724字,纯文字阅读大概需要7分钟。
内容图文
这是几个世纪?【难度: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;
}
}?
内容总结
以上是互联网集市为您收集整理的C#练习题答案: 这是几个世纪?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战全部内容,希望文章能够帮你解决C#练习题答案: 这是几个世纪?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。