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. 01顺序栈_Stack---(栈与队列)

    #include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...

  2. IOS上iframe的滚动条失效的解决办法

    #iframe-wrap { position: fixed; top: 100px; bottom: 0px; left: 0px; right: 0px; -webkit-overflow-scr ...

  3. 学习C++ Primer 的个人理解(六)

    第四章和第五章没什么特别的.基本上就是书本上的字面意思,也没什么需要注意的细节.直接记录第六章. 本章介绍了函数,其实也没什么特别的.但有几个重点 1.形参的类型决定了形参和实参的交互方式.形参是引用 ...

  4. <<深入Java虚拟机>>-第二章-Java内存区域-学习笔记

    Java运行时内存区域 Java虚拟机在运行Java程序的时候会将它所管理的内存区域划分为多个不同的区域.每个区域都有自己的用途,创建以及销毁的时间.有的随着虚拟机的启动而存在,有的则是依赖用户线程来 ...

  5. Linux C 程序 空语句-gcc编译命令(SIX)

    C语言语句1.空语句:当一个if或者while判断部分能完成工作,空语句可以用来表示if或者while内的空循环体 if(a == b ); 2.gcc编译器 //程序的编译过程: // 预处理:pr ...

  6. C# IO操作磁盘上的txt

    using System.IO; //写入并导出到磁盘 StreamWriter sw = new StreamWriter(@"H:\text.txt"); sw.WriteLi ...

  7. FBWF和EWF的对比

    最近在公司接触了嵌入式的wes7系统,wes7和wes2009是组件化的windows系统,除具有最新版的windows功能之外还具有适用于嵌入式系统的一些嵌入式功能,如例如EWF,FBWF. FBW ...

  8. C#之Attribute(特性)

    本文主要复习下基础知识: 1.C#系统自带的特性: 建立一个控制台项目取名为AttributeTest: 我们添加了一个系统自带的Attribute叫Condition,这个特性表示在程序的DEBUG ...

  9. TIBCO ActiveMatrix BPM 生成daa包脚本

    Ant 配置: <?xml version="1.0" encoding="UTF-8" ?> <project name="pro ...

  10. const用法

    一.const作用 二.const用法 1.修饰一般常量   修饰符const可以用在类型说明符前,也可以用在类型说明符后. 例如: ; ; 2.修饰常数组  修饰符const可以用在类型说明符前,也 ...