展开全部
C可以这么写32313133353236313431303231363533e78988e69d8331333361303664:#include "stdio.h"
int main(void){
int n;
while(1){
printf("Input n(int n>0)...\nn=");
if(scanf("%d",&n) && n>0){
n-=!(n&1);
printf("The result is %d\n",(1+n)/2*(n/2+1));
break;
}
printf("Error, must be n>0: ");
fflush(stdin);
}
return 20;
}
也可以这么写:#include "stdio.h"
int main(void){
int n,i,sum;
while(1){
printf("Input n(int n>0)...\nn=");
if(scanf("%d",&n) && n>0){
for(sum=0,i=1;i<=n;sum+=i,i+=2);
printf("The result is %d\n",sum);
break;
}
printf("Error, must be n>0: ");
fflush(stdin);
}
return 20;
}