C#练习题答案: 好对邪恶【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

好对邪恶【难度:2级】:

答案1:

using System;

public class Kata
{
  public enum GoodRacesValues
  {
    Hobbits = 1,
    Men = 2,
    Elves = 3,
    Dwarves = 3,
    Eagles = 4,
    Wizards = 10
  }
  
  public enum EvilRacesValues
  {
    Orcs = 1,
    Men = 2,
    Wargs = 2,
    Goblins = 2,
    UrukHai = 3,
    Trolls = 5,
    Wizards = 10
  }

  public static string GoodVsEvil(string good, string evil)
  {
     string[] goodArmyValues = good.Split(' ');
     string[] evilArmyValues = evil.Split(' ');
     int goodArmyForces = Kata.CalculateForces<GoodRacesValues>(goodArmyValues);
     int evilArmyForces = Kata.CalculateForces<EvilRacesValues>(evilArmyValues);
  
     return Kata.GetBattleResult(goodArmyForces, evilArmyForces);
  }
  
  public static int CalculateForces<T>(string[] armyValues)
  {
      int i = 0;
      int totalForces = 0;
      foreach(T raceValue in Enum.GetValues(typeof(T)))
      {
        totalForces += Convert.ToInt32(raceValue) * int.Parse(armyValues[i]);
        ++i;
      }
      return totalForces;
  }
  
  public static string GetBattleResult(int goodArmyForces, int evilArmyForces)
  {
      if (goodArmyForces > evilArmyForces)
      {
        return "Battle Result: Good triumphs over Evil";
      } 
      else if (goodArmyForces < evilArmyForces)
      {
        return "Battle Result: Evil eradicates all trace of Good"; 
      }
      else
      {
        return "Battle Result: No victor on this battle field";       
      }
  }
}

答案2:

using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
    var gWorth = new[] { 1, 2, 3, 3, 4, 10 };
            var eWorth = new[] { 1, 2, 2, 2, 3, 5, 10 };
            var g = good.Split(' ').Select(int.Parse).Zip(gWorth, (f, s) => f * s).Sum();
            var b = evil.Split(' ').Select(int.Parse).Zip(eWorth, (f, s) => f * s).Sum();
            return (g > b) ? "Battle Result: Good triumphs over Evil" : ((g == b) ? "Battle Result: No victor on this battle field" : "Battle Result: Evil eradicates all trace of Good");
  }
}

答案3:

using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
    var powerOfGood =new[] {1,2,3,3,4,10};
    var powerOfEvil = new[] {1,2,2,2,3,5,10};
  
    var resultOfGood = good.Split(' ')
      .Select(x => int.Parse(x))
      .Zip(powerOfGood, (m,p) => m*p)
      .Sum(); 
    var resultOfEvil = evil.Split(' ')
      .Select(x => int.Parse(x))
      .Zip(powerOfEvil, (m,p) => m*p)
      .Sum(); 
    var result = resultOfGood >  resultOfEvil
      ? "Battle Result: Good triumphs over Evil"
      : resultOfGood < resultOfEvil
        ? "Battle Result: Evil eradicates all trace of Good"
        : "Battle Result: No victor on this battle field";

    return result;    
  }
}

答案4:

using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
    int[] goodNumbers = {1, 2, 3, 3, 4, 10};
    int[] evilNumbers = {1, 2, 2, 2, 3, 5, 10};
    
    Func<string, int[], int> calculatePower = (army, numbers) => army.Split(' ').Select(int.Parse).Zip(numbers, (a, b) => a * b).Sum();
    
    int goodPower = calculatePower(good, goodNumbers);
    int evilPower = calculatePower(evil, evilNumbers);
    
    return goodPower == evilPower
      ? "Battle Result: No victor on this battle field"
      : goodPower > evilPower
        ? "Battle Result: Good triumphs over Evil"
        : "Battle Result: Evil eradicates all trace of Good";
  }
}

答案5:

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

public class Kata
{
  public static int[] GoodPowerz = new [] { 1, 2, 3, 3, 4,    10 };
  public static int[] EvilPowerz = new [] { 1, 2, 2, 2, 3, 5, 10 };
  
  public static Dictionary<int, string> BattleOutcomes = new Dictionary<int, string> {
      { -1, "Battle Result: Evil eradicates all trace of Good" },
      {  0, "Battle Result: No victor on this battle field" },
      { +1, "Battle Result: Good triumphs over Evil" }
  };

  public static string GoodVsEvil(string good, string evil)
  {
    var goodHits = Attack(GoodPowerz, good);
    var evilHits = Attack(EvilPowerz, evil);
    return BattleOutcomes[goodHits.CompareTo(evilHits)];    
  }
  
  private static int Attack(int[] powerz, string groupSizes)
  {
    return groupSizes.Split(' ').Select(int.Parse)
      .Zip(powerz, (d, p) => d * p)
      .Sum();
  }
}

答案6:

using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
    var goodPoints = new[] { 1, 2, 3, 3, 4, 10 };
    var evilPoints = new[] { 1, 2, 2, 2, 3, 5, 10 };

    var goodResult = good.Split(' ')
                         .Select(int.Parse)
                         .Select((x, i) => x * goodPoints[i])
                         .Sum();

    var evilResult = evil.Split(' ')
                         .Select(int.Parse)
                         .Select((x, i) => x * evilPoints[i])
                         .Sum();

    return goodResult == evilResult
        ? "Battle Result: No victor on this battle field"
        : (goodResult > evilResult
            ? "Battle Result: Good triumphs over Evil"
            : "Battle Result: Evil eradicates all trace of Good");
  }
}

答案7:

using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
            var teamGandalf = new int[] { 1, 2, 3, 3, 4, 10 };
            var teamSauron = new int[] { 1, 2, 2, 2, 3, 5, 10 };

            var teamGandalfStrength = good.Split(' ')
                .Select(x => int.Parse(x.ToString()))
                .Zip(teamGandalf, (first, second) => first*second)
                .Sum();

            var teamSauronStrength = evil.Split(' ')
                .Select(x => int.Parse(x.ToString()))
                .Zip(teamSauron, (first, second) => first*second)
                .Sum();

            if (teamGandalfStrength > teamSauronStrength)
                return "Battle Result: Good triumphs over Evil";

            return teamSauronStrength > teamGandalfStrength ? "Battle Result: Evil eradicates all trace of Good" : "Battle Result: No victor on this battle field";
  }
}

答案8:

using System;
using System.Linq;

public class Kata
{
    public static string GoodVsEvil(string good, string evil)
    {
      var goodList = ConvertToList(good);
      var goodCount = CalculateGood(goodList);
      var evilList = ConvertToList(evil);
      var evilCount = CalculateEvil(evilList);
      return GetGoodVsEvilResult(goodCount, evilCount);
    }

    private static int[] ConvertToList(string s)
    {
      return s.Split(' ').Select(int.Parse).ToArray();
    }

    private static int CalculateGood(int[] l)
    {
      return l[0] + 2*l[1] + 3*l[2] + 3*l[3] + 4*l[4] + 10*l[5];
    }

    private static int CalculateEvil(int[] l)
    {
      return l[0] + 2 * l[1] + 2 * l[2] + 2 * l[3] + 3 * l[4] + 5 * l[5] + 10 * l[6];
    }

    private static string GetGoodVsEvilResult(int goodCount, int badCount)
    {
      if (goodCount == badCount) return "Battle Result: No victor on this battle field";
      return goodCount > badCount 
        ? "Battle Result: Good triumphs over Evil" 
        : "Battle Result: Evil eradicates all trace of Good";
    }
}

答案9:

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

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
    int[] powersOfGood = {1, 2, 3, 3, 4, 10};
    int[] powersOfEvil = {1, 2, 2, 2, 3, 5, 10};
  
    Func<string, int[], int> calcPower = (fraction, powers) => fraction.Split(' ')
      .Select(s => Convert.ToInt32(s))
      .Zip(powers, (count, power) => count * power)
      .Sum();

    Dictionary<int, string> resultOfBattle = new Dictionary<int, string>() {
      {-1, "Battle Result: Evil eradicates all trace of Good"},
      {0, "Battle Result: No victor on this battle field"},
      {1, "Battle Result: Good triumphs over Evil"}
    };
    
    int whoIsWin = calcPower(good, powersOfGood).CompareTo(calcPower(evil, powersOfEvil));

    return resultOfBattle[whoIsWin];
  }
}

答案10:

using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
        {
            var parsedGood = CalculateGood(good);
            var parsedEvil = CalculateEvil(evil);

            if (parsedGood == parsedEvil)
            {
                return "Battle Result: No victor on this battle field";
            }
            
;
            return parsedGood > parsedEvil ? "Battle Result: Good triumphs over Evil" 
                : "Battle Result: Evil eradicates all trace of Good";
        }

        private static int CalculateGood(string good)
        {
            var parsedGood = good.Split().Select(int.Parse).ToList();
            return parsedGood[0] + (parsedGood[1] * 2) + (parsedGood[2] * 3)
                + (parsedGood[3] * 3) + (parsedGood[4] * 4) + (parsedGood[5] * 10);
            
        }

        private static int CalculateEvil(string evil)
        {
            var parsedEvil = evil.Split().Select(int.Parse).ToList();
            return parsedEvil[0] + (parsedEvil[1] * 2) + (parsedEvil[2] * 2)
                + (parsedEvil[3] * 2) + (parsedEvil[4] * 3) + (parsedEvil[5] * 5) + (parsedEvil[6] * 10);
        }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值