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(" ...
随机推荐
- PHP利用GD库绘图和生成验证码图片
首先得确定php.ini设置有没有打开GD扩展功能,測试例如以下 print_r(gd_info()); 假设有打印出内容例如以下,则说明GD功能有打开: Array ( [GD Version] = ...
- ImportError: No module named '_sqlite3'
问题: Python 3.5.1 报错如下 Traceback (most recent call last): File "manage.py", line 16, in < ...
- C# 中安全代码与不安全代码
C# 中安全代码与不安全代码 P/Invoke 非托管代码需要在unsafe块中书写. using System; using System.Collections.Generic; using Sy ...
- IEnumerable, IEnumerator接口
IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...
- 【转】Jenkins+Ant+Jmeter接口自动化集成测试实例
出处:https://my.oschina.net/MrToStudy/blog/742251 一.Jenkins安装配置 1.安装配置JDK1.6+环境变量: 2.下载jenkins.war,放入C ...
- 一站式WPF--依赖属性(DependencyProperty)
2009-10-20 11:32 by 周永恒, 22441 阅读, 24 评论, 收藏, 编辑 书接上文,前篇文章介绍了依赖属性的原理和实现了一个简单的DependencyProperty(DP), ...
- iOS - 每隔一段时间,反复执行同一个任务
我们有时候会有这样的需求,当程序处于运行状态,每隔几秒给服务器发送一次请求. 这时我们可以这样处理: UILocalNotification *localNotification = [[UILoca ...
- Black And White(DFS+剪枝)
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others ...
- Zabbix-Agent 客户端安装配置
1.安装Zabbix官方的yum源 [root@crazy-acong ~]# rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zab ...
- 在线工具集合(新增cron quartz表达式在线生成……)
缘起 平时工作,须要一些工具.经过一些使用,对照,保留一些比較方便好用的在线工具 工具会持续更新中.. . 在线编译&&反编译 http://www.showmycode.com/ ...