c# - C# 算法代码未通过测试用例
问题描述
挑战是根据给定的输入计算一顿饭的总成本:
- 餐费(不含税或小费)(双份)
- 小费百分比(以 int 形式给出)
- 税收百分比(以整数形式给出)
注意:默认情况下,这些数据类型必须保持这种方式。我不能将他们的初始声明更改
double
为int
给定的步骤如下:
- 读取 3 个值的输入
- 计算提示使用
tip = mealCost x (tipPercent / 100)
- 使用计算税
tax = mealCost x (taxPercent / 100)
mealCost
通过添加、tip
和来计算总膳食成本tax
。- 将最终答案四舍五入并打印出来(如“总餐费是
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。我只是写下了实现
解决方案
你犯了一个轻微的错字。你dollars
在应该使用的时候使用dollars.
。
在对您的代码进行更改后,它然后通过https://www.hackerrank.com/challenges/30-operators/problem。
推荐阅读
- mysql - 没有在 mysql 中修整的 LPAD
- sql - VOLTDB ERROR: SQL ERROR 执行 SQL 时使用了超过 100 MB 的临时表内存
- django - 创建对象时分配给外键字段时的值错误
- python-3.x - Selenium Web 驱动程序 - 尝试单击具有 unselectable="on" 的保存按钮。有没有办法解决这个问题?
- laravel - 从有条件的数据库中获取数据并在视图中显示
- checkstyle - checkstyle 不允许 SuppressWarnings 注释,除非附近有注释
- python-3.x - 使用 Pytest 在导入期间模拟类
- javascript - 在 npm 模块中导入不同的嵌套包
- python - 在 matplotlib 图中填充指定区域
- css - R rmarkdown ioslides - 如何包含“谢谢”幻灯片?