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

这篇博客详细解答了C#编程练习题"Vasya和板",提供了从答案1到答案10的多种解决方案,适合C#初学者和进阶者提升算法与逻辑思维能力。
摘要由CSDN通过智能技术生成

Vasya和板【难度:1级】:

答案1:

using System;
using System.Linq;

public class EatingPlan
    {
        public static int CountClean(int b, int p, int[] dishes)
        { 
            int answer = Math.Max(0, dishes.Count(d => d == 1) - b);
           b = Math.Max(0, b - dishes.Count(d => d == 1));
           p += b;

           answer += Math.Max(0, dishes.Count(d => d == 2) - p);
           return answer;
        }
    }

答案2:

using System;
using System.Linq;

public class EatingPlan
    {
        public static int CountClean(int b, int p, int[] dishes)
        { 
            return Math.Max(dishes.Count(d => d == 1) - b + Math.Max(dishes.Count(d => d == 2) - p, 0), 0);
        }
    }

答案3:

using System;
using System.Linq;

public class EatingPlan
{
    public static int CountClean(int b, int p, int[] dishes)
    {
        // Throw out invalid input
        if (b < 1 || b > 1000) throw new ArgumentException("Value must be between 1 and 1000", "b");
        if (p < 1 || p > 1000) throw new ArgumentException("Value must be between 1 and 1000", "p");
        if (dishes == null || dishes.Length == 0) throw new ArgumentException("Array must contain at least 1 element", "dishes");
        if (dishes.Any(i => i != 1 &amp;&amp; i != 2)) throw new ArgumentException("Array values can only be 1 or 2", "dishes");
        
        var bowlsNeeded = dishes.Count(i => i == 1);
        var washBowls = Math.Abs(Math.Min(0, b - bowlsNeeded));
        var platesNeeded = dishes.Count(i => i == 2);
        var washPlates = Math.Abs(Math.Min(0, Math.Max(0, b - bowlsNeeded) + p - platesNeeded));
        return washBowls + washPlates;
    }
}

答案4:

using System.Linq;

public class EatingPlan {
    public static int CountClean( int b, int p, int[] dishes ) {
        var dishes1 = dishes.Count( d => d == 1 );
        var dishes2 = dishes.Count( d => d == 2 );
        var washCount = 0;
        foreach ( var dish in dishes ) {
            if ( dish == 1 ) {
                if ( b == 0 ) {
                    washCount += 1;
                }
                else {
                    b -= 1;
                }
                dishes1 -= 1;
            } else if ( dish == 2 ) {
                if ( b > dishes1 ) {
                    b -= 1;
                }
                else {
                    if ( p == 0 ) {
                        washCount += 1;
                    }
                    else {
                        p -= 1;
                    }
                }
                dishes2 -= 1;
            }

        }
        return washCount;
    }
}

答案5:

using System;
using System.Linq;

public class EatingPlan
    {
        public static int CountClean(int b, int p, int[] dishes)
        { 
            int wash = 0;
            foreach (int meal in dishes)
            {
              switch (meal)
              {
              case 1:
                b--;
                if (b < 0)
                  wash++;
                break;
              case 2:
              if (b == 0)
                {p--;
                if (p < 0)
                  wash++;
                }
              else
                b--;
              if (b < 0)
                wash++;
              break;
              default:
                break;
            }
            }
            return wash;
        }
    }

答案6:

using System;
   public class EatingPlan
    {
        public static int CountClean(int b, int p, int[] d)
        {
            int licznik = 0;
            for(int i = 0; i < d.Length; i++)
            {
                if(d[i] == 2) if (p > 0) p--; else if (b > 0) b--; else licznik++;
                if(d[i] == 1) if (b > 0) b--; else licznik++;
            }
            return licznik;
        }
    }

答案7:

using System;
public class EatingPlan
{
  public static int CountClean(int b, int p, int[] dishes)
  { 
    int wash_count = 0;
    int clean_bowl_cnt = b;
    int clean_plate_cnt = p;
    for (int i = 0; i < dishes.Length; i++)
    {
      if (dishes[i] == 1)
      {
        if (clean_bowl_cnt == 0)
        {
          wash_count += 1;
        }
        else
        {
          clean_bowl_cnt -= 1;
        }
      }
      else
      {
        if (clean_plate_cnt > 0)
        {
          clean_plate_cnt -= 1;
        }
        else
        {
          if (clean_bowl_cnt == 0)
          {
            wash_count += 1;
          }
          else
          {
            clean_bowl_cnt -= 1;
          }
        }
      }
    }
    return wash_count;
  }
}

答案8:

using System;
using System.Linq;

public class EatingPlan
    {
        public static int CountClean(int b, int p, int[] dishes)
        {
            var count = 0;
            foreach (var t in dishes)
            {
                if (t == 1)
                {
                    if (b == 0) count++;
                    else b--;
                }
                else
                {
                    if (p > 0) p--;
                    else if (b > 0) b--;
                    else count++;
                }
            }
            return count;
        }
    }

答案9:

using System;
using System.Linq;

public static class EatingPlan
{
    public static int CountClean(int bowls, int plates, int[] dishes)
    {
        var soups = dishes.Count(IsSoup);
        var schnitzel = dishes.Count(IsSchnitzel);
        var extra_bowls = Math.Max(0, bowls - soups);
        var bowls_to_wash = Math.Max(0, soups - bowls);
        var plates_to_wath = Math.Max(0, schnitzel - extra_bowls - plates);
        return bowls_to_wash + plates_to_wath;
    }

    public static bool IsSoup(this int dish) => dish == 1;
    public static bool IsSchnitzel(this int dish) => dish == 2;
}

答案10:

using System;
using System.Linq;

public class EatingPlan
    {
        public static int CountClean(int b, int p, int[] dishes)
        {
            int clean = 0;
            for (int i = 0; i < dishes.Length; i++)
            {
                int foodType = dishes[i];
                if (foodType == 1)
                {
                    if (b > 0)
                        b--;
                    else
                        clean++;
                }
                else
                {
                    if (p > 0)
                        p--;
                    else
                    {
                        if (b > 0)
                            b--;
                        else
                            clean++;
                    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值