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

mac2025-01-25  34

猫年,狗年【难度: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)