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.

using System;
using System.Linq; public class Kata
{
public static string GoodVsEvil(string good, string evil)
{
string result = "Battle Result: Good triumphs over Evil";
int[] goodValue = { , , , , , };
int[] evilValue = { , , , , , , };
int[] goodCount = good.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
int[] evilCount = evil.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
int goodSum = Enumerable.Range(, goodValue.Length).Aggregate(, (sum, x) => sum + goodValue[x] * goodCount[x]);
int evilSum = Enumerable.Range(, evilValue.Length).Aggregate(, (sum, x) => sum + evilValue[x] * evilCount[x]);
if (goodSum == evilSum)
{
result = "Battle Result: No victor on this battle field";
}
else if (goodSum < evilSum)
{
result = "Battle Result: Evil eradicates all trace of Good";
}
return result;
}
}

其他人的解法:

using System;

public class Kata
{
public enum GoodRacesValues
{
Hobbits = ,
Men = ,
Elves = ,
Dwarves = ,
Eagles = ,
Wizards =
} public enum EvilRacesValues
{
Orcs = ,
Men = ,
Wargs = ,
Goblins = ,
UrukHai = ,
Trolls = ,
Wizards =
} 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 = ;
int totalForces = ;
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";
}
}
}
using System;
using System.Linq; public class Kata
{
public static string GoodVsEvil(string good, string evil)
{
var gWorth = new[] { , , , , , };
var eWorth = new[] { , , , , , , };
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");
}
}

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. 12天学好C语言——记录我的C语言学习之路(Day 7)

    12天学好C语言--记录我的C语言学习之路 Day 7: 昨天进行了一天的数组学习,今天大家可以先写几个昨天的程序热热身,回顾回顾,然后今天第一个新程序也是关于数组的,比较难,准备好就开始啦! //输 ...

  2. 解决div布局中第一个div的margin-top在浏览器中显示无效果问题。

    原味来源:http://www.hicss.net/do-not-tell-me-you-understand-margin/ 垂直外边距合并问题 别被上面这个名词给吓倒了,简单地说,外边距合并指的是 ...

  3. windows2003可用gt630显卡驱动

    http://file2.mydrivers.com/display/301.42-desktop-winxp-32-international-whql.exe 驱动精灵自动下载的不好用,这个版本可 ...

  4. JSP编程中常用的JavaScript技术(转载)

    1.<tronMouseOver=this.style.backgroundColor=’#FFFFFF’ onMouseOut=this.style.backgroundColor=”> ...

  5. bzoj1016:[JSOI2008]最小生成树计数

    思路:模拟kruskal的过程,可以发现对于所有权值相同的边,有很多种选择的方案,而且权值不同的边并不会相互影响,因为先考虑权值较小的边,权值比当前权值大的边显然不在考虑范围之内,而权值比当前权值小的 ...

  6. MySQL中,修改表的某一字段的部分值

    语法:update 表名 set 字段名 = replace(字段名,'替换前内容','替换后的内容') where 条件. 如: 执行sql语句:update student set name = ...

  7. android的liveview装载数据

    设置布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro ...

  8. ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践

    写在前面 :从提出需求到完美的解决问题,实现过程是曲折的. 需求:在前(web client)后(Restful Service)端完全解耦的模式框架下,webclient需要请求 Service 返 ...

  9. Unity3D--学习太空射击游戏制作(四)

    步骤七:添加声音和特效(射击声音和爆炸效果) 01:在Project窗口单机右键,选择Import Package->Custome Package,然后到资源文件目录packages浏览uni ...

  10. jQuery name checked 模糊查找匹配ID

    ("div[name='jobTitle']") $("#aDiv").find("input[type='checkbox']:checked&qu ...