2014-09-01 114 views
0

我有一个程序来计算(致命百分比和罐的数量),但是我遇到的问题是输出与测试文件不匹配。我得到 [30266666, 8647618]的输出,而测试文件有[302667, 864762]。我是新来的C + +,我试图看着intdouble但似乎没有工作。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; 
} 
+2

这些数字是以“100”为因子的,这与“1/artificial_Sweetener”相同。我怀疑这跟它有关系。 – 2014-09-01 04:26:18

+0

也许你应该使用那个“磅”的东西,或者修改那个“以磅为单位”的要求。 – 2014-09-01 06:38:36

回答

1

使用一个师都行给我一个警告C4244:

警告C4244: '=':转换从 '常量双' 到' int',可能丢失数据

这给你一个提示,你在混合整数和浮点数据。像权重和转换因子的数量一定是double而不是int。例如,在这种情况下,即使numLethalDosenumSodaCan应该是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); 
+0

谢谢你的帮助。我已经看过我的数学,并以另一种方式表达了自己的想法。我还研究了'#include '还有'cout << fixed << setprecision(0);',它帮助浮点数。 – YoYo 2014-09-01 07:03:51

+0

我认为问题的根源在于你在第一行中用'artificialSweetener'划分,它不属于那里。甜味剂的比例对致死剂量没有影响。所以除以0.01有效地乘以100你的输出。 – 2014-09-01 17:20:02