刚开始i=0,a=0,然后调用函数double f(double x ),返回值为1,a=a+1,然后i=10,继续调用函数double f(double x ),返回值为101,a=a+101=102,然后i=20,继续调用函数double f(double x )
,返回值为401,a=a+401=503,所以最后输出503
把我的代码拷贝过去运行一次
#include
#include
double f(double x);
int main ()
{
double a=0;//初始化a为0
int i ; //声明i
for(i=0;i<30;i+=10)//循环3次,
{
a+=f((double)i); //每次循环时a += (i*i+1)
printf("a = %5.0f\n", a);
}
/*
*第一次f((double)i) = 0*0+1 = 1
*第二次f((double)i) = 10*10+1 = 101
*第三次f((double)i) = 20*20+1 = 401
*/
printf("%5.0f\n",a);
}
double f(double x )
{
return x*x+1;
}
#include "stdio.h"
double f(double x);
void main ()
{ double a=0;int i ;
for(i=0;i<30;i+=10) //外层循环共3次,a=a+f(i);
// 执行完一次循环内容a+=f((double)i);后,i=i+10,所以i的值依次为0,10,20.
a+=f((double)i); // 1也就是a=a+f(0)+f(10)+f(20);
//
printf("%5.0f\n",a);
}
double f(double x )
{return x*x+1;} // 2 返回x的平方再加1的值
//程序运行后输出的结果是503
//有1与2,表达为数学问题就是(0^2+1)+(10^2+1)+(20^2+1)
// =3+100+400=503