Good vs Evil

Description

Middle Earth is about to go to war. The forces of good will have many battles with the forces of evil. Different races will certainly be involved. Each race has a certain 'worth' when battling against others. On the side of good we have the following races, with their associated worth:

  • Hobbits - 1
  • Men - 2
  • Elves - 3
  • Dwarves - 3
  • Eagles - 4
  • Wizards - 10

On the side of evil we have:

  • Orcs - 1
  • Men - 2
  • Wargs - 2
  • Goblins - 2
  • Uruk Hai - 3
  • Trolls - 5
  • Wizards - 10

Although weather, location, supplies and valor play a part in any battle, if you add up the worth of the side of good and compare it with the worth of the side of evil, the side with the larger worth will tend to win.

Thus, given the count of each of the races on the side of good, followed by the count of each of the races on the side of evil, determine which side wins.

Input:

The function will be given two parameters. Each parameter will be a string separated by a single space. Each string will contain the count of each race on the side of good and evil.

The first parameter will contain the count of each race on the side of good in the following order:

  • Hobbits, Men, Elves, Dwarves, Eagles, Wizards.

The second parameter will contain the count of each race on the side of evil in the following order:

  • Orcs, Men, Wargs, Goblins, Uruk Hai, Trolls, Wizards.

All values are non-negative integers. The resulting sum of the worth for each side will not exceed the limit of a 32-bit integer.

Output:

Return ""Battle Result: Good triumphs over Evil" if good wins, "Battle Result: Evil eradicates all trace of Good" if evil wins, or "Battle Result: No victor on this battle field" if it ends in a tie.

  1. using System;
  2. using System.Linq;
  3.  
  4. public class Kata
  5. {
  6. public static string GoodVsEvil(string good, string evil)
  7. {
  8. string result = "Battle Result: Good triumphs over Evil";
  9. int[] goodValue = { , , , , , };
  10. int[] evilValue = { , , , , , , };
  11. int[] goodCount = good.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
  12. int[] evilCount = evil.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
  13. int goodSum = Enumerable.Range(, goodValue.Length).Aggregate(, (sum, x) => sum + goodValue[x] * goodCount[x]);
  14. int evilSum = Enumerable.Range(, evilValue.Length).Aggregate(, (sum, x) => sum + evilValue[x] * evilCount[x]);
  15. if (goodSum == evilSum)
  16. {
  17. result = "Battle Result: No victor on this battle field";
  18. }
  19. else if (goodSum < evilSum)
  20. {
  21. result = "Battle Result: Evil eradicates all trace of Good";
  22. }
  23. return result;
  24. }
  25. }

其他人的解法:

  1. using System;
  2.  
  3. public class Kata
  4. {
  5. public enum GoodRacesValues
  6. {
  7. Hobbits = ,
  8. Men = ,
  9. Elves = ,
  10. Dwarves = ,
  11. Eagles = ,
  12. Wizards =
  13. }
  14.  
  15. public enum EvilRacesValues
  16. {
  17. Orcs = ,
  18. Men = ,
  19. Wargs = ,
  20. Goblins = ,
  21. UrukHai = ,
  22. Trolls = ,
  23. Wizards =
  24. }
  25.  
  26. public static string GoodVsEvil(string good, string evil)
  27. {
  28. string[] goodArmyValues = good.Split(' ');
  29. string[] evilArmyValues = evil.Split(' ');
  30. int goodArmyForces = Kata.CalculateForces<GoodRacesValues>(goodArmyValues);
  31. int evilArmyForces = Kata.CalculateForces<EvilRacesValues>(evilArmyValues);
  32.  
  33. return Kata.GetBattleResult(goodArmyForces, evilArmyForces);
  34. }
  35.  
  36. public static int CalculateForces<T>(string[] armyValues)
  37. {
  38. int i = ;
  39. int totalForces = ;
  40. foreach(T raceValue in Enum.GetValues(typeof(T)))
  41. {
  42. totalForces += Convert.ToInt32(raceValue) * int.Parse(armyValues[i]);
  43. ++i;
  44. }
  45. return totalForces;
  46. }
  47.  
  48. public static string GetBattleResult(int goodArmyForces, int evilArmyForces)
  49. {
  50. if (goodArmyForces > evilArmyForces)
  51. {
  52. return "Battle Result: Good triumphs over Evil";
  53. }
  54. else if (goodArmyForces < evilArmyForces)
  55. {
  56. return "Battle Result: Evil eradicates all trace of Good";
  57. }
  58. else
  59. {
  60. return "Battle Result: No victor on this battle field";
  61. }
  62. }
  63. }
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Kata
  5. {
  6. public static string GoodVsEvil(string good, string evil)
  7. {
  8. var gWorth = new[] { , , , , , };
  9. var eWorth = new[] { , , , , , , };
  10. var g = good.Split(' ').Select(int.Parse).Zip(gWorth, (f, s) => f * s).Sum();
  11. var b = evil.Split(' ').Select(int.Parse).Zip(eWorth, (f, s) => f * s).Sum();
  12. 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");
  13. }
  14. }

Good vs Evil的更多相关文章

  1. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  2. C#Light/Evil合体啦

    决定将C#Light和C#Evil合并成一个项目,毕竟C#Evil包含C#Light所有的功能,分开两个,基本的表达式方面有什么bug还得两头改 暂时就C#Light/Evil这么叫吧,庆祝合体,画了 ...

  3. C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.

    C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...

  4. Java unserialize serialized Object(AnnotationInvocationHandler、ysoserial) In readObject() LeadTo InvokerTransformer(Evil MethodName/Args)

    Java unserialize serialized Object(AnnotationInvocationHandler.ysoserial) In readObject() LeadTo Tra ...

  5. 只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs

    只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs [ide is evil] (http://i.cnblogs.com/EditP ...

  6. D. Book of Evil

    D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Gym 100463D Evil DFS

    Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...

  8. CF 337D Book of Evil 树形DP 好题

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  9. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...

  10. codeforces 337D 树形DP Book of Evil

    原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...

随机推荐

  1. union 和 union all 的区别

    Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All  两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...

  2. [jquery]高级篇--js绑定事件

    参考:  http://www.cnblogs.com/leejersey/p/3545372.html jQuery on()方法是官方推荐的绑定事件的一个方法.$(selector).on(eve ...

  3. JS原型与原型链终极详解(转)

    JavaScript原型及原型链详解 一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object,Function 是JS自带的函数对象. ...

  4. js闭包的使用例子

    网上关于闭包的介绍太多,这就导致了泛滥,对于新手来说,网上好多讲解就说了闭包是啥,还都是用下面这种例子: 我的天啊,我们都看了不知道多少遍了,看完有啥用?在什么场合下用啊? 于是我翻阅各种资料,自己总 ...

  5. D3序

    最近做公司的APM项目涉及到数据可视化,简单调研了一下目前业内推崇的工具,自然最终选择是非D3莫属,特别是看了官网上那些绝妙的示例之后,感觉这玩意儿炫到爆!选择D3最重要的一点是D3提供基础的必要的功 ...

  6. QML鼠标区域控制

    鼠标操作使用很多,下面给出一个示例: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Window 2.2 import Q ...

  7. 让hyper-v虚拟机中类ubuntu系统可以全屏

    很久之前一直都没有方法让linux虚拟机支持hyper-v的全屏,只能以1024x768或者800x600等方形屏幕 如果是windows7以前的电脑,可以用mstsc远程桌面修改分辨率,window ...

  8. c#个人记录常用方法(更新中)

    1.日期毫秒转换为标准的C#日期格式 //使用时,先将秒Convert.ToInt64,返回格式2015-2-10 14:03:33 public DateTime JavaTimeToC(long ...

  9. 在网页中插入qq连接

    <a href="tencent://message/?uin=这里写qq号 &Site=这里随便七个名字 &Menu=要为yes">显示出来的名字&l ...

  10. css写圆角效果

    .introTips i{ position: absolute; display: block; top: 8px; right: 8px; width:; height:; font-size:; ...