#include "stdio.h"
int main(int argv,char *argc[]){
int i;
double s;
for(s=0.0,i=1;i<1001;i++)
if(i%3==0 || i%7==0)
s+=1.0/i;
printf("The result are %.5f\n",s);
return 0;
}
运行结果:
#include
int main()
{int i;
double y=0.0;
for(i=1;i<=1000;i++)
if(i%3==0||i%7==0)y+=1.0/i;
printf("%.5lf\n",y);
return 0;
}
C语言版本如下:#includeintmain(){inti,sum=0;for(i=1;i<=100;i++){if(i%3==0||i%7==0)sum+=i;elseprintf("%3d不能被3或7整除\n",i);}printf("所有能被3或7整除的自然数和为%d\n",sum);return0;}
#include
int main()
{
int i;
double s=0;
for(i=1;i<1000;i++)
if(i%3==0 || i%7 == 0)
s+=1.0/i;
printf("%.5lf\n", s);
return 0;
}
如下,方法比较笨,但是好理解
#include
double HE=0;
void main( )
{
int i;
for(i=3;i<=1000;i++)
{
if((i%3==0)||(i%7==0))
{
HE += 1/(double)i;
}
}
printf("%.5lf\r\n",HE);
}