猫年,狗年【难度: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};
}
答案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 };
}
}