using System;using System.Linq;using System.Collections.Generic;publicstaticclassKata{publicstaticintSolve(List<char> xk,int n){
List<char> xs =newList<char>();
xs = xk.ToList();int res =0;for(int i =0; i < xs.Count(); i++){if(xs[i]=='D'){int check =0;for(int j = i - n; j < i; j++){if(j >-1&& xs[j]=='C'){
res++;
xs[j]='P';
check++;break;}}if(check ==0){for(int j = i +1; j <= i + n; j++){if(j < xs.Count()&& xs[j]=='C'){
res++;
xs[j]='P';break;}}}}}return res;}}
答案2:
using System;using System.Collections.Generic;using System.Linq;publicstaticclassKata{publicstaticintSolve(List<char> xs,int n){if(xs.Count <2|| n ==0)return0;var dog ='D';var cat ='C';var result =newList<int>();var cats =newList<int>();for(var i =0; i < xs.Count; i++){if(xs[i]== cat) cats.Add(i);}for(var i =0; i < xs.Count; i++){if(xs[i]== dog){var allowCats = cats.Where(x =>!result.Contains(x)&& Math.Abs(x - i)<= n).ToList();if(!allowCats.Any())continue;var catIndex = allowCats.First();
result.Add(catIndex);}}return result.Count;}}
答案3:
using System;using System.Collections.Generic;publicstaticclassKata{publicstaticintSolve(List<char> input,int n){//To indicate whether each cat has been caughtvar caughtCats =newbool[input.Count];var caughtCount =0;for(int i =0; i < input.Count; i ++){if(input[i]=='D'){var startCatchableRange = Math.Max(0, i - n);var endCatchableRange = Math.Min(input.Count -1, i + n);for(int j = startCatchableRange; j <= endCatchableRange; j++){if(input[j]=='C'&&!caughtCats[j]){
caughtCount++;
caughtCats[j]=true;break;}}}}return caughtCount;}}
答案4:
using System;using System.Collections.Generic;publicstaticclassKata{publicstaticintSolve(List<char> xs,int n){//To indicate whether each input has been usedvar usedXs =newbool[xs.Count];var count =0;for(int i =0; i < xs.Count -1; i ++){if(usedXs[i]){continue;}for(int j = i +1; j <= i + n; j++){if(j >= xs.Count){break;}elseif(usedXs[j]){continue;}elseif(xs[i]!= xs[j]){
count++;
usedXs[i]=true;
usedXs[j]=true;break;}}}return count;}}
答案5:
using System;using System.Collections.Generic;publicstaticclassKata{publicstaticintSolve(List<char> input,int n){
input =newList<char>(input);var count =0;for(int i =0; i < input.Count; i++){for(int c =1; c <= n; c++){if((i + c)>= input.Count)break;if(input[i]=='C'){if(input[i + c]=='D'){
input[i + c]='X';
count++;break;}}elseif(input[i]=='D'){if(input[i + c]=='C'){
input[i + c]='X';
count++;break;}}}}return count;}}
答案6:
using System;using System.Collections.Generic;publicstaticclassKata{publicstaticintSolve(List<char> xs,int n){var used_dogs =newHashSet<int>();for(int i =0; i < xs.Count; i++){if(xs[i]=='D')continue;for(int k =(i - n)<0?0: i - n; k <= i + n && k < xs.Count; k++){if(used_dogs.Contains(k))continue;if(xs[k]=='D'){
used_dogs.Add(k);break;}}}return used_dogs.Count;}}
答案7:
using System;using System.Collections.Generic;using System.Linq;publicstaticclassKata{publicstaticintSolve(List<char> xs,int n){var dogs =newList<Dog>();var cats =newList<Cat>();var catsCaught =0;var index =0;//Get positionsforeach(var item in xs){if(item =='D'){
dogs.Add(newDog{ Position = index });}if( item =='C'){
cats.Add(newCat{ Position = index });}
index++;}//Find catsforeach(var dog in dogs){var cat = cats.Where(w =>!w.Caught).FirstOrDefault(c =>(n >0)&&(Math.Abs(dog.Position - c.Position)<= n));if(cat !=null){
cat.Caught =true;
catsCaught++;}}return catsCaught;}publicclassDog{publicint Position;}publicclassCat{publicint Position;publicbool Caught;}}
答案8:
using System;using System.Collections.Generic;using System.Linq;publicstaticclassKata{publicclassAnimalUsed{publicchar Animal {get;set;}publicbool Used {get;set;}publicAnimalUsed(char animal,bool used =false){
Animal = animal;
Used = used;}}publicstaticintSolve(List<char> xs,int n){//a cat cannot be caught more than once//a dog cannot catch more than one cat//iterate through each can and see if it can be caughtint caught =0;
List<AnimalUsed> list =newList<AnimalUsed>();foreach(char c in xs){
list.Add(newAnimalUsed(c));}for(int i =0; i < list.Count();++i){//get elements at index i +- n, is a cat and hasn't been caughtif(list[i].Animal =='D'){try{int matchedindex = list.Select((animal, index)=>new{ index, animal }).Where(x => x.index >= i - n
&& x.index <= i + n
&& x.animal.Animal =='C'&&!x.animal.Used).First().index;//mark both as used
list[i].Used =true;
list[matchedindex].Used =true;++caught;}catch(Exception e)//none found{}}}return caught;}}
答案9:
using System;using System.Collections.Generic;using System.Linq;publicstaticclassKata{publicstaticintSolve(List<char> xs,int n){//convert to the helper class
Animal[] newField = xs.AsParallel().Select(x =>newAnimal(x)).ToArray();for(int a =0; a < newField.Length; a++){if(newField[a].IsUsed){continue;}//count and correct range edges for the helper functionintfrom=(a - n <0)?0: a - n;int to =(a + n >= newField.Length)? newField.Length -1: a + n;Check(newField,from, to, a);}return newField.Count(x => x.IsCat && x.IsUsed);}//checks the actual range of animals from our current animal into negative and positive directionsprivatestaticvoidCheck(Animal[] field,intfrom,int to,int it){//check the given rangefor(int i =from; i < to+1; i++){if(i == it){continue;}//not already catched a cat or has been catched by a dog and//the ivestigated pair of animals are a dog and a catif(!field[i].IsUsed && field[i].IsCat ==!field[it].IsCat){
field[i].IsUsed =true;
field[it].IsUsed =true;break;}}}//helper inner class to follow the already checked elements and the result of checkclassAnimal{publicAnimal(char mark){
IsCat = mark.Equals('C');if(!IsCat &&!mark.Equals('D')){thrownewException("Neither Cat or Dog has been given!");}
IsUsed =false;}publicbool IsUsed {get;set;}publicbool IsCat {get;set;}}}
答案10:
using System;using System.Collections.Generic;using System.Linq;publicstaticclassKata{publicstaticintSolve(List<char> xs,int n){//convert to the helper class
Animal[] newField = xs.AsParallel().Select(x =>newAnimal(x)).ToArray();for(int a =0; a < newField.Length; a++){if(newField[a].IsUsed){continue;}//count and correct range edges for the helper functionintfrom=(a - n <0)?0: a - n;int to =(a + n >= newField.Length)? newField.Length -1: a + n;Check(newField,from, to, a);}return newField.Count(x => x.IsCat && x.IsUsed);}//checks the actual range of animals from our current animal into negative and positive directionsprivatestaticvoidCheck(Animal[] field,intfrom,int to,int it){//check the given rangefor(int i =from; i < to+1; i++){if(i == it){continue;}//not already catched a cat or has been catched by a dog and//the ivestigated pair of animals are a dog and a catif(!field[i].IsUsed && field[i].IsCat ==!field[it].IsCat){
field[i].IsUsed =true;
field[it].IsUsed =true;break;}}}//helper inner class to follow the already checked elements and the result of checkclassAnimal{publicAnimal(char mark){
IsCat = mark.Equals('C');if(!IsCat &&!mark.Equals('D')){thrownewException("What is this???");}
IsUsed =false;}publicbool IsUsed {get;set;}publicbool IsCat {get;set;}}}