gpt4 book ai didi

C++ 不匹配的结果

转载 作者:行者123 更新时间:2023-11-28 06:42:57 24 4
gpt4 key购买 nike

我有一个程序可以计算(致死百分比的数量和 jar 头的数量)但我遇到的问题是输出与测试文件不匹配。我得到的输出[30266666, 8647618] 而测试文件有 [302667, 864762] 。我是 C++ 的新手,我尝试查看 intdouble 但似乎没有任何效果。

这是我的:

#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;
}

最佳答案

使用除法的两行都给我一个警告 C4244:

warning C4244: '=' : conversion from 'const double' to 'int', possible loss of data

这提示您混淆了整数和 float 据。权重和转换因子等数量绝对应该是 double 而不是 int。在这种情况下,甚至 numLethalDosenumSodaCan 也应该是 double,因为它可能需要 4.5 个汽水 jar 才能杀死你,例如。

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);

关于C++ 不匹配的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25598817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com