Good vs Evil
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的更多相关文章
- 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 ...
- C#Light/Evil合体啦
决定将C#Light和C#Evil合并成一个项目,毕竟C#Evil包含C#Light所有的功能,分开两个,基本的表达式方面有什么bug还得两头改 暂时就C#Light/Evil这么叫吧,庆祝合体,画了 ...
- C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.
C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...
- Java unserialize serialized Object(AnnotationInvocationHandler、ysoserial) In readObject() LeadTo InvokerTransformer(Evil MethodName/Args)
Java unserialize serialized Object(AnnotationInvocationHandler.ysoserial) In readObject() LeadTo Tra ...
- 只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs
只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs [ide is evil] (http://i.cnblogs.com/EditP ...
- D. Book of Evil
D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Gym 100463D Evil DFS
Evil Time Limit: 5 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descri ...
- 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 ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
- codeforces 337D 树形DP Book of Evil
原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...
随机推荐
- Oracle Text Slowly
When oracle text more and more slowly, execute the following script: ANALYZE TABLE Table_Name COMPU ...
- JavaScript学习笔记(1)——JavaScript简介
JavaScript一种解释性脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,该引擎为浏览器的一部分.JavaScript最早是用 ...
- JS基本类型和引用类型的值
JS中可以把变量分成两部分,基本类型和引用类型. 基本类型比较简单,包括:Undefined.Null.Boolean.Number和String,基本类型值就是简单的数据段:引用类型值可能由多个值构 ...
- 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取
版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 看完两篇,相信大家已经从开始的 ...
- JS中内嵌函数中this关键字的使用
this关键字的使用 在嵌套函数中:和变量不同,this关键字没有作用域的限制,在嵌套函数的内部使用this关键字可以分为以下两种情况: 1)如果嵌套函数作为方法调用,那么this为当前的上下文. 2 ...
- CentOS 根据命令查所在的包
在工作中经常会遇到想用某个命令机器没装却又不知道命令在哪个包(源码编译不再本文范围内),下面介绍个比较笨的方法可以帮助我们搞定这个问题. 说明:蓝色=命令名称 浅绿=命令参数 ...
- 【2】认识Bootstrap
作为当下最流行的前端开发框架Bootstrap,它可大大简化网站开发过程,从而深受广大开发者的喜欢,当然你去它的官网中文网站就能看到大大的小标定义:“简洁.直观.强悍.移动设备优先的前端开发框架,让w ...
- Python3.4 多线程
线程安全和全局解释器锁 Thread State and the Global Interpreter Lock 总结: 通过使用GIL后, Python多线程安全, 并且数据保持同步. Python ...
- Python 守护进程
import os import sys from time import sleep try: pid = os.fork() if pid > 0: sys.exit(0) # Exit p ...
- awk的使用备忘
[转]http://www.cnblogs.com/mydomain/archive/2012/09/24/2699467.html awk引用外部变量 一.用awk 有以下几种方法去调用变量: ...