NYOJ 436 sum of all integer numbers

桃扇骨 2022-09-19 08:07 105阅读 0赞

sum of all integer numbers

时间限制: 1000 ms | 内存限制: 65535 KB

难度: 0

描述

Your task is to find the sum of all integer numbers lying between 1 and N inclusive.

输入

There are multiple test cases.
The input consists of a single integer N that is not greater than 10000 by it’s absolute value.

输出

Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive.

样例输入

  1. 3

样例输出

  1. 6

思路:坑爹的题目。

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n;
  5. while(scanf("%d",&n) != EOF)
  6. {
  7. if (n>0)
  8. {
  9. printf("%d\n",(n*(n+1)) >> 1);
  10. }
  11. else if (n == 0)
  12. {
  13. printf("1\n");
  14. }
  15. else
  16. {
  17. printf("%d\n",1-((n*(n-1)) >> 1));
  18. }
  19. }
  20. }

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Sum of Two Integers

    思路:在不使用运算符求和的问题中,最容易想到的一种方法是借助与、异或和移位操作符。与和移位操作符配合使用可以找到进位,异或操作符能够找到非进位,两者相加得到Sum,由于不允许使