c++ - visual studio 2013 C++ 中的迭代循环

标签 c++

我需要代码来计算用户选择的值(每年节省的金额、达到的金额和利息百分比)。

我最好的猜测是这可以通过某种循环来解决吗?所以每年 500 * 1.05(利息是 5%)= 525 + 500 = 1025 * 1.05..等等。

可能没那么难...但我还需要知道达到目标需要多少年,例如需要循环多少次才能达到 >= 50 000。 我已经开始编写一些代码,但这是我继“Hello world”之后的第二件事。 任何对此的帮助都将是惊人的!

#include <iostream>

using namespace std;

int main() {
    float save, goal, intrest;

    cout << "save: ";
    cin >> save;
    cout << "goal: ";
    cin >> goal;
    cout << "intrest: ";
    cin >> intrest;

    float intrest1(ranta / 100 + 1);
    float sum = save * intrest1;

    while (sum < goal) {
        sum + 500 = 
    }

    cout << "summa: " << sum;
    system("pause");
}

最佳答案

我认为您关于使用循环进行计算的说法是正确的,而且您也说这并不难 :)。

我只是稍微编辑了你的代码,让它输出你节省的总金额以及达到你的目标需要多少年。

每次迭代,我都会增加年数,然后将 sum 指定为那一年之后节省的金额。

希望这可以帮助您到达目的地。

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    float save, goal, interest;

    cout << "save: ";
    cin >> save;
    cout << "goal: ";
    cin >> goal;
    cout << "intrest: ";
    cin >> interest;

    float interest1 = interest / 100 + 1;
    float sum = 0;
    int years = 0;

    while (sum < goal) {
      years = years + 1;
      sum = sum * interest1 + save;
    }

    cout << "sum: " << sum << "\n";
    cout << "years: " << years << "\n";
    system("pause");
}

关于c++ - visual studio 2013 C++ 中的迭代循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613720/

相关文章:

c++ - cv::goodFeaturesToTrack 不返回任何特征

c++ - 带有自定义比较器的 C++ std::map 用于保持比赛锦标赛

c++ - 运算符++ 中的 Int 参数

c++ - 在 boost::asio 中发送/接收结构

c++ - CPPRestSDK (casablanca) 从传入的 WebSocket 消息中提取 JSON(格式错误的 token )

c++ - 您可以对非整数类型使用 C++14 的单引号 (') 数字分隔符吗?

c++ - 如何指定方法更改了哪些数据成员?

c++ - 检查整数是否具有整数立方根

c++ - 线程安全三角测量库

c++ - 创建 QGradient