注册 登录
编程论坛 数据结构与算法

版主救命~

落在秋天的叶 发布于 2010-11-02 17:24, 687 次点击
1068. Sum
Time Limit: 2.0 second
Memory Limit: 16 MB

Your task is to find the sum of all integer numbers lying between 1 and N inclusive.
Input
The input consists of a single integer N that is not greater than 10000 by it's absolute value.
Output
Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive.
 

#include "stdio.h"
int main()
{
   int n,sum = 0;
   int i;
   scanf("%d",&n);
   if(n > 10000)
       return 0;
   if(n < 0)
   {
     for( i = n; i <=0;i ++)
     {
       sum += i;
     }
     sum += 1;
   }
   else
   {
      for(i = 0;i <= n;i ++)
      {
         sum += i;
      }
   }
   printf("%d\n",sum);
   return 0;
}
//////////
#include "stdio.h"
#include "math.h"
int main()
{
   int n,sum = 0;
   int i;
   scanf("%d",&n);
   if(n > 10000)
       return 0;
   printf("%d\n",(n + 1) * (abs(n - 1) + 1)/2);
   return 0;
}
上面是一个acm简单题的两个不同的程序,测试数据相同 结果相同,为什么上面一个通不过,下面一个却可以 。。。
5 回复
#2
筱晓绾2010-11-02 17:26
好像上一个缺少#include "math.h"
#3
落在秋天的叶2010-11-02 17:27
回复 2楼 筱晓绾
哪里缺?第一个不需要的吧
#4
筱晓绾2010-11-02 17:32
程序代码:
if(n < 0)
   {
     for( i = n; i <=0;i ++)
     {
       sum += i;
     }
     sum += 1;
   }
   else
   {
      for(i = 0;i <= n;i ++)
      {
         sum += i;
      }
   }
在此代码外围加个else{},仅供参考!!!
#5
寒风中的细雨2010-11-02 22:32
两个都可以  运行没有什么不对
#6
wangjianhui2010-11-03 21:25
回复 2楼 筱晓绾
·····这个不用math.h也是可以的·
1