C++ 编程家庭作业(do...while 循环)

标签 c++

我的一个编程作业如下:

The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both the towns at that time. (A sample input is: Population of town A = 5000, growth rate of town A = 4%, population of town B = 8000, and growth rate of town B = 2%.)

我把作业的主要部分写下来,输入人口和增长率并计算 x 年后的人口,但我不知道如何比较 A 镇的最终人口与 B 镇的最终人口和总和。任何提示将不胜感激。

#include <iostream>

using namespace std;

int main()
{
    int popA, popB, year = 1; 
    double growth_rateA, growth_rateB; 

    cout << "Enter the population and growth rate of Town A: ";
    cin >> popA >> growth_rateA;
    cout << endl;

    cout << "Enter the population and growth rate of Town B: ";
    cin >> popB >> growth_rateB;
    cout << endl;

    if (popA < popB && growth_rateA > growth_rateB)
    {
        {   
        do {
                (popA = ((growth_rateA / 100) * popA) + popA); // calculates population growth in one year
                (popB = ((growth_rateB / 100) * popB) + popB);
                year++;
            }

            while (popA < popB);

            cout << "Town A will surpass Town B in population after " << year << " years.\n" << endl;
            cout << "The final population of Town A is: " << popA << ".\n" << endl;
            cout << "The final population of Town B is: " << popB << ".\n" << endl;
        }
    }
    else
    {
        cout << "Invalid Data.";
    }

    system("pause");
    return 0;
}

最佳答案

Bettorun 说:因为 popApopB 被声明在 outside do-while 循环,它们将在您退出该循环后保留其最终值,您可以使用它们对它们执行其他计算。

这是我添加的代码。我还去掉了一层不必要的嵌套,并从 0 而不是 1 开始这一年。

#include <iostream>

using namespace std;

int main()
{
    int popA, popB, year = 0; 
    double growth_rateA, growth_rateB; 

    cout << "Enter the population and growth rate of Town A: ";
    cin >> popA >> growth_rateA;
    cout << endl;

    cout << "Enter the population and growth rate of Town B: ";
    cin >> popB >> growth_rateB;
    cout << endl;

    if (popA < popB && growth_rateA > growth_rateB) {       
        do {
            (popA = ((growth_rateA / 100) * popA) + popA); // calculates population growth in one year
            (popB = ((growth_rateB / 100) * popB) + popB);
            year++;
        } while (popA < popB);

        int popDifference = popA - popB;
        int popTotal = popA + popB;
        cout << "Town A will surpass Town B in population after " << year << " years.\n" ;
        cout << "The final population of Town A is: " << popA << ".\n";
        cout << "The final population of Town B is: " << popB << ".\n";
        cout << "In year " << year << ", Town A has " << popDifference << " more people than town B.\n";
        cout << "The total population of both Town A and Town B is " << popTotal << ".\n";
    } else {
        cout << "Invalid Data.";
    }

    system("pause");
    return 0;
}

关于C++ 编程家庭作业(do...while 循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163815/

相关文章:

c++ - 将 2 个双数组组合成一个找到它们的并集和交集的最简单方法

c++ - 在 Windows 中,如何检测 Unicode 字符是否呈现为正方形( bean 腐)?

c++ - 删除列表中的结构元素

c++ - 工作线程片刻后停止工作

c++ - OpenGL 中的 GUI 工具包

c++ LoadLibrary() 导致程序退出

c++ - 窗口移动后点击坐标

c++ - 从文本文件中读取矩阵并在 C++ 中使用一列的数字

c++ - VexCL 中的密集矩阵 vector 乘法

c++ - OpenCV C++。快速计算混淆矩阵