明確にするために編集。混乱をお詫び申し上げます:3
オーケー、それで私はオンラインの CS クラスをフォローしていて、C でプログラムを作成することになっています。毎日。
毎日、昨日の2倍に加えて、前日のすべてを得ることができます。
例: .01 から開始し、3 日目までの現在の合計を計算します。したがって、1 日目は .01、2 日目は .02、3 日目は .04 です。3 日目には 0.01+0.02+0.04 (.09) になります。
このプログラムは、このプロセスを任意の月 (28 ~ 31 日) にわたって計算することを目的としています。
これを実装するのに本当に苦労しています。2倍になりましたが、以前に計算された日数を合わせる方法がわかりません。
これが私のコードです:
#include <stdio.h>
#include <math.h>
int main(void) {
/*days represents total days in months*/
/*pens represents the number of pennies on the first day*/
long long days;
long long pens;
do {
printf("Enter the number of days in the month: ");
scanf("%llu", &days);
} while(days < 28 || days > 31);
printf("Enter the initial number of pennies: ");
scanf("%llu", &pens);
for (int i=0; i<= days-1; i++) {
pens += pow(2,i);
printf("You'll have $%llu\n", pens);
}
}
edit2: わかりました。皆さんの素晴らしいアドバイスのおかげで修正できたと思います。最後の部分を次のように変更しました。
for (int i=0; i<= days-1; i++)
{
pens = pens + (pens * 2);
}
total = pens / 100;
printf("You'll have $%.2f\n", total);
}
出力にはまだわずかな問題がありますが (これは、使用しているデータ型が原因であると考えています)、次のように出力されます。
$0.00 $0.00 $0.00 $0.00 $2.00 $7.00 $21.00 $65.00 $196.00 $590.00 $1771.00 $5314.00 $15943.00 $47829.00 $143489.00 $430467.00 $1291401.00 $3874204.00
等
かなり良いですが、最初の数回の反復が 0.00 であるため、それほど正確ではないに違いありません。