如何获得for-each循环中所有项目的总成本?
<?xml version="1.0" encoding="utf-8" ?>
<Characters>
<Character ID="1" Name="Simmo">
<Inventory MaxSlots="20">
<Item ID="1" Name="Gold" Cost="1">
<Count>100</Count>
</Item>
<Item ID="1" Name="hat" Cost="10">
<Count>1</Count>
</Item>
<Item ID="2" Name="stick" Cost="15">
<Count>2</Count>
</Item>
</Inventory>
</Character>
</Characters>
Run Code Online (Sandbox Code Playgroud)
例如1*100 + 10*1 + 15*2 = 140
我未完成的 xsl解决方案:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>xsl file</title>
</head>
<body>
<ul>
<xsl:for-each select="/Characters/Character">
<li>
Character name: <xsl:value-of select="@Name"/>
<br/>
<xsl:for-each select="Inventory/Item">
<xsl:variable name="cos" select="@Cost"/>
<xsl:variable name="cou" select="Count"/>
<xsl:variable …Run Code Online (Sandbox Code Playgroud) 如何使用Linq打印出所有人及其宠物.我只想打印出有宠物的人.
首选结果如下:
Kate Bed:
Rex
Sally
Run Code Online (Sandbox Code Playgroud)
我的工作解决方案是:
class Program
{
static void Main(string[] args)
{
result();
}
static void result() {
var list = StaticGenator.getPersons().Where(x => x.Pets != null);
foreach (var person in list)
{
Console.WriteLine(person.Firstname + " " + person.Lastname + ":");
foreach(var pet in list){
Console.WriteLine(" " + pet.Pets);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的是:
Kate Bed:
system.collection.generic.list'1[MainLibrary.Pet]
system.collection.generic.list'1[MainLibrary.Pet]
Run Code Online (Sandbox Code Playgroud)
以下是了解我要问的代码:
数据保存在这里:
public static class StaticGenator
{
public static List<Person> getPersons()
{
List<Person> persons = new List<Person>();
persons.Add(new Person() …Run Code Online (Sandbox Code Playgroud) public class Sort {
enum Sides {
left, middle, right
};
public static void main(String[] param) {
Sides[] list = { Sides.middle, Sides.right, Sides.left, Sides.middle, Sides.left };
order(list);
System.out.print("Ordered: ");
for (Sides side : list) {
System.out.print(side.toString() + ", ");
}
}
public static void order(Sides[] sides) {
//toDo
}
}
Run Code Online (Sandbox Code Playgroud)
Arrays.sort(); 这不是解决方案.我知道有一种更快的算法算法.
当前输出:有序:中,右,左,中,左,
有序输出:有序:左,左,中,中,右,
值范围是固定的
我怎样才能按名字对动物进行分类?名称由String值表示.
data Animal = Cat String | Dog String | Fox String deriving (Show)
testAnimals = [(Dog "c"),(Fox "a"),(Cat "b")]
sortAnimalsByName :: [Animal] -> [Animal]
sortAnimalsByName animals = undefined
Run Code Online (Sandbox Code Playgroud)
查询:
sortAnimalsByName testAnimals
Run Code Online (Sandbox Code Playgroud)
应该返回:
[(Fox "a"),(Cat "b"),(Dog "c")]
Run Code Online (Sandbox Code Playgroud)
我认为,功能sort :: Ord a => [a] -> [a]从Data.List应使用,但如何?
我有以下具有指数复杂性的函数:
c :: Integer -> Integer
c n
| n <= 4 = n + 10
| otherwise = c (n-1) + 2 * (c (n-4))
Run Code Online (Sandbox Code Playgroud)
我正在努力使这个功能的复杂性变为线性.
c x 即使1000 <x <100000,也要终止