迷失了如何解决这个问题
Lost on how to go about this
我当前在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;
}
}
相关文章:
- Project Euler问题4的错误解决方案
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 使用 bfs 解决连接组件问题时得到错误的答案
- 如何解决 Ninja c++ 构建和执行问题
- 无法在问题解决方案中执行输出逻辑
- 如何在不到O(N)的时间内解决这个问题?
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- 如何在不使用额外空间的情况下解决这个问题?
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 如何解决"no Qt platform plugin could be initialized"问题?
- 如何解决可能的唯一组合问题
- Windows上的模板有什么问题?解决方案是什么