ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#练习题答案: 这是几个世纪?【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

2019-09-13 22:39:01  阅读:248  来源: 互联网

标签:练习题 return 景越 century C# int year ct string


这是几个世纪?【难度: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 &amp;&amp; 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 &amp;&amp; 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有