C#练习题答案: 小马快递【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

小马快递【难度:1级】:

答案1:

using System.Linq;

class Kata
{
  public static int Riders(int[] s) => s.Aggregate(new []{0, 1}, (t, i) => t[0] + i > 100 ? new []{i, t[1] + 1} : new []{t[0] + i, t[1]})[1];
}

答案2:

class Kata
{
    public static int Riders(int[] stations)
    {
        int res = 1;
        int sum = 0;
        for(int i = 0; i < stations.Length; i++)
        {
          sum += stations[i];
          if (sum > 100)
          {
            res++;
            sum = 0;
            i--;
            continue;
          }
        }
        return res;
    }
}

答案3:

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


    class Kata
    {
        public static decimal distanseMax = 100;
        public static int Riders(int[] stations)
        {
            int count = 1;
            decimal d = 0;
            foreach (int v in stations)
            {
                d += v;
                if (d> distanseMax)
                {
                    count++;
                    d = v;
                }
            }
            return count;
        }
    }

答案4:

class Kata
{
    public static int Riders(int[] stations)
    {
        var riders = 1;
        var current = 0;
        foreach (var dist in stations)
        {
            current += dist;
            if (current > 100)
            {
                current = dist;
                riders++;
            }
        }
        
        return riders;
    }
}

答案5:

public class Kata {
    public static int Riders( int[] stations ) {
        var ridersCount = 1;
        if ( stations == null || stations.Length == 0 ) {
            return ridersCount;
        }
        var miles = 0;
        for ( int i = 0; i < stations.Length; i++ ) {
            if ( miles + stations [ i ] > 100 ) {
                miles = 0;
                ridersCount += 1;
            }
            miles += stations [ i ];
        }

        return ridersCount;
    }
}

答案6:

using System;
using System.Linq;

class Kata
{
    public static int Riders(int[] stations)
    {
            int rider = 100, riders_count = 1;
            for(int i = 0; i < stations.Length; i++)
            {
                if (rider - stations[i] < 0)
                {
                    rider = 100;
                    riders_count++;
                }
                rider -= stations[i];    
            }

            return riders_count;
    }
}

答案7:

class Kata
{
    public static int Riders(int[] stations)
    {
        int sum = 0;
        int riders = 1;
        
        for(int i = 0; i < stations.Length; i++)
        {
          sum += stations[i];
          if (sum > 100)
          {
            riders++;
            sum = 0;
            i--;
          }
        }
        
        return riders;
    }
}

答案8:

using System;

class Kata {
  public static int Riders(int[] stations) {
    int res = 1;
    int currDist = 0;
    foreach (int i in stations) {
      if (currDist + i > 100) {
        res++;
        currDist = 0;
      }
      currDist += i;
    }
    return res;
  }
}

答案9:

class Kata
{
    public static int Riders(int[] stations)
        {
            
            int riders = 1;
            int sum = 0;
            
            for(int i = 0; i < stations.Length; i++)
            {
                sum += stations[i];
                if (sum > 100)
                {
                    riders++;
                    sum = 0;
                    i--;
                }
            }
            
            return riders;
        }
}

答案10:

class Kata
{
    public static int Riders(int[] stations)
    {
        int riders = 1;
        int count = 0;
        
        foreach (var item in stations)
        {
          if (count + item > 100)
          {
            count = 0;
            riders += 1;
          }
          count += item;
        }
        return riders;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值