C#练习题答案: 渐渐地打折奶价【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

渐渐地打折奶价【难度:1级】:

答案1:

using System;
public class Kata
{
  const double discount = 0.05;
  const double litreReductionPoint = 2;
  
  public static double MilkPrice(double litres, double pricePerLitre)
  {
    var maximumDiscount = (Math.Floor(litres / litreReductionPoint) * discount);
    var avgDiscount = (discount + maximumDiscount) / litreReductionPoint;
    
    var extraLitres = litres % litreReductionPoint;
    var mainLitres = litres - extraLitres;
    
    var unroundedPrice = (mainLitres * (pricePerLitre - avgDiscount)) + (extraLitres * (pricePerLitre - maximumDiscount));
    
    return Math.Round(unroundedPrice, 2);
  }
}

答案2:

using System;

public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
    double sum = 0;

    while (litres >= 2)
    {
      pricePerLitre -= 0.05;
      sum += 2 * pricePerLitre;
      litres -= 2;
    }

    if (litres > 0)
      sum += litres * pricePerLitre;

    return Math.Round(sum, 2);
  }
}

答案3:

using System;

public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
    double sum = 0;
    double price = pricePerLitre;
    
    while(litres >= 2){
      pricePerLitre -= 0.05;
      sum += pricePerLitre * 2;
      litres -= 2;
    }
    sum += litres * pricePerLitre;
    return Math.Round(sum, 2);
  }
}

答案4:

using System;

public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
    double sum = 0;
  
    if(litres>=2)
    {
      for(var i=0;i<=litres;i+=2)
      {
        pricePerLitre -= 0.05;
    
        var litresCalc = 2;
        sum += litresCalc * pricePerLitre;
    
        litres--;
        litres--;
      }
    }
  
    sum += litres * pricePerLitre;
  
    return Math.Round(sum, 2);
  }
}

答案5:

using System;
public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
      double total = 0;
      double discount = 0.05;
      double price = pricePerLitre;

        while(litres >= 2)
        {
          price -= discount;
          total += 2 * price;
          litres -=2;
        }

        //remining portion
        total += litres * price;
  
    return Math.Round( total, 2);;
  }
}

答案6:

using System;

public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
    double tot = 0;
  
    int floored = (int)Math.Floor(litres);
    
    int remainder;
    int result = Math.DivRem(floored, 2, out remainder);
    
    double remaining = litres-floored+remainder;
    
    for(int i = 1; i <= result; i++) 
    {
      pricePerLitre-=0.05;
      tot+=(2*pricePerLitre);
    }
    
    tot+=(remaining*pricePerLitre);
    return Math.Round(tot, 2);
  }
}

答案7:

  using System;
  public class Kata
    {
        public static double MilkPrice(double l, double p)
        {
            if (l < 2) return Math.Round(l * p, 2);
            p -= 0.05;
            double check = 0;
            double res = 0;
            while (check <= l - 2)
            {
                res = res + 2 * p;
                check = check + 2;
                p = p - 0.05;
            }
            res = res + (l - check) * (p + 0.05);
                 return Math.Round(res, 2);
        }
        }

答案8:

using System;
public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
    double price = 0;
    while(litres > 0) {
      if(litres - 2 >= 0)
      {
        litres -= 2;
        pricePerLitre -= 0.05;
        price += 2 * pricePerLitre;
      }
      else
      {
        price += litres * pricePerLitre;
        break;
      }
    }
    return Math.Round(price, 2);
  }
}

答案9:

public class Kata
{
  public static double MilkPrice(double liters, double unitPrice)
  {
    double remaining = liters;
    double price = 0;
    const double priceStep = 0.05;
    for (;remaining >= 2; remaining -= 2)
    {
      unitPrice -= priceStep; // buy enough and we start paying you!
      price += 2 * unitPrice;
    }
    
    price += remaining * unitPrice;
    
    return System.Math.Round(price, 2);
  }
}

答案10:

using System;

public class Kata
{
  public static double MilkPrice(double litres, double pricePerLitre)
  {
      Console.WriteLine(litres + " ### " + pricePerLitre);
      int count = (int)litres / 2;
      litres -= count * 2;
      double ans = 0;
      for (int i = 1; i <= count; i++)
      {
          pricePerLitre -= 0.05;
          ans += pricePerLitre * 2;
      }
      return litres == 0 ? Math.Round(ans, 2) : Math.Round(ans + pricePerLitre * litres, 2);
  }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值