程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> NYOJ-sum of all integer numbers

NYOJ-sum of all integer numbers

編輯:C++入門知識

NYOJ-sum of all integer numbers


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.
樣例輸入
3
樣例輸出
6

代碼:

#include
#include
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		if(n==0)
		{
			printf("1\n");
			continue;
		}
		int k=1,t=0;  
		if(n<0)
		k=-1,t=1;
		n=abs(n);
		if(n&1)
		printf("%d\n",((n+1)>>1)*n*k+t);
		else
		printf("%d\n",(n>>1)*(n+1)*k+t);
	   
	}
	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved