问题:SUM(n) = 1 + 2 + 3 + ... + n写个算法
回答:
#include<stdio.h>
int main()
{
int n,sum;
while(scanf("%d",&n)!=EOF)
{
if(n%2==0) //oushu
sum=n/2*(n+1);
else
sum=(n+1)/2*n; //jishu
printf("%d\n\n",sum);
}
return 0;
}
时间: 2024-10-26 05:38:52