Counting sheep...

Description:

Consider an array of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).

For example,

[true,  true,  true,  false,
true, true, true, true ,
true, false, true, false,
true, false, false, true ,
true, true, true, true ,
false, false, true, true]
using System;

public static class Kata
{
public static int CountSheeps(bool[] sheeps)
{
int count = ;
for(int i = ; i < sheeps.Length; i++) {
if (sheeps[i]) count++;
}
return count;
}
}


比较好的方法,是使用Linq
using System;
using System.Linq; public static class Kata
{
public static int CountSheeps(bool[] sheeps)
{
return sheeps.Count(s => s);
}
}

Array类的扩展方法Count

public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

 

http://www.codeproject.com/Articles/383749/How-does-it-work-in-Csharp-Part-Csharp-LINQ-in-d

In .NET any data structure which is derived from the IEnumerable<T> interface of the System.Collections.Generic namespace of the mscorlib.dll (this assembly is located in C:\Windows\Microsoft.NET\Frameworkv4.0.30319 but depends on the installation of the VS) assembly is able to access all the extension methods defined in the Enumerable class of the System.Linq namespace of the System.Core.dll assembly in the .NET Framework (see more about LINQ). This Enumerable class is a static non inheritable class defined in the System.Linq namespace of the System.Core.dll assembly(C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll but depends on the installation of VS). The definition of the Enumerbale class is as below:



Counting sheep...的更多相关文章

  1. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  2. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  3. HDU 2952 Counting Sheep(DFS)

    题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...

  4. HDU2952:Counting Sheep(DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  5. Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  6. ACM HDU-2952 Counting Sheep

    Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. 2001. Counting Sheep

      After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...

  8. 【Kata Daily 190927】Counting sheep...(数绵羊)

    题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...

  9. hdu 2952 Counting Sheep

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...

随机推荐

  1. WinForms 小型HTML服务器

    最近教学,使用到了Apache和IIS,闲着无聊,有种想自己写个小服务器的冲动. 在网上找了半天的资料,最后终于搞定了,测试可以访问.效果图如下: 因为只是处理简单的请求,然后返回请求的页面,所以没有 ...

  2. bootstrap响应式网页设计的9条基本原则!

    bootstrap响应式网页设计是针对多屏幕问题的一个很好的解决方案,但从印刷的视角来看有点困难.没有固定的页面尺寸,没有毫米或英寸,没有任何的物理限制,无从下手. 为了desktop和mobile单 ...

  3. php学习日志(2)-php变量

    变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变量(例如:$x=$y+$z).变量的定义主要有以下规则: 变量以$开始,后面跟着变量的名称: 变量名 ...

  4. apache配置网站目录的读写权限

    分享下apache中配置网站目录文件权限的方法. 假设http服务器运行用户和用户组是www,网站用户为centos,网站根目录是/home/centos/web. 操作方法与步骤:1,首先,设定网站 ...

  5. phpmailer 实现发送邮件

    在注册的时候,常常会用到邮件验证,一直想弄明白这是怎么实现的,记得2年前曾经试过这个问题,没有实现,今天困到不行的时候开始决定搞明白这个,然后,然后就出来了. <?php require(&qu ...

  6. [旧博客]QQ旋风加速漏洞

    漏洞是这样的,用开通QQ会员的账号登录QQ旋风,添加要下载的任务,启动加速后,注销,登录lixian.qq.com 删除刚才添加的离线任务,这时QQ旋风还是在加速那个任务.而你又可以登录QQ旋风添加其 ...

  7. vsftpd本地用户登录密码错误

    今天发现自己虚拟机的vsftp使用本地用户名无法登陆,于是重新配置,但配置了很多次都没成功,一直显示 530 Login incorrect. Login failed 尝试了网上很多修改PAM的方案 ...

  8. oracle 绿色版本 instantclient 使用说明

    1,将instantclient直接放到D盘根目录 2,注册表修改 3,点击工具>>首选项, 在Oracle主目录名中输入“D:\instantclient_10_2\”,在OCI库中输入 ...

  9. RAC环境下SCAN IP可以PING通,1521端口也可以TELNET,但是无法建立数据库连接

    昨天用户请求帮助处理一个问题:有个厂家需要连某个业务系统的数据库,网络上已经开通了权限,SCAN IP可以PING通,测试TELNET 1521端口也是正常.但是想通过SQLPLUS连接,总是会提示连 ...

  10. go语言使用protobuf

    网上为什么充斥着大量几乎一模一样而且不正确的教程??? 妈的打开一个关于golang和protobuf的教程,无非都是protobuf多么多么牛逼,xml多么多么傻逼,然后就是怎么安装protobuf ...