我有一个程序来计算(致命百分比和罐的数量),但是我遇到的问题是输出与测试文件不匹配。我得到 [30266666, 8647618]
的输出,而测试文件有[302667, 864762]
。我是新来的C + +,我试图看着int
和double
但似乎没有工作。C++不匹配的结果
以下是我有:
#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;
}
这些数字是以“100”为因子的,这与“1/artificial_Sweetener”相同。我怀疑这跟它有关系。 – 2014-09-01 04:26:18
也许你应该使用那个“磅”的东西,或者修改那个“以磅为单位”的要求。 – 2014-09-01 06:38:36