Description of the question
The original base number is 1. If you study hard, your ability value will increase by 1% compared to the previous day, and if you let it go, your ability value will decrease by 1% compared to the previous day. What is the difference in effect after 1 year (365 days)?
Question analysis
The original base number is 1. If you work hard for one day, you will improve by 1%, and the effect will be 1*(1+0.01). If you work hard for two days, you will improve by 1% on the basis of the previous day, and the result will be 1*(1+0.01)*(1+0.01). The upward force every day after one year is (1+0.01) raised to the 365th power.
The opposite force one year later is (1-0.01) raised to the 365th power. Let’s calculate and see the difference in results.
Code
Method 1: Use for loop to implement
#include<stdio.h>
int main()
{
int i;
float up=1.0,down=1.0;
for(i=1;i<=365;i++)
{
up=up*(1+0.01);
down=down*(1-0.01);
}
printf("每天进步一点点一年之后%5.2f\n",up);
printf("每天退步一点点一年之后%5.2f\n",down);
return 0;
}
operation result
Method 2: while statement implementation
#include<stdio.h>
int main()
{
int i=1;
float up=1.0,down=1.0;
while(i<=365)
{
up=up*(1+0.01);
down=down*(1-0.01);
i++;
}
printf("每天进步一点点一年之后%5.2f\n",up);
printf("每天退步一点点一年之后%5.2f\n",down);
return 0;
}
Method 3: do-while statement implementation
#include<stdio.h>
int main()
{
int i=1;
float up=1.0,down=1.0;
do
{
up=up*(1+0.01);
down=down*(1-0.01);
i++;
}while(i<=365);
printf("每天进步一点点一年之后%5.2f\n",up);
printf("每天退步一点点一年之后%5.2f\n",down);
return 0;
}
Method 4:pow()
The pow() function is used to find the y power (power) of x. x, y and function values are actually double types. Its prototype in use is: double pow(double x, double y); Use the pow function A header file must be added: #include<math.h>
#include<stdio.h>
#include<math.h>
int main()
{
int i=1;
float up=1.0,down=1.0;
up=pow(1+0.01,365);
down=pow(1-0.01,365);
printf("每天进步一点点一年之后%5.2f\n",up);
printf("每天退步一点点一年之后%5.2f\n",down);
return 0;
}
think
How do you feel about the difference between making a little progress and slacking off a little?