无法在 C++ 中找到两个项目之间的差异
Trouble finding differential between two items in C++
首先让我说我对 C++ 还是非常陌生,希望让事情尽可能简单。我的数学也很差。
大多数情况下,我正在寻找是否有人可以帮助我的代码,以便它始终给出正确的结果。除了在一种情况下,我大部分时间都让它做我想做的事。
我的代码试图找出某人购买了多少包热狗威纳和多少包热狗面包。然后它会告诉用户他们可以从中制作多少热狗,以及他们将拥有多少剩余的 weiner 或面包。假设一包威纳有 12 个,一包包子有 8 个,这是我目前得出的结论:
#include <iostream>
#include <cmath>
using namespace std;
void hotdog(int a, int b){ //a = weiner packages, b = bun packages
int weiners = 12 * a;
int buns = 8 * b;
int total = (weiners + buns) - (weiners - buns);
int leftOverWeiners = total % weiners;
int leftOverBuns = total % buns;
int totalHotDogs = total / 2;
cout << "You can make " << totalHotDogs << " hotdogs!" << endl;
if (leftOverWeiners > 0){
cout << "You have " << leftOverWeiners << " weiners left over though." << endl;
}else if (leftOverBuns > 0){
cout << "You have " << leftOverBuns << " buns left over though." << endl;
}
}
int main(){
int a;
int b;
cout << "Let's see how many hotdogs you can make!" << endl;
cout << "How many weiner packages did you purchase?: ";
cin >> a;
cout << "How many bun packages did you purchase?: ";
cin >> b;
hotdog(a, b);
return 0;
}
有了这个,如果馒头和馒头的比例相同,或者馒头比馒头多,我总能得到正确答案。
由于我设置总 and/or leftOverBuns 的方式(第 9、11 行),我永远无法得到剩余面包的正确答案。我知道如果不能修改我当前的代码,那么必须有一种更简单的方法来执行此操作,但我很困惑。
我知道我几乎留下了零符号,所以如果你想要一些请告诉我!
#include <iostream>
void hotdog( int weinerspackages, int bunspackages ){
const int weinersPerPackage = 12;
const int bunsPerPackage = 8;
const int totalweiners = weinerspackages * weinersPerPackage;
const int totalbuns = bunspackages * bunsPerPackage;
int leftoverweiners = 0;
int leftoverbuns = 0;
int amountOfHotdogs = 0;
if( totalweiners > totalbuns ){
leftoverweiners = totalweiners - totalbuns;
amountOfHotdogs = totalbuns;
leftoverbuns = 0;
}
else if( totalbuns > totalweiners ){
leftoverbuns = totalbuns - totalweiners;
amountOfHotdogs = totalweiners;
leftoverweiners = 0;
}
else{
amountOfHotdogs = totalweiners;
leftoverweiners = 0;
leftoverbuns = 0;
}
std::cout << "You can make: " << amountOfHotdogs << " Hotdogs" << std::endl;
std::cout << "Leftover Weiners: " << leftoverweiners << " || Leftover Buns: " << leftoverbuns << std::endl;
}
int main(){
int PackagesW = 8;
int PackagesB = 12;
hotdog( PackagesW, PackagesB );
system("pause");
return 0;
}
注意:可以用更少的变量来做到这一点,我声明了这个数量的变量是为了更容易理解数字代表什么。
假设做一个热狗只需要每一种,你可以找出你所用的材料最少的,你能做的热狗的数量将受到该材料数量的限制,即为什么 amountOfHotdogs
取较小的值。如果两者的数量相等,那么amountOfHotdogs
可以取其中一个的数量。
只有量大的食材才会有剩菜,因此leftoverweiners = totalweiners - totalbuns;
当totalweiners > totalbuns
反之亦然。
你把事情搞得太复杂了。试试这个:
if(weiners > buns)
{
cout << "You can make " << buns << " hotdogs!" << endl;
cout << "with " << weiners-buns << " weiners left over" << endl;
return;
}
cout << "You can make " << weiners << " hotdogs!" << endl;
if(buns > weiners)
{
cout << "with " << buns-weiners << " buns left over" << endl;
}
{buns, weiners} 中较小的是热狗的数量,if-then 块决定函数是否报告剩余的面包或 weiners。
首先让我说我对 C++ 还是非常陌生,希望让事情尽可能简单。我的数学也很差。
大多数情况下,我正在寻找是否有人可以帮助我的代码,以便它始终给出正确的结果。除了在一种情况下,我大部分时间都让它做我想做的事。
我的代码试图找出某人购买了多少包热狗威纳和多少包热狗面包。然后它会告诉用户他们可以从中制作多少热狗,以及他们将拥有多少剩余的 weiner 或面包。假设一包威纳有 12 个,一包包子有 8 个,这是我目前得出的结论:
#include <iostream>
#include <cmath>
using namespace std;
void hotdog(int a, int b){ //a = weiner packages, b = bun packages
int weiners = 12 * a;
int buns = 8 * b;
int total = (weiners + buns) - (weiners - buns);
int leftOverWeiners = total % weiners;
int leftOverBuns = total % buns;
int totalHotDogs = total / 2;
cout << "You can make " << totalHotDogs << " hotdogs!" << endl;
if (leftOverWeiners > 0){
cout << "You have " << leftOverWeiners << " weiners left over though." << endl;
}else if (leftOverBuns > 0){
cout << "You have " << leftOverBuns << " buns left over though." << endl;
}
}
int main(){
int a;
int b;
cout << "Let's see how many hotdogs you can make!" << endl;
cout << "How many weiner packages did you purchase?: ";
cin >> a;
cout << "How many bun packages did you purchase?: ";
cin >> b;
hotdog(a, b);
return 0;
}
有了这个,如果馒头和馒头的比例相同,或者馒头比馒头多,我总能得到正确答案。
由于我设置总 and/or leftOverBuns 的方式(第 9、11 行),我永远无法得到剩余面包的正确答案。我知道如果不能修改我当前的代码,那么必须有一种更简单的方法来执行此操作,但我很困惑。
我知道我几乎留下了零符号,所以如果你想要一些请告诉我!
#include <iostream>
void hotdog( int weinerspackages, int bunspackages ){
const int weinersPerPackage = 12;
const int bunsPerPackage = 8;
const int totalweiners = weinerspackages * weinersPerPackage;
const int totalbuns = bunspackages * bunsPerPackage;
int leftoverweiners = 0;
int leftoverbuns = 0;
int amountOfHotdogs = 0;
if( totalweiners > totalbuns ){
leftoverweiners = totalweiners - totalbuns;
amountOfHotdogs = totalbuns;
leftoverbuns = 0;
}
else if( totalbuns > totalweiners ){
leftoverbuns = totalbuns - totalweiners;
amountOfHotdogs = totalweiners;
leftoverweiners = 0;
}
else{
amountOfHotdogs = totalweiners;
leftoverweiners = 0;
leftoverbuns = 0;
}
std::cout << "You can make: " << amountOfHotdogs << " Hotdogs" << std::endl;
std::cout << "Leftover Weiners: " << leftoverweiners << " || Leftover Buns: " << leftoverbuns << std::endl;
}
int main(){
int PackagesW = 8;
int PackagesB = 12;
hotdog( PackagesW, PackagesB );
system("pause");
return 0;
}
注意:可以用更少的变量来做到这一点,我声明了这个数量的变量是为了更容易理解数字代表什么。
假设做一个热狗只需要每一种,你可以找出你所用的材料最少的,你能做的热狗的数量将受到该材料数量的限制,即为什么 amountOfHotdogs
取较小的值。如果两者的数量相等,那么amountOfHotdogs
可以取其中一个的数量。
只有量大的食材才会有剩菜,因此leftoverweiners = totalweiners - totalbuns;
当totalweiners > totalbuns
反之亦然。
你把事情搞得太复杂了。试试这个:
if(weiners > buns)
{
cout << "You can make " << buns << " hotdogs!" << endl;
cout << "with " << weiners-buns << " weiners left over" << endl;
return;
}
cout << "You can make " << weiners << " hotdogs!" << endl;
if(buns > weiners)
{
cout << "with " << buns-weiners << " buns left over" << endl;
}
{buns, weiners} 中较小的是热狗的数量,if-then 块决定函数是否报告剩余的面包或 weiners。