我有一个计算程序(致命百分比和罐数量),但我遇到的问题是输出与测试文件不匹配。我得到了一个输出
[30266666, 8647618]
,而测试文件有[302667, 864762]
。我是c ++的新手,我尝试查看int
和double
,但似乎没有任何结果。
这就是我所拥有的:
#include <iostream>
#include <string>
using namespace std;
#include "cs150check.h"
//// Include your own header files here
//// Define any file-wide constants here
/**
* A government research lab has concluded that an artificial sweetener commonly used in diet soda will cause death in laboratory mice.
* A friend of yours is desperate to lose weight but cannot give up soda.
* Your friend wants to know how much diet soda it is possible to drink without dying as a result. Write a program to supply the answer.
*
* Input: The amount of artificial sweetener need to kill a mouse, the weight of the mouse, and the weight of the dieter.
* Output: Amount of Lethal and Soda Cans
*
* @param cin the standard input stream.
* @param cout the standard output stream.
* @return 0 for success.
*/
int run(istream& cin, ostream& cout)
{
// Weight of the mouse in grams: 15
//Lethal dose for the mouse(in grams) : 100
//Desired weight of the dieter(in pounds) : 100
//Lethal dose in grams, cans is[302667, 864762]
const double artificial_Sweetener = .01;
const int WEIGHT_OF_SODA = 350;
const int pound = 454;
int numLethalDose;
int numSodaCan;
int weightMouse;
int lethal;
int desiredWeight;
cout << "Weight of the mouse in grams: ";
cin >> weightMouse;
cout << "Lethal dose for the mouse (in grams): ";
cin >> lethal;
cout << "Desired weight of the dieter (in pounds): ";
cin >> desiredWeight;
//Math
numLethalDose = (lethal * (desiredWeight * pound)) / (weightMouse * artificial_Sweetener);
numSodaCan = (1 / ((WEIGHT_OF_SODA * artificial_Sweetener) / (numLethalDose)));
cout << "Lethl dose in grams, cans is: " << "[" << numLethalDose << ", " << numSodaCan << "]" << endl;
return 0;
}
答案 0 :(得分:1)
使用分部的两条线都给我一个警告C4244:
警告C4244:'=':从'const double'转换为'int',可能会丢失数据
这会向您提示您正在混合整数和浮点数据。权重和转换因子等数量肯定应为double
而不是int
。在这种情况下,即使numLethalDose
和numSodaCan
也应该是double
,因为例如,它可能需要4.5个汽水罐来杀死你。
const double artificial_Sweetener = .01;
const double WEIGHT_OF_SODA = 350.0;
const double pound = 454.0;
double numLethalDose;
double numSodaCan;
double weightMouse;
double lethal;
double desiredWeight;
注意使用小数零。从技术上讲,它没有做任何事情,但它使代码更容易理解。
尽管如此,结果仍然是[30266666.667 8647619.048]
给定的输入。结论是你的方程本身是错误的,或者你的测试文件是错误的。我有点太累了,无法找出给定问题的正确数学。
编辑:问题确实存在于你的数学中。你应该在纸上找出数学,然后用注释来表达你在做什么。此外,使用更明确的变量名称。这使得它更容易理解,结果很简单。
// Weight of the mouse in grams: 15
//Lethal dose for the mouse(in grams) : 100
//Desired weight of the dieter(in pounds) : 100
//Lethal dose in grams, cans is[302667, 864762]
//
const double artificial_Sweetener = .01;
const double WEIGHT_OF_SODA = 350.0;
const double POUND_PER_GRAM = 454.0;
double mouseWeight;
double humanWeight;
double mouseLethalDose;
double humanLethalDose;
double numSodaCan;
cout << "Weight of the mouse in grams: ";
cin >> mouseWeight;
cout << "Lethal dose for the mouse (in grams): ";
cin >> mouseLethalDose;
cout << "Desired weight of the dieter (in pounds): ";
cin >> humanWeight;
// Part 1: Figure out the lethal dose for our human
//
// mouseLethalDose / mouseWeight = humanLethalDose / humanWeight
//
// humanLethalDose = humanWeight * (mouseLethalDose / mouseWeight)
humanLethalDose = humanWeight*POUND_PER_GRAM * (mouseLethalDose / mouseWeight);
// Part 2: Figure out how much cans we need to have "humanLethalDose" grams of sweetener
// sweetenerPerCan = artificial_Sweetener * WEIGHT_OF_SODA
// totalSweetener = numSodaCans * sweetenerPerCan
// numSodaCans = sweetenerPerCan / totalSweetener
numSodaCan = humanLethalDose / (artificial_Sweetener * WEIGHT_OF_SODA);
printf("[%.3f %.3f]\n", humanLethalDose, numSodaCan);