C#练习题答案: 人类可读的持续时间格式【难度:4级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

人类可读的持续时间格式【难度:4级】:

答案1:

public class HumanTimeFormat{
   
  public static string formatDuration(int seconds){
   
    //Enter Code here
            string s = "";
            int sec = seconds;
            int[] divArr = {
    60 * 60 * 24 * 365, 60 * 60 * 24, 60 * 60, 60, 1 };
            string[] nameArr = {
   "year","day","hour","minute","second"};

            if (seconds == 0)
            {
   
                s = "now";
            }

            for (int i = 0; i< divArr.Length; i++)
            {
   
                int k = sec / divArr[i];
                sec = sec % divArr[i];
                if (k != 0)
                {
   
                    string pref = "";
                    if (s != "")
                    {
   
                        if (sec == 0)
                        {
   
                            pref = " and ";    
                        }
                        else
                        {
   
                            pref = ", ";
                        }
                    }
                    s = s + pref + k.ToString() + " " + nameArr[i];
                    s += k > 1 ? "s" : "";
                }
            }
            return s;
  }
}

答案2:

public class HumanTimeFormat{
   
  public static string formatDuration(int seconds){
   
                string s = "";
            int sec = seconds;
            int[] divArr = {
    60 * 60 * 24 * 365, 60 * 60 * 24, 60 * 60, 60, 1 };
            string[] nameArr = {
   "year","day","hour","minute","second"};

            if (seconds == 0)
            {
   
                s = "now";
            }

            for (int i = 0; i< divArr.Length; i++)
            {
   
                int k = sec / divArr[i];
                sec = sec % divArr[i];
                if (k != 0)
                {
   
                    string pref = "";
                    if (s != "")
                    {
   
                        if (sec == 0)
                        {
   
                            pref = " and ";    
                        }
                        else
                        {
   
                            pref = ", ";
                        }
                    }
                    s = s + pref + k.ToString() + " " + nameArr[i];
                    s += k > 1 ? "s" : "";
                }
            }
            return s;
  }
}

答案3:

using System;
using System.Collections.Generic;
using System.Linq;

public class HumanTimeFormat{
   
  public static string formatDuration(int seconds){
   
    if (seconds == 0){
   
      return "now";
    }
    
    var time = TimeSpan.FromSeconds(seconds);
    var timesList = new string[]{
   
      MultipleFormat("year", time.Days / 365),
      MultipleFormat("day", time.Days % 365),
      MultipleFormat("hour", time.Hours),
      MultipleFormat("minute", time.Minutes),
      MultipleFormat("second", time.Seconds)
    };
    var list = timesList.Where(x => x != string.Empty).ToList();
    
    if (list.Count == 1){
   
      return list.First();
    }
    
    var firstPart = string.Join(", ", list.Take(list.Count - 1));
    
    return $"{firstPart} and {list.Last()}";
  }
  
  private static string MultipleFormat(string measure, double count){
   
    var c = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值