现在代码问题给出了预期的输出

时间:2017-09-26 10:44:41

标签: c++

在正确评分此问题时遇到问题。它一直显示为100%中的87%,但所有先决条件都按预期运行。该程序应该采用一些cookie并确定要使用多少个cookie,盒子和容器,并在有一个时输出剩余的cookie。显然,我错过了一些东西,但我无法弄清楚是什么。

  

一盒24个饼干。

     

容器中有75个盒子。

https://pastebin.com/HURqViSB(如果需要)

非常感谢任何帮助。

#include <iostream>

using namespace std;

int main()
{
    // Write your main here

    int cookies;
    int leftoverCookies;
    int boxes;
    int leftoverBoxes;
    int containers;

    cout << "Please enter the total number of cookies: ";
    cin >> cookies;

    if (cookies == 24) {
        boxes = 1;
        leftoverCookies = 0;
        cout << endl
             << "The number of boxes used: " << boxes;
        cout << endl
             << "The number of leftoverCookies is: " << leftoverCookies;
    }
    else if (cookies < 24) {
        leftoverCookies = cookies;
        boxes = 0;
        cout << endl
             << "The number of boxes used: " << boxes;
        cout << endl
             << "The number of leftover cookies is: " << leftoverCookies;
    }

    else if (cookies > 24) {
        boxes = (cookies / 24);
        cout << endl
             << "The number of boxes used: " << boxes;
        leftoverCookies = (cookies % 24);
        cout << endl
             << "The number of leftover cookies is: " << leftoverCookies;
    }

    if (boxes == 75) {
        containers = 1;
        leftoverBoxes = 0;
        cout << endl
             << "The number of containers used: " << containers;
        cout << endl
             << "The number of leftover boxes: " << leftoverBoxes;
    }
    else if (boxes < 75) {
        containers = 0;
        leftoverBoxes = 0;
        cout << endl
             << "The number of containers used: " << containers;
        cout << endl
             << "The number of leftover boxes: " << leftoverBoxes;
    }

    else if (boxes > 75) {
        containers = (boxes / 75);
        cout << endl
             << "The number of containers used: " << containers;
        leftoverBoxes = (boxes % 75);
        cout << endl
             << "The number of leftover boxes is: " << leftoverBoxes;
    }

    return 0;
}   

2 个答案:

答案 0 :(得分:0)

不应该这一行

else if (boxes < 75) {
    containers = 0;
    leftoverBoxes = 0;

改为阅读...

else if (boxes < 75) {
    containers = 0;
    leftoverBoxes = boxes;

?,由于没有足够的盒子可以添加到容器中,左侧的盒子应该只是盒子的总数

答案 1 :(得分:0)

您可能会忽略此练习的重点 - 您不需要一个条件,只有/%

顺便说一句,摆脱条件也可以在boxes < 75时修复错误,并设置leftoverBoxes = 0而不是leftoverBoxes = boxes

你只需要这样的东西:

int main()
{
    int cookies = 0;
    cout << "Please enter the total number of cookies: ";
    cin >> cookies;

    int boxes = cookies / 24;
    int leftoverCookies = cookies % 24;

    int containers = boxes / 75;
    int leftoverBoxes = boxes % 75;

    cout << endl
         << "The number of boxes used: " << boxes;
    cout << endl
         << "The number of leftover cookies is: " << leftoverCookies;
    cout << endl
         << "The number of containers used: " << containers;
    cout << endl
         << "The number of leftover boxes: " << leftoverBoxes;
}