|
|
back to boardDiscussion of Problem 1068. Sumhelp! what is wrong with this code. #include<stdio.h> #include<stdlib.h> int main() { int n,i;long long int sum=0; scanf("%d",&n); if(n>=1) { for(i=1;i<=n;i++) sum=sum+i; } if(n<0) { for(;n<=1;n++) sum=sum+n; } printf("%ld \n",sum); system("pause");
} why i got wa? Re: help! if(n>0) { for(int i=1;i<=n;i++) sum=sum+i; } else { for(int j=n;j<=1;j++) sum=sum+j; } Accepted Posted by Undead 19 Nov 2007 02:09 int main(int argc, char* argv[]) { int n,i,c; scanf("%d",&n); c=0; if (n>0) for (i=1;i<=n;i++) c=c+i; if (n<0) for (i=n;i<=1;i++) c=c+i; if (n==0) c=1; printf("%d",c); getch(); return 0; } |
|
|