C ++编程家庭作业分配(做......循环)

时间:2017-04-02 01:49:12

标签: c++

我的一个编程作业如下:

  

A镇的人口少于B镇的人口。但是,A镇的人口增长速度快于B镇的人口。编写一个程序,提示用户输入人口和增长率每个城镇。该计划在多少年后输出A镇的人口将大于或等于B镇的人口和当时两个城镇的人口。 (样本输入为:城镇人口A = 5000,城镇A增长率= 4%,城镇人口B = 8000,城镇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;
}

2 个答案:

答案 0 :(得分:0)

Bettorun说:因为popApopBdo - while循环之外宣布 ,所以他们将保留其最终值你已退出该循环,并可以使用它们对它们执行其他计算。

这是我添加的代码。我也摆脱了一层没有必要的嵌套:

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

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

答案 1 :(得分:0)

#include <iostream>

using namespace std;

int main() {

int popA, popB, year; 
double growA, growB;


 cout << "Population of town A ";
 cin >> popA;

  cout << "Population of town B ";
  cin >> popB;
// You can also say cout << "Population of town A and B "; and then say 
// cin >> popA >> popb; just to compile it better
cout << "Growth rate in percent of town A ";
cin >> growA;

cout << "growth rate in percent of town B ";
cin >> growB;
year = 0;
if (popA <= popB && growA > growB) 
{
   do {      
 (popA = ((growA / 100) * popA) + popA);
 (popB = ((growB / 100) * popB) + popB);
    year++;
   } while (popA <= popB);

      cout << year << " year(s) till Town A surpasses Town B in population ";
      cout << popA << ": population of Town A after it surpasses Town B";
      cout << popB << ": population of Town B after it is surpassed by Town A";
   }
   else
   {
       cout << "Does Not Compute";
   {
   cout << endl;
    }  
 }
   return 0;
}