C#练习题答案: 猫年,狗年【难度:0级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

猫年,狗年【难度:0级】:

答案1:

public class Dinglemouse {

  public static int[] humanYearsCatYearsDogYears(int humanYears) {
    
    int catYear = 15 + (humanYears >= 2 ? 9 + 4 * (humanYears-2): 0);
    int dogYear = 15+ (humanYears >= 2 ? 9 + 5 * (humanYears-2): 0);
   
    return new int[]{humanYears,catYear ,dogYear};
  }

}

答案2:

public class Dinglemouse {

  public static int[] humanYearsCatYearsDogYears(int humanYears) {
    if(humanYears == 0) return new int[]{0,0,0};
    int catYears = 0, dogYears = 0;
    if(humanYears == 1)
    {
      catYears = 15;
      dogYears = 15;
    }
    else if(humanYears == 2)
    {
      catYears = 15 + 9;
      dogYears = 15 + 9;
    }
    else
    {
      catYears = 15 + 9 + (humanYears - 2) * 4;
      dogYears = 15 + 9 + (humanYears - 2) * 5;
    }
    return new int[]{humanYears,catYears,dogYears};
  }

}

答案3:

public class Dinglemouse {

  public static int[] humanYearsCatYearsDogYears(int humanYears) => humanYears < 2 ? new int[]{1,15,15} : humanYears < 3 ? new int[]{2,24,24} : new int[]{humanYears,24+(humanYears-2)*4,24+(humanYears-2)*5};
  }
  
  // {  
  //  int[] output = new int[]{humanYears,0,0};
    
  //  if(humanYears == 1){
  //    output[1]= 15;
  //    output[2]= 15;
  //    }
  //  else if(humanYears == 2){
  //    output[1]= 24;
  //    output[2]= 24;
  //    }
  //  else{
  //    output[1]= 24 + 4*(humanYears-2);
  //    output[2]= 24 + 5*(humanYears-2);
  //  }
  //  return output;
  //  }
  //}​

答案4:

public class Dinglemouse {

  public static int[] humanYearsCatYearsDogYears(int humanYears) {
    switch(humanYears)
    {
    case 1:
     return new int[]{1,15,15};
    case 2:
      return new int[]{2,24,24};
    default:
     int catYears = 24;
     int dogYears = 24;
     
     for(int i = humanYears -2; i > 0; i--)
     {
       catYears += 4;
       dogYears += 5;
     }
      return new int[]{humanYears,catYears,dogYears};
    }
  }

}

答案5:

public class Dinglemouse 
{
  public static int[] humanYearsCatYearsDogYears(int y) => y < 2 ? new[]{1, 15, 15} : new[]{y, 16+4*y, 14+5*y};
}

答案6:

public class Dinglemouse
{
    public static int[] humanYearsCatYearsDogYears(int humanYears)
    {
        switch (humanYears)
        {
            case 1: return new[] {1, 15, 15};
            case 2: return new[] {2, 24, 24};
            default: return new[] {humanYears, (humanYears - 2) * 4 + 15 + 9, (humanYears - 2) * 5 + 15 + 9};
        }
    }
}

答案7:

public class Dinglemouse {

  public static int[] humanYearsCatYearsDogYears(int humanYears) =>
    new int[]{humanYears,(humanYears + 3)*4 + (humanYears > 1 ? 5 : 0) -1 ,(humanYears + 2)*5 + (humanYears > 1 ? 4 : 0)};

}

答案8:

public class Dinglemouse 
{
  public static int[] humanYearsCatYearsDogYears(int humanYears) 
  {
    int commonAdd = humanYears == 1 ? 15 : humanYears >= 2 ? 24: 0;
    int catYears = commonAdd;
    int dogYears = commonAdd; 
    if (humanYears > 2)
    {
      for (int i = 3; i <= humanYears; i++)
      {
        catYears += 4;
        dogYears += 5;
      }
    }
    return new int[]{humanYears,catYears,dogYears};
  }
}

答案9:

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

public class Dinglemouse
{
  public static int[] humanYearsCatYearsDogYears(int humanYears)
  {
    return new int[]
    {
      humanYears,
      CatYears().Take(humanYears).Sum(),
      DogYears().Take(humanYears).Sum()
    };
  }

  static IEnumerable<int> CatYears()
  {
    yield return 15;
    yield return 9;
    while (true)
    {
      yield return 4;
    }
  }

  static IEnumerable<int> DogYears()
  {
    yield return 15;
    yield return 9;
    while (true)
    {
      yield return 5;
    }
  }
}

答案10:

public class Dinglemouse
{

  public static int[] humanYearsCatYearsDogYears(int humanYears)
  {
    int catAge = (humanYears == 1) ? 15 : (humanYears == 2) ? 24 : 24 + ((humanYears - 2) * 4);
    int dogAge = (humanYears == 1) ? 15 : (humanYears == 2) ? 24 : 24 + ((humanYears - 2) * 5);
    
    return new int[] { humanYears, catAge, dogAge };
  }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值