Yangyang buys apples:
Yangyang bought 2 apples on the first day, 0.8 yuan per apple. Starting the next day, he buys twice times the number of apples a day before, until the number of apples purchased reaches the maximum value of 100. How much money does the average day spend on programming?
1#include <stdio.h>2 intMain ()3 {4 intnum =2;5 intDay =1;6 floatMoney =2*0.8;7 while(1) 8 {9 if(2* Num >= -)//when twice times the number of apples is greater than or equal to 100, jump out of the loopTen { One Break; A } -num =2*num; -Money + = num *0.8; theday++; - } -printf"%.6f\n", Money/Day ); - GetChar (); + return 0; -}
View Code
Example code reference:
1#include <stdio.h>2 intMain ()3 {4 intn =2, day =0;5 floatMoney =0;6 while(N < -)//The number of apples does not exceed7 {8Money + =0.8N//sum up the amount of money spent each day9day++;//days IncrementTenN *=2;//twice times the number of days before buying One } Aprintf"%.6f\n", Money/Day ); - GetChar (); - return 0; the}
View Code
Self-study C second article