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是危险的. 现已知 ...
随机推荐
- 九度OJ 1386 旋转数组的最小数字 【算法】
题目地址:http://ac.jobdu.com/problem.php?pid=1386 题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋 ...
- slabs.c
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Slabs memory allo ...
- jQuery—一些常见方法(3)【width(),innerWidth(),outerWidth()】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux性能监控的几个工具(转)
转载于:http://blog.csdn.net/tianlesoftware/article/details/6198780 Linux系能监控主要涉及系统4个方面资源的监控: CPU Memory ...
- 利用Keepalived+mysql构建高可用MySQL双主自动切转
转载:http://www.it300.com/index.php/article-15266.html 关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储, ...
- 使用phpize安装php模块
1.下载包 2./usr/local/php/bin/phpize 3../configure --enable-soap --with-php-config=/usr/local/php/bin/ ...
- 聊天工具实现winform端实现
最近在找能够实现客户端点对点聊天的技术,通过github我发现了一个项目,它能够支持webscoket通讯,服务端是由c#socket完成. 我要的是winform端的通信,所以在他的基础上,增加了桌 ...
- C语言基础知识-循环结构
用while打印出1~100之间7的倍数 int i = 1; while循环是当条件表达式的结果为真时,执行大括号里面的循环体,重复执行直到条件表达式的结果为假时结束循环. w ...
- WPF从入门到放弃系列第一章 初识WPF
什么是WPF WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET Framework 3.0的一部分.它提供 ...
- Random.Next获取随即数
Random random = new Random(); random.Next()--------------返回非负的一个随机数. random.Next(int maxValue)----- ...