我正在尝试制作一张桌子,第2天的工资从第1天开始翻倍,第3天从第2天开始翻倍,并保持加倍。有5天时间显示,然后计算5天的总工资。
但结果从第1天到第5天只有0.02。我错过了什么?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int entries;
double pay = 0.010000;
double sum = 0;
int day;
cout << "Enter number of days";
cin >> entries; //Number of day
cout << "Day Pay\n";
cout << "=========\n";
for (day = 1; day <= entries; day++)
{
cout << day << "\t\t" << (pay * 2);
}
return 0;
}
答案 0 :(得分:2)
您必须在每次迭代中更新薪酬变量:
for (day = 1; day <= entries; day++)
{
cout << day << "\t\t" << pay<<endl;
sum += pay;
pay *= 2;
}
cout << "Total is\t\t" << sum <<endl;
答案 1 :(得分:0)
你需要每天跟踪加倍。您当前的代码每天仅打印两倍的起始值。尝试类似:
for (day = 1; day <= entries; day++)
{
cout << day << "\t\t" << pay;
sum += pay;
pay *= 2;
}