【问题标题】:Trouble calculating cumulative sum of products无法计算产品的累积总和
【发布时间】:2014-11-06 10:47:24
【问题描述】:

我必须编写一个程序来计算该国每年的人口。 2014 年人口为 x,2015 年为 x*(12%),每年增加 12%。 我试过这样做,但无法通过它:

#include<iostream>

using namespace std;
int main(){
int year;
double pop=344000

  cout<<"Year of population: ";
cin >> year;

switch(year){
case 2014: cout<< "344000\n";
break;
case 2015: cout<< pop+=* 0.12 + pop ; //last year pop *0.12+ last year pop
break;
cout <<year;


}
system("pause");
return year;

}

我知道它一团糟,但我真的是 C++ 的菜鸟

【问题讨论】:

  • 这是什么:year+=* 0.12
  • 请修正你的标题
  • 你只需要year += (year*0.12)
  • 或者,年*1.12
  • 这是错误的。首先,您没有存储人口的变量。其次,在case 2015 中,您想与去年的人口相乘,而不是与 YEAR ITSELF (这个year+=* 0.12 + year也是错的,非常错误)

标签: c++ increment


【解决方案1】:

将明年的人口增加 12% 的上一年,我们检查过去了多少年并增加人口 不需要 切换

   int main(){
    int year;
    int baseYear=2014;
    int dif;
    int population=2000; //number of people in 2014

       cout<<"Year of population: ";
       cin >> year;
       dif=year-baseYear
       for(int i=0;i<dif;i++)
         population+= ((0.12)*population) 
        cout << population;


    }

【讨论】:

    【解决方案2】:

    我希望查看这段代码可以帮助您了解您缺少什么。

    include<iostream.h>
    void main()
    {
      int population= 3000000;
      int year;
      cout<<"Enter a year greater than or equal to 2014 ";
      cin>>year;
    
      if(year<2014)
        cout<<"year must be greater than or equal to 2014";
      elseif(year==2014)
        cout<<population;
      else
       {
         int i=2014;
         while(i<=year)
         {
           population=population*1.12;  
           i++;
         }
         cout<<"population";
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2021-06-18
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      相关资源
      最近更新 更多