天天看点

C#练习题答案: 刽子手【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战答案1:答案2:答案3:答案4:答案5:答案6:答案7:答案8:答案9:答案10:答案11:答案12:

刽子手【难度:2级】:

答案1:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {    
        int chances = 6;
        List<char> set = new List<char>();
        
        foreach(var letter in letters)
        {
          if(!word.Contains(letter))
          {
            chances--;
          }
          else
          {
            set.Add(letter);
          }
          
          if(chances < 0)
          {        
            return false;
          }
          
          if(set.Distinct().OrderBy(o => o).SequenceEqual(word.Distinct().OrderBy(o => o)))
          {
            return true;
          }
        }
        
        return false;        
    }
}​

           

答案2:

public class Kata
{
    public bool Hangman(string w, char[] l)
    {
        int m=0; for (int i=0; i<l.Length; i++) if (w.IndexOf(l[i])>=0) { if ((w=w.Replace(l[i]+"","")).Length==0) return true; } else if(++m>6) return false;
        return false;
    }
}​

           

答案3:

using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {
        int counter = 0, errors = 0;        
        word = new string(word.ToCharArray().Distinct().ToArray());
        
        for (int i = 0; i<letters.Length; i++)
          if (word.Contains(letters[i]))
            counter++;
          else
            errors++;
            
        if (errors > 7) return false;
        return (counter == word.Length) ? true : false;
    }
}​

           

答案4:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {
        int miss = 0;
        int index = 0;
        foreach (var el in letters)
        {
            if (word.IndexOf(el) == -1)
            {
                Console.WriteLine(el);
                miss++;
                if (miss == 7) return false;
            }
            else
            {
                index++;
                if (index == word.Length) return true;
            }
        }
        foreach (var el in word)
        {
            if (Array.IndexOf(letters, el) == -1) return false;
        }
        return true;
    }
}​

           

答案5:

using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {
        int mistakesCounter = 0;
        int hits = 0;

        foreach(var l in letters)
        {
            if(word.Contains(l))
            {
                hits += word.Count(c => c == l);
            }
            else
            {
                mistakesCounter++;
            }

            if(mistakesCounter == 7)
            {
                return false;
            }

            if(hits == word.Length)
            {
                return true;
            }
        }

        return false;
    }
}​

           

答案6:

using System.Text.RegularExpressions;

public class Kata
{
  public bool Hangman(string word, char[] letters)
  {
    var error = 0;
    foreach(var c in letters)
    {
      if (word.Contains(c.ToString()))
      {
        word = new Regex(c.ToString()).Replace(word, "");
      }
      else if (++error == 7)
      {
        return false;
      }
      if (word.Length == 0)
      {
        return true;
      }
    }
    
    return false;
  }
}​

           

答案7:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {
        var mistakes = 0;
        foreach (var letter in letters)
        {
            if (word.Contains(letter.ToString()))
            {
                word = word.Replace(letter.ToString(), "");
                if (string.Equals(word, string.Empty)) return true;
            }
            else
            {
                mistakes++;
                if (mistakes > 6) return false;
            }
        }
        return false;
    }
}​

           

答案8:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {
            letters = letters.Distinct().ToArray();
            word = string.Join("", word.ToCharArray().Distinct().ToList());
            int mistakeCounter = 0;
            string completedWord = "";
            List<bool> a = letters.Select((p, i) =>
            {
                if (completedWord.Length != word.Length &amp;&amp; !word.Contains(p))
                {
                    mistakeCounter++;
                }
                if (word.Contains(p))
                {
                    completedWord = completedWord + p;
                }
                return true;
            }).ToList();
            return (completedWord.Length == word.Length) &amp;&amp; mistakeCounter<=6 ;
    }
}​

           

答案9:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {
        int lives = 7;
        foreach (var c in letters)
        {
            if (word.Contains(c))
            {
                word = word.Replace(c.ToString(), "");
            }
            else
            {
                lives--;
            }

            if (lives == 0)
            {
                return false;
            }
            else if (word.Length == 0)
            {
                return true;
            }
        }

        return false;
    }
}​

           

答案10:

public class Kata
{
    public bool Hangman(string w, char[] l)
    {
        for (int i=0; i<l.Length; i++) if ((w=w.Replace(l[i]+"","")).Length==0) return true; //works, but not the sense of the game?!
        return false;
    }
}​

           

答案11:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string w, char[] l)
    {
        return w.All(x=>l.Contains(x));
    }
}​

           

答案12:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public bool Hangman(string word, char[] letters)
    {        
        int errors = 0;
        int curr_pos = 0;
        int i = 0;
        while (i < word.Length){
          char c = word[i];
          if (curr_pos > letters.Length - 1)
            break;
          if (c == letters[curr_pos]){
            curr_pos++;
            i++;
          }
          else{
            errors++;          
            curr_pos++;
          }
        }
        return errors < 7; 
    }
}​

           

继续阅读