一个循环问题:这个题应该没有,但运行结果却有问题,问题在哪
#include <stdio.h>
#include <math.h>
int main()
{ int i8,i5,i3,i2,i1,n1,n2,n3,n4,n5;
float sum=0,max=0;
for (i8=0;i8<3;i8++)
{
for (i5=0;i5<=(20-(8*i8))/5;i5++)
{
for (i3=0;i3<=(20-8*i8-5*i5)/3;i3++)
{
for (i2=0;i2<=(20-8*i8-5*i5-3*i3)/2;i2++)
{
i1=20-8*i8-5*i5-3*i3-2*i2;
sum=2000.0*pow((1+0.0063*12),(float)i1)
*pow((1+0.0066*12),(float)i2)
*pow((1+0.0069*12),(float)i3)
*pow((1+0.0075*12),(float)i5)
*pow((1+0.0084*12),(float)i8);
if (sum>max)
{
max=sum;
n1=i8;
n2=i5;
n3=i3;
n4=i2;
n5=i1;
}
}
}
}
}
printf ("8 year:%d 5 year:%d 3 year:%d 2 year:%d 1 year:%d Total:%.2f ",n1,n2,n3,n4,n5,max)
; return 0;
}
你确定的你公式没有,我大概看下,的确是当i8~i2都是0,i1=1时,值最大(也就是第一个sum值)
你的循环根本没用