后来想到可以用三目运算符简化代码
#include <stdio.h> int main (){ int n,i,temp; int sum=0; scanf("%d",&n); for(i=0;i<=n;++i){ if(i%2==0){ temp=i*(-1); }else{ temp=i; } sum=sum+temp; } printf("%d",sum); return 0; }
#include <stdio.h> int main (){ int n,i,temp; int sum=0; scanf("%d",&n); for(i=0;i<=n;++i){ if(i%2==0){ temp=i*(-1); }else{ temp=i; } sum=sum+temp; } printf("%d",sum); return 0; }
相关推荐