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; }
<script type="text/java script">
-
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
-
点击复制链接 与好友分享!
回本站首页
<script>
function copyToClipBoard(){
var clipBoardContent=document.title + '\r\n' + document.location;
clipBoardContent+='\r\n';
window.clipboardData.setData("Text",clipBoardContent);
alert("恭喜您!复制成功");
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
-
您对本文章有什么意见或着疑问吗?请到
论坛讨论您的关注和建议是我们前行的参考和动力??
-
上一篇:
UVA 12167 - Proving Equivalences(强连通分量+缩点)
-
下一篇:
BNU25359Escape Time II(状态压缩DP)
-
相关文章
-
<script type="text/java script">BAIDU_CLB_fillSlot("182716");
-
<script type="text/java script">BAIDU_CLB_fillSlot("517916");
-
图文推荐
<script>
(function(){
var appid = 'cyrBEfE7C',
conf = 'prod_830794cf494da8b808afb2994cfe0fee';
var doc = document,
s = doc.createElement('script'),
h = doc.getElementsByTagName('head')[0] || doc.head || doc.documentElement;
s.type = 'text/java script';
s.charset = 'utf-8';
s.src = 'http://assets.changyan.sohu.com/upload/changyan.js?conf='+ conf +'&appid=' + appid;
h.insertBefore(s,h.firstChild);
window.SCS_NO_IFRAME = true;
})()
-
<script type="text/java script">BAIDU_CLB_fillSlot("771057");
|