Home > Back-end >  C beginners ask answer written in vs2017
C beginners ask answer written in vs2017

Time:09-16

Topic is you go to buy apples, 5.8 yuan per jin of apples, buy two jins of apple, on the first day on the second day began to buy twice the day before every day, until the purchase of the total weight of apple does not exceed a maximum of 100 jins, programming on an average day, I pray thee, spend how many money?

The code I wrote is # include
#include
#include
Void main ()
{
Double p, j, z, t;//
P=0;
T=1;
J=2;

For (j=2; J & lt; 100; J j + j)=2 *
{
J=2 * j + j;
P=5.8 * j;
t++;
}
Z=p/t;

Printf (" % lf \ n ", z);
system("pause");
}
Results there are errors I output respectively calculated jin number was wrong but what was wrong with you can help me have a look? J is jin, t is the day, p is the total price, z is the average money every day,

CodePudding user response:

You less a stdlib. H header file

CodePudding user response:

Your number on the first day for the second day 2 jins is 2 * 2 + 2 is 6 jins but on the third day becomes a 2 * 6 + 6 is 18 jins right should be 2 * 4 + 6 is the second day 14 kg because you buy is 4 jins should buy 8 jin on the third day can not see you buy 12 jins of such written circulation conditions there have a problem and you're in a for loop and write again j=2 * j + j this result more wrong code posted below you can see
# include & lt; stdio.h>

Int main ()
{
Double p=0.00, z=0.00;//
Int t=0, j=0, I=0;

For (I=2; i <=100; I *=2)
{
If (j + I & lt; 100)
{
J +=I;
P=5.8 * j;
t++;
}
}
Z=p/t;

Printf (" % lf \ n ", z);
return 0;
}

CodePudding user response:

refer to the second floor N_n_n_n_n_N response:
your number on the first day for the second day 2 jins is 2 * 2 + 2 is 6 jins but on the third day becomes a 2 * 6 + 6 is 18 jins right should be 2 * 4 + 6 is the second day 14 kg because you buy is 4 jins should buy 8 jin on the third day can not see you buy 12 jins of such written circulation conditions there have a problem and you're in a for loop and write again j=2 * j + j this result more wrong code posted below you can see
# include & lt; stdio.h>

Int main ()
{
Double p=0.00, z=0.00;//
Int t=0, j=0, I=0;

For (I=2; i <=100; I *=2)
{
If (j + I & lt; 100)
{
J +=I;
P=5.8 * j;
t++;
}
}
Z=p/t;

Printf (" % lf \ n ", z);
return 0;
}
thank you very much, I haven't learned!!!!!

CodePudding user response:

reference 1st floor cnlzytq response:
you less a stdlib. H header file
thank you
  • Related