注册 登录
编程论坛 C++教室

求解(关于循环while的问题)

正在努力的人 发布于 2022-07-05 15:23, 685 次点击
为什么我这个程序只能循环一次
#include<iostream>
#include<iomanip>
using namespace std;

int main(){
    int x,n;
    double ans;
    cin>>x>>n;
    while(n-1){
        ans = (1+0.001)*x;
        x = ans;
        n--;
    }
    cout<<fixed<<setprecision(4)<<ans;
   
    return 0;
}
2 回复
#2
rjsp2022-07-05 18:13
为什么不肯贴出你的输入数据?
循环多少次只取决于你的输入,
如果输入 3 3,那就循环2次;
如果输入 2 2,那就循环1次;
如果输入 1 1,那就循环0次.

另外,为什么不肯贴出题目要求?
ans = (1+0.001)*x; x = ans; 非常奇怪,运算是double,结果是int。但看不到题目要求,即不能说你错,也不能说你对。
#3
op1232022-07-13 15:21
对,贴出来呀!!!
1