1001Sum Problem
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)   
Total Submission(s): 224457 Accepted Submission(s): 55071    
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).   
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
Sample Input
1
100
Sample Output
1
5050 水题,不过有个陷阱,坑了我好久啊,一个边界溢出问题,要注意。 下面是AC代码: 用这种方法写是没什么问题的,注意用long类型:#include<stdio.h>
int main ()
{
long int n,i,sum;
while(scanf("%ld",&n)!=EOF)
{
sum=0;
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("%ld\n",sum);
printf("\n");
}
return 0;
}
但是如果用求和公式的话,一定要这样,防止边界溢出,考虑项数除以2 是否为0
#include<iostream>
using namespace std;
int main()
{
int s,n;
while(cin>>n)
{
s=(n%2==0?n/2*(n+1):(n+1)/2*n);
cout<<s<<endl<<endl;
}
}
java版就大的多
import java.util.*;
public class Main {
public static void main(String[] args)
{
int sum,i,n;
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
sum =0;
n=cin.nextInt();
for(i=0;i<=n;i++)
{sum+=i;}
System.out.println(sum);
System.out.print("\n\n");
}
}
}
1001Sum Problem的更多相关文章
- HDOJ1001-1005题解
		
1001--Sum Problem(http://acm.hdu.edu.cn/showproblem.php?pid=1001) #include <stdio.h> int sum(i ...
 - 1199 Problem B: 大小关系
		
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
 - No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
		
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
 - C - NP-Hard Problem(二分图判定-染色法)
		
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
 - Time Consume Problem
		
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
 - Programming Contest Problem Types
		
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
 - hdu1032 Train Problem II (卡特兰数)
		
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
 - BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
		
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
 - [LeetCode] Water and Jug Problem 水罐问题
		
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
 
随机推荐
- IntelliJ IDEA使用总结篇
			
解决控制台中文乱码的问题: 1.windows下改intellij安装目录下bin\idea.exe.vmoptions文件 加上 -Dfile.encoding=UT 2.设置IDEA server ...
 - 【英语】Bingo口语笔记(21) - 表达“请客吃饭”
 - 获取当前匹配元素 包括自身的html
			
$(".test").prop("outerHTML"); 来自为知笔记(Wiz)
 - mysql 表空间及索引的查看方法
			
CONCAT : concat() 方法用于连接两个或多个数组. database : 数据库(11张) 数据库,简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件 ...
 - 使用服务器端控制AJAX页面缓存
			
你知道 response.setHeader("Cache-Control","no-cache"); 这条语句是干什么的吗? 这是用来防止浏览器缓存动态内容生 ...
 - Intent(意图)
			
Intent的中文意思是“意图,目的”的意思,可以理解为不同组件之间通信的“媒介”或者“信使”. 目标组件一般要通过Intent来声明自己的条件,一般通过组件中的<intent-filter&g ...
 - ylb:创建数据库、表,对表的增查改删语句
			
ylbtech-SQL Server:SQL Server-创建数据库.表,对表的增查改删语句 SQL Server 创建数据库.表,对表的增查改删语句. 1,ylb:创建数据库.表,对表的增查改删语 ...
 - 在Eclipse中用TODO标签管理任务
			
在Eclipse中用TODO标签管理任务 Elipse为Java项目的时候,有一个很人性化的“任务管理”功能,利用这个功能可以方便地将项目中一些需要处理的任务记录下来.先来看看“任务管理”是怎么使用的 ...
 - PHP如何返回json格式的数据
			
我们常见一些网站在做ajax时返回JSON格式的数据: 返回的是json格式的数据 这有什么好处那?很显然前端在接到返回的数据时可以直接使用,而不用再用eval('(+ returnString +) ...
 - C++ STL算法系列6---copy函数
			
现在我们来看看变易算法.所谓变易算法(Mutating algorithms)就是一组能够修改容器元素数据的模板函数,可进行序列数据的复制,变换等. 我们现在来看看第一个变易算法:元素复制算法copy ...