迷失了如何解决这个问题

Lost on how to go about this

本文关键字:问题 解决 何解决 迷失      更新时间:2023-10-16

我当前在CS135中,我被困在我的任务上。它要求我做任何类型的循环,但我必须"每月付费一分钱"。每个月的付款翻倍。然后给出64个月的产出,支付的总金额以及支付的金额延长到给定月。我完全亏损。我得到了如何加倍数量,但是从那里开始,我不知道如何继续。我如何有效地完成任务?(奖品是按以下方式支付的:获胜者收到图纸的一分钱。在接下来的64个月内,该金额增加了一倍,例如,第2个月,金额为两美分,第3个月,金额为4美分,而因此。另外,获胜者可以选择以$ 1,000,000.00的价格进行一次性付款。编写一个C 计划,该计划显示每月必须支付多少Mega Magazine Magazine Magazine Mail必须支付胜利者和每月付款的总付款。识别**公司总计$ 1,000,000.00的月份。(

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    for (int n = 1 ; n <= 65; n*= 2){
        cout << n << endl; 
    }
    return 0;
}

您需要在函数中创建一个变量x,并在每次循环时添加n(每月付款?(的值(您的付款?(。在循环结束时,此X变量将是支付的总金额。

在循环的任何给定迭代中,X的当前值将是"支付给定月底的金额"。您可以在每个循环期间打印X值以显示此信息?

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    long double totalPayment = 0;
    long double currentPayment = 1;
    for (int n = 1 ; n <= 64; n++){
        totalPayment = totalPayment + currentPayment;
        cout << n << endl;          //This is the month
        cout << currentPayment << endl;         //This is the payment for the given month
        cout << totalPayment << endl;       //This is the total amount paid up to and including the given month
        currentPayment = currentPayment*2;
    }
    return 0;
}

类似上述代码。

您只需要在每次迭代中占据2的力量即可。INT也可能导致整数溢出。因此,未签名长。

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    unsigned long  amt=1, total_amt=0, temp;
    for (int n = 0 ; n < 64; n++)
    { 
        temp = amt*pow(2,n);
        total_amt += temp; 
        cout << "current amount in month "<< n<<" " << temp << endl; 
    }
    cout << "total amount " << total_amt << endl;
    return 0;
}

您的每月应付时间为0.01美分,但每月两倍,因此在64个月的时间内,您欠下荒谬的金额。只需在整个期限内创建一个for循环,并计算您支付的每月金额以及累积的总额。

#include <iostream>
#include <iomanip>
double getPayment(double payment)
{
    return (payment == 0.0) ? payment += 0.01 : payment * 2.0;
}
int main()
{
    double amount{0.0};  // monthly payment amount
    double total{0.0};   // total paid over lifetime (64 months)
    std::cout << std::fixed << std::showpoint;
    std::cout << std::setprecision(2); // money format i.e. $X.00
    for (size_t i = 0; i < 64; i++)
    {
        amount = getPayment(amount);
        total += amount;
        std::cout << "month["<<i+1<<"]: amount due "<< amount
           << ", total paid " << total << std::endl;
    }
    return 0;
}
int calculate(totalMonths){
int currAmount=1;
int totalAmount=0;
for(int currMonth=1; currMonth<=totalMonths; currMonth++){
    totalAmount = totalAmount+currAmount;
    currAmount *=2;
    cout<<"current month = "<<currMonth<<endl;
    cout<<"current amount = "<<currAmount<<endl;
    cout<<"total amount = "<<totalAmount<<endl;
}

}