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 ...
随机推荐
- .gitignore规则不生效的解决办法
.gitignore规则不生效的解决办法 使用git 的时候,在.gitignore中已经添加了某个文件或者文件夹,但是使用git status还能看见该文件的修改提示--–说明.gitignore未 ...
- iOS XMPP Framework 中文概述
本篇文章翻译XMPP Framework中的Overview of the XMPP Framework部分 介绍 The framework is divided into 2 parts: 1. ...
- Java面向对象编程概述
一. 01.软件开发经历周期: 软件分析:分析问题领域,了解客户的需求 软件设计:确定软件的总体结构,把整个软件系统划分为大大小小的多个子系统,设计每个子系统的具体结构 软件编码:用选定的编程语言来编 ...
- Mybatis foreach
批量删除: <delete id= "deleteBatchByXXX" parameterType= "list"> delete from 表名 ...
- ApplicationContext.xml 的最终xml声明,包括注解 aop
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- An unexpected exception occurred while creating a change object. see the error log for more details
今天再给Android项目工程中的包重命名时出现了这个错误(之前重命名的时候就没有出现,郁闷): An unexpected exception occurred while creating a c ...
- Python time mktime()方法
描述 Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数. 如果输入的值不 ...
- unity, audio falloff
要达到声音随距离衰减的效果,需要使用3D音效,即把Spatial Blend设为1,然后再调节Min Distance和Max Distance(蓝色线框球体),并选择合适的Volume Rollof ...
- Creole
Home Bisher besucht: AnzeigenAnhängeInfo The Creole 1.0 project has been succ ...
- pdf Factory的设置
将文件自动保存到其它位置的设置方法(以Excel文件打印为例):