首页 > 解决方案 > C# 算法代码未通过测试用例

问题描述

挑战是根据给定的输入计算一顿饭的总成本:

  1. 餐费(不含税或小费)(双份)
  2. 小费百分比(以 int 形式给出)
  3. 税收百分比(以整数形式给出)

注意:默认情况下,这些数据类型必须保持这种方式。我不能将他们的初始声明更改doubleint

给定的步骤如下:

  1. 读取 3 个值的输入
  2. 计算提示使用tip = mealCost x (tipPercent / 100)
  3. 使用计算税tax = mealCost x (taxPercent / 100)
  4. mealCost通过添加、tip和来计算总膳食成本tax
  5. 将最终答案四舍五入并打印出来(如“总餐费是totalCost美元。”)

所以我的程序是这样的:

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

    // Complete the solve function below.
    static void solve(double meal_cost, int tip_percent, int tax_percent) {
        double tip = meal_cost * (tip_percent / 100.0);
        double tax = meal_cost * (tax_percent / 100.0);

        double totalCost = (meal_cost + tip + tax);

        Console.WriteLine("The total meal cost is {0} dollars", Convert.ToInt32(totalCost));
    }

    static void Main(string[] args) {
        double meal_cost = Convert.ToDouble(Console.ReadLine());

        int tip_percent = Convert.ToInt32(Console.ReadLine());

        int tax_percent = Convert.ToInt32(Console.ReadLine());

        solve(meal_cost, tip_percent, tax_percent);
    }
}

然而,尽管我的输出是相同的(甚至在四舍五入之前),HackerRank 仍然将我的解决方案标识为在它的测试用例上“失败”,而不是我的自定义测试用例。对此有什么解释吗?

注意:HackerRank 提供的公式实际上是 (Meal Cost) x Tax Percent / 100。我只是写下了实现

标签: c#algorithm

解决方案


你犯了一个轻微的错字。你dollars在应该使用的时候使用dollars.

在对您的代码进行更改后,它然后通过https://www.hackerrank.com/challenges/30-operators/problem


推荐阅读