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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值