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. 连接Oracle数据库的OracleHelper.cs

    using System; using System.Configuration; using System.Data; using System.Data.OracleClient; using S ...

  2. SQL技巧之行列转换

    比如:id     姓名      状态  1      刘德华    12      刘德华    23      周华健    04      吴彦祖    1 在access中,用一条sql查询 ...

  3. DIV当textarea使用,在聚焦的时候将光标移动到内容的末尾

    #### DIV当textarea使用,在聚焦的时候将光标移动到内容的末尾 #### <style type="text/css"> .test_box { width ...

  4. laravel项目拉取下来安装,node.js库安装

    1.拉取项目 2.切换分支 圈圈里面是版本 composer 安装laravel组件其他库 安装node.js安装包  npm set registry=https://registry.npm.ta ...

  5. ThinkPHP图片上传

    ThinkPHP是国内比较流行的轻量级的PHP框架,它在国内流行的一个最主要的因素在于它的说明文档非常健全完善,以及它源码内的注释都是中文的,方便于英语能力较差的程序员学习. 图片上传在网站里是很常用 ...

  6. 一个基于python的即时通信程序

    5月17日更新: 广播信息.用户列表.信息确认列表以及通信信息,从原来的用字符串存储改为使用字典来存储,使代码更清晰,更容易扩展,具体更改的格式如下: 广播信息(上线): { 'status': 信息 ...

  7. 【原创】一起学C++ 之->(箭头符号) ---------C++ primer plus(第6版)

    1.C++新手在指定结构成员时,不知道何时用.运算符,何时是用->运算符. 结论:如果结构标识符是结构名,则使用句点运算符:如果标识符是指向结构的指针,则使用箭头运算符. #include &l ...

  8. 深层次详解Exception

    所有的异常类都继承自System.Exception类,当异常产生时,CLR将创建该异常类的实例对象,将从最底层依次寻找合适的异常类型,同时若存在catch语句时将会选择最合适的语句进行处理. cat ...

  9. EXTJS 3.0 资料 控件之 combo 用法

    EXTJS combo 控件: 1.先定义store //年款 var comboData_ReleasYear = [ ['], ['], ['], ['] ]; 2.定义combo控件 { lay ...

  10. EXTJS 3.0 资料 控件之 FormPanel 插入button用法

    最近发现项目中FormPanel 里面需要 增加 button,的模块比较多,具体代码如下: var eastPanelForm_Dele = new Ext.form.FormPanel({ id: ...