hdoj1001--Sum Problem
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
import java.util.*;
import java.io.*; class Main
{
public static int sum(int n) {
if(n % 2 == 0) return n/2*(n+1);
else return (n+1)/2*n; }
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n;
while(cin.hasNextInt()) {
n = cin.nextInt();
System.out.println(sum(n));
System.out.println();
}
}
}
code(C):
#include <stdio.h> int sum(int n)
{
if(n % 2)
return (n + 1) / 2 * n;
else
return (n / 2) * (n + 1);
} int main()
{
int n;
while(scanf("%d",&n) != EOF){
printf("%d\n\n",sum(n));
}
return 0;
}
hdoj1001--Sum Problem的更多相关文章
- summary of k Sum problem and solutions in leetcode
I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏
Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HD2058The sum problem
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Maxmum subsequence sum problem
We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- NYOJ--927--dfs--The partial sum problem
/* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...
- 动态规划法(三)子集和问题(Subset sum problem)
继续讲故事~~ 上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界. 这天,丁丁刚回到家,他 ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Problem-1001:Sum Problem
Sum Problem Sample code : #include <stdio.h> int main() { int i,n; int sum; while(scanf(" ...
随机推荐
- Python内置函数之super()
super(type[,object-or-type]) super()的作用在于类继承方面. 他可以实现不更改类内部代码,但是改变类的父类. 例子: 一般我们继承类的方式: >>> ...
- Python内置函数之range()
range(stop)range(start,stop[,step]) 返回一个range对象,第三个参数的含义为:间隔的个数. range对象同时也是可迭代对象. >>> isin ...
- Redis用LPUSH和RPOP实现消息队列
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceS ...
- 从零開始学Swift之Hello World进化版
上节课,也就是昨晚啦,我们学习到从零開始学Swift之Hello World.那一节仅仅有一句代码,大家会认为不够过瘾. 那么这节课,就给大家来多点瘾货吧! 先上图! //var 代表变量的类型, s ...
- Parencodings - poj 1068
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22764 Accepted: 13344 Description L ...
- web 开发之js---js 实现自动添加input text 编辑框
<html><head><script type="text/javascript">function addNewLine(){var for ...
- cmake实战第一篇:初试 cmake
1.准备工作: 首先,在/code_test 目录下建立一个 cmake 目录,用来放置我们学习过程中的所有练习.(如果以下命令出现xxx: cannot create directory ‘x’: ...
- MATLAB循环结构:for语句+定积分实例
for语句 格式: for 循环变量=表达式1:表达式2:表达式3 循环体语句 end 表达式1:循环变量初值:表达式2:步长:表达式3:循环变量终值. for 循环变量=矩阵表达式 循环体语句 en ...
- 记录-JQuery日历插件My97DatePicker日期范围限制
对于日期控件,有时会有不能选择今天以前的日期这种需求..... My97DatePicker是一个非常优秀的日历插件,不仅支持多种调用模式,还支持日期范围限制. 常规的调用比较简单,如下所示: 1 & ...
- AFN多文件进度下载
AFN参考资料 http://www.jianshu.com/p/c36159094e24 http://blog.cnbang.net/tech/2320/http://blog.cnbang.ne ...