如果一个人每天的工资是一分钱,那么随着时间的推移,他会挣多少钱(IT专业的学生除外)
C++ program on how much a person would earn over time if salary is one penny per day (Not an IT student)
我有一个项目要写一个程序,计算一个人在一段时间内的收入,如果他或她的工资是第一天一便士,第二天两便士,然后每天翻一番。程序应该询问用户的天数。显示一个表,显示每天的工资,然后在期末显示总工资。输出应该以美元金额显示,而不是便士的数目。
输入验证:工作天数不接受小于1的数字。
这是我的代码到目前为止,我似乎不能让它正常工作(不是一个学生)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<iomanip.h>
int main()
{
int days;
double pennies = 0.01;
double total = 0.0;
double fixed;
double showpoint;
cout << "For how many days will the pay double?";
cin >> days;
cout << endl << "Day Total Payn";
cout << "------------------------n";
for (int count = 1; count <= days; count++)
{
cout << count << "tt$t" << (pow(2, pennies)) << endl;
}
cout << setprecision(2) << fixed << showpoint;
cout << "--------------------------------n";
cout << "Total $t" << total << endl;
getch();
return 0;
}
我已经尽力解释我所做的更改,但如果您需要了解更多,请询问
// Headers for standard library features don't have .h on the end (normally)
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<iomanip>
int main()
{
int days = 0;
// double pennies = 0.01; Not needed.
double total = 0.0;
// If you don't initialise variables it will cause a crash or undefined behaviour.
// double fixed;
// double showpoint;
while (days < 1) // This prevents negative or 0 day contracts.
{
// You need to use the full name to cout or that abomination of a command using namespace std
std::cout << "For how many days will the pay double?";
std::cin >> days;
}
std::cout << std::endl << "Day Total Payn";
std::cout << "------------------------n";
// looping from 0 while less than days is more "normal".
for (int count = 0; count < days; count++)
{
double payForTheDay = (pow(2, count));
std::cout << count << "tt$t" << payForTheDay << std::endl;
total += payForTheDay; // You need to increment the total.
}
// Not sure what this is about
// std::cout << std::setprecision(2) << fixed << showpoint;
std::cout << "--------------------------------n";
std::cout << "Total $t" << total << std::endl;
getch();
return 0;
}
尝试将(pow(2, pennies))
替换为(pennies * pow(2, (count - 1)))
…
指出:
-
pennies
不应该被命名为dollars
吗? -
要计算
total
,只需将其增加到每天的日工资(例如在输出每一行表的循环中)。
看一下问题的基础。它基本上是一个几何级数。
在等比数列中n个数的和为;
Sn=a1((1-r^n)/(1-r))
[a1=first element(in your case 1);r=2(in this case)]
使用公式得到n天的便士数。现在把它转换成美元价值。
如果你需要完整的代码注释在这里
很晚了,但在我看来,对2^n使用按位移位是最好的方法。它是快速和容易使用。
int days; // = amount of days for payment;
// Add input/output logic
if (days<1) {
// Do invalid input logic
}
// Use long long if days>31
for (int K=0; K<days; K++)
cout<<"Day "<<K<<": "<<(1<<K)<<"n;
这里1<<K
是2^K
或者,您可以使用一个变量来保存支付每次迭代移动1
相关文章:
- 复制列表初始化的隐式转换的等级是多少
- while循环中while循环的时间复杂度是多少
- 如何检查一个c++字符串中有多少相同的字符/数字
- C++有多少类型的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 在条件变量中触发错误信号的频率是多少
- 函数的时间复杂度是多少?
- 必须为 C++20 协程帧保留多少内存?
- 对于四轴飞行器,PID中I控制器的理想值应该是多少
- 为什么我会" void value not ignored as it ought to be"?
- C++,数组有多少个地址?
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- 在 Linux 中存储区域设置名称的缓冲区大小应该是多少?
- 在内存不足之前,我可以声明多少个 const 变量?
- 可以读入进程内存的最大块大小是多少?
- 如何在不知道C++中有多少可选参数的情况下在循环中使用va_arg?
- 在二维向量或数组中可以存储的最大元素数是多少?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 计算出有多少客户端可以连接到我正在使用的一些tcp服务器代码
- 如果一个人每天的工资是一分钱,那么随着时间的推移,他会挣多少钱(IT专业的学生除外)