鱼C论坛

 找回密码
 立即注册
查看: 670|回复: 4

请问为什么无法进入循环,始终输出值为1

[复制链接]
发表于 2022-3-20 21:27:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/*题目:已知2021年中国GDP总量为美国的77%,若中国GDP增速保持为6%,美国GDP增速保持为2%(2019年数据),编程计算需要多久中国GDP将超过美国?
*/
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int year=1;
    double USA=1,CHN=0.77,m=1+0.06,n=1+0.02;
    for(;0.77*CHN>USA;year++)
    {
        pow(m,year);
        CHN=m;
        pow(n,year);
        USA=n;
    }
    cout<<"中国的GDP需要"<<year<<"年超过美国"<<endl;

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-20 21:28:52 | 显示全部楼层
一个小数乘以一个小数能比1还大吗?怎么进入循环体
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 23:03:39 | 显示全部楼层
1.pow是需要返回值的,你这里应该是这样吧
  1. CHN= pow(m,year);
  2. USA= pow(n,year);
复制代码

2、你的for条件是 【0.77 *cn > us】 这不是一下子就退出了吗
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     int year=1;
  7.     double USA=1,CHN=0.77,m=1+0.06,n=1+0.02;
  8.     for(;CHN<USA;year++)
  9.     {
  10.         CHN= pow(m,year);
  11.                 USA= pow(n,year);
  12.     }
  13.     cout<<"中国的GDP需要"<<year<<"年超过美国"<<endl;
  14.         return 0;
  15. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 23:07:10 | 显示全部楼层
大马强 发表于 2022-3-20 23:03
1.pow是需要返回值的,你这里应该是这样吧

2、你的for条件是 【0.77 *cn > us】 这不是一下子就退出了吗 ...

不对呀,你咋用pow呢
我觉得是这样的
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     int year=1;
  7.     double USA=1,CHN=0.77,m=1+0.06,n=1+0.02;
  8.     for(;CHN<USA;year++)
  9.     {
  10.         CHN= CHN * m;
  11.                 USA= USA * n;
  12.     }
  13.    
  14.     cout<<"中国的GDP需要"<<year<<"年超过美国"<<endl;
  15.         return 0;
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-21 20:13:23 | 显示全部楼层
这样?
  1. #include <stdio.h>

  2. typedef struct {
  3.         float monetary, speed;
  4. }GDP;

  5. void f(GDP* Country) {
  6.         Country->monetary = Country->monetary + (Country->monetary * Country->speed);
  7. }

  8. int main() {
  9.         GDP China = { 0.77, 0.06 };
  10.         GDP US = { 1., 0.02 };
  11.         int N = 0;
  12.         while (China.monetary < US.monetary)
  13.         {
  14.                 f(&China);
  15.                 f(&US);
  16.                 N++;
  17.                 printf("year %d: GDP China: %.2f, GDP US: %.2f\n", N, China.monetary, US.monetary);
  18.         }
  19.         printf("%d", N);
  20.         return 0;
  21. }
复制代码
  1. year 1: GDP China: 0.82, GDP US: 1.02
  2. year 2: GDP China: 0.87, GDP US: 1.04
  3. year 3: GDP China: 0.92, GDP US: 1.06
  4. year 4: GDP China: 0.97, GDP US: 1.08
  5. year 5: GDP China: 1.03, GDP US: 1.10
  6. year 6: GDP China: 1.09, GDP US: 1.13
  7. year 7: GDP China: 1.16, GDP US: 1.15
  8. 7
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 19:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表