33 lines
582 B
C
33 lines
582 B
C
/*------------------------------------------------------------------------
|
||
【程序设计】请编写函数sum,其功能是:用While循环语句求1到n之间(包括n)能
|
||
被3整除的所有整数之和,并将结果返回给主函数。(n值由主函数传入)
|
||
运行程序后若输入:10,则输出为:18
|
||
----------------------------------------------------------------------*/
|
||
#include "stdio.h"
|
||
|
||
long sum(int n)
|
||
{
|
||
/**********Program**********/
|
||
int i, s=0;
|
||
for(i=1;i<=n;i++){
|
||
if(i%3==0){
|
||
s+=i;
|
||
}
|
||
}
|
||
|
||
|
||
return s;
|
||
/********** End **********/
|
||
|
||
}
|
||
|
||
void main()
|
||
{
|
||
int x;
|
||
long f;
|
||
scanf("%d",&x);
|
||
f=sum(x);
|
||
printf("%ld\n",f);
|
||
|
||
}
|