C#练习题答案: 积分总额【难度:0级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

积分总额【难度:0级】:

答案1:

using System.Linq;
using System;
public static class Kata {
    public static int TotalPoints(string[] games) {
        int total = 0;
        foreach (string game in games) {
          if (game[0] > game[2])
            total += 3;
          else if (game[0] == game[2])
            total += 1;            
        }
        return total;
    }
}​

答案2:

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

public static class Kata {
    public static int TotalPoints(string[] games) {
        return games.Select(x =>
            {
                var values = x.Split(':').Select(int.Parse);

                if (values.ElementAt(0) > values.ElementAt(1))
                {
                    return 3;
                }

                return values.ElementAt(0) < values.ElementAt(1)
                ? 0
                : 1;
            }).Sum(x => x);
    }
}​

答案3:

using System.Linq;

public static class Kata {
    public static int TotalPoints(string[] games) {
        return games.Select(x => CalculatePoints(x)).Sum();
    }
    
    public static int CalculatePoints(string game){
      var scores = game.Split(":");
      if(int.Parse(scores[0]) > int.Parse(scores[1])) return 3;
      if(int.Parse(scores[0]) < int.Parse(scores[1])) return 0;
      return 1;
    }
}​

答案4:

using System.Linq;

public static class Kata {
    public static int TotalPoints(string[] games) => 
        games.Select(e =>(x:e[0], y:e[2])).Select(e => e.x > e.y ? 3 : e.x == e.y ? 1 : 0).Sum();
}​

答案5:

using System;

public static class Kata {
    public static int TotalPoints(string[] games) 
    {
        var points = 0;
        foreach (var item in games)
        {
            points += (item[0] > item[2]) ? 3 : (item[0] == item[2]) ? 1 : 0;
        }
        return points;
    }
}​

答案6:

using System.Linq;

public static class Kata {
    public static int TotalPoints(string[] games) 
    {        
        return games.Where(x => x[0] > x[2]).Count()*3 + games.Where(y=>y[0]==y[2]).Count();
    }
}​

答案7:

using System.Linq;

public static class Kata {
    public static int toInt(char c){
      switch(c){
        case '0': return 0; break;
        case '1': return 1; break;
        case '2': return 2; break;
        case '3': return 3; break;
        case '4': return 4; break;
        case '5': return 5; break;
        case '6': return 6; break;
        case '7': return 7; break;
        case '8': return 8; break;
        default: return 9;
      }
    }
    public static int TotalPoints(string[] games) {
        int points = 0;
        foreach(string c in games){
          int x = toInt(c[0]);
          int y = toInt(c[2]);
          if(x>y) points+=3;
          else if(x==y)points+=1;
        }
        return points;
    }
}​

答案8:

using System;
using System.Linq;

public static class Kata 
{
    public static int TotalPoints(string[] games) 
    {
      return games.Sum(matchResult => InterpreteMatch(matchResult, GetScoreByChampionshipRules));
    }
    
    private static int InterpreteMatch(string match, Func<int, int, int> calculateScore)
    {      
      int scoreTeamA= (int)char.GetNumericValue(match[0]);
      int scoreTeamB = (int)char.GetNumericValue(match[2]); 
      return calculateScore(scoreTeamA, scoreTeamB);
    }
    
    private static int GetScoreByChampionshipRules(int a, int b)
    {
      if(a == b) return 1;
      if(a < b) return 0;
      return 3;
    }
}​

答案9:

using System.Linq;

public static class Kata {
    public static int TotalPoints(string[] games) {
        int result = 0;
        foreach(var score in games)
        {
          result += score[0] > score[2] ? 3 :
                    score[0] == score[2] ? 1 : 0;
        }
        
        return result;
    }
}​

答案10:

using System;
using System.Linq;

public static class Kata {
    public static int TotalPoints(string[] games) {
        int point = 0;
        
        for (int i = 0; i < games.Length; i++)
        {
            string[] scores = new string[2];
            scores = games[i].Split(":");

            int score1 = Convert.ToInt32(scores[0]);
            int score2 = Convert.ToInt32(scores[1]);

            if (score1 > score2)
            {
                point += 3;
            }
            else if (score1 == score2)
            {
                point++;
            }
        }

        return point;
    }
}​
上一篇:Java中创建String对象的两种方式


下一篇:基本数据结构和算法