Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 三点解决方案代码Hunt 2.06和2.07_C#_Linq - Fatal编程技术网

C# 三点解决方案代码Hunt 2.06和2.07

C# 三点解决方案代码Hunt 2.06和2.07,c#,linq,C#,Linq,[包含扰流板] 嘿,我在玩C。大多数问题都相当简单和直接。但这件事快把我逼疯了。问题是计算a(问题2.06)或给定字符(问题2.07)的出现次数 显而易见的解决方案只有两点 int counter = 0; foreach (var c in str) { if (c == 'a') counter ++; } return counter; return s.Count(c=>c=='a'); 使用linq的较短版本也仅给出两点 int counter = 0; forea

[包含扰流板]

嘿,我在玩C。大多数问题都相当简单和直接。但这件事快把我逼疯了。问题是计算a(问题2.06)或给定字符(问题2.07)的出现次数

显而易见的解决方案只有两点

int counter = 0;
foreach (var c in str)
{
     if (c == 'a') counter ++;
}
return counter;
return s.Count(c=>c=='a');
使用linq的较短版本也仅给出两点

int counter = 0;
foreach (var c in str)
{
     if (c == 'a') counter ++;
}
return counter;
return s.Count(c=>c=='a');
对不同的解决方法有什么想法吗。对于这个问题,我想不出任何更简单或更简短的解决办法

对于那些不知道密码搜寻的人。这是一个编程游戏,它给出了给定问题的解决方案有多优雅的观点。最简单和最好的解决方案得3分。 谢谢你,祝你今天愉快;)

编辑: 抨击者回答:

s.Split('a').Length - 1;
也仅给出两点

也许吧

s.Split('a').Length - 1;

这是一个三点解决方案

使用系统;
公共课程{
公共静态整型拼图(字符串s){
返回s.Length-s.Replace(“a”和“).Length;
}
}
适用于2.07

一,

二,


尝试使用
string。将
string.Length