HDOJ(1001) Sum Problem
这一套题做错了几次,按理说直接用等差数列求和公式就行了,主要是要考虑一些运算符的结核性问题:
四则运算符(+、-、*、/)和求余运算符(%)结合性都是从左到右。
于是,我自己写了一个版本,主要是考虑(n+1)*n始终为偶数,这样就不用担心除以2时的取整问题:
#include <stdio.h> int main(void)
{
int n;
__int64 sum = ; while (scanf("%d", &n) != EOF)
{
sum = ((n + ) * n) / ;
printf("%I64d\n", sum);
printf("\n");
} return ;
}
但是,这个版本并不能通过,还有一种可能就是(n+1)*n导致数据溢出了,于是搜索了一下,发现有人给出了一个通过了的代码,如下:
#include <stdio.h> int main(void)
{
int n;
__int64 sum = ; while (scanf("%d", &n) != EOF)
{
sum = (n % ) == ? (( + n) * (n / )) : (( + n) * (n / ) + n / + );
printf("%I64d\n", sum);
printf("\n");
} return ;
}
如果n为奇数,则使用(1+n)*(n/2)就会漏掉序列最中间的项,所以要最后再加上中间项n/2+1。
为了验证是防止溢出问题,我于是写了另外一个版本,结果通过了,代码如下:
#include <stdio.h> int main(void)
{
int i, n;
int sum = ; while (scanf("%d", &n) != EOF)
{
sum = , i = ; while (i <= n){
sum += i;
++i;
}
printf("%I64d\n", sum);
printf("\n");
} return ;
}
HDOJ(1001) Sum Problem的更多相关文章
- 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 ...
- 1001 Sum Problem [ACM刷题]
这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...
- HDU 1001 Sum Problem(AC代码)
#include <stdio.h> int main(){ int k,sum; while(scanf("%d",&k)!=EOF){ ==){ sum=( ...
- HDU 1001 Sum Problem
/* 注意可以是负整数,而且在过程中会超过int,所以要用longlong */ #include <cstdio> int main() { long long n; while (sc ...
- 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 ...
- 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 ...
随机推荐
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- [AngularJS - app] AngularJS Location-picker app
From: http://rangle.io/blog/two-ways-to-build-a-location-picker-for-a-mobile-angularjs-application/ ...
- memcached在windows下的安装与命令使用方法
先下载memcached for win32 下载地址1:http://filemarkets.com/fs/8tdo6ndg41d919599/ 下载地址2:http://www.400gb.com ...
- [Effective C++ --026]尽可能延后变量定义式的出现时间
引言 每一次构造和析构都需要成本,因此我们在设计代码的时候,应该尽可能考虑到构造和析构的成本. 第一节 延后实现 考虑有以下的代码: void encrypt(string& s); stri ...
- 文件I/O(不带缓冲)之dup和dup2函数
下面两个函数都可用来复制一个现有的文件描述符: #include <unistd.h> int dup( int filedes ); int dup2( int filedes, int ...
- Mysql数据库导出压缩并保存到指定位置备份脚本
#!/bin/bashbackdir=/home/shaowei/dbbakdbuser='dbusername'dbpass='dbpasswd'dblist=$(ls -p /var/lib/my ...
- linux vi 撤销重做于前进后退--转
在vi中按u可以撤销一次操作 u 撤销上一步的操作Ctrl+r 恢复上一步被撤销的操作 注意:如果你输入“u”两次,你的文本恢复原样,那应该是你的Vim被配置在Vi兼容模式了.重做如果你撤销得太多 ...
- Mailing API
Mailing API Configuration Basic Usage Embedding Inline Attachments Mail & Local Development Work ...
- ClientKey实现登录QQ空间,并设置背景音乐
ClientKey大家都知道的,通过webbrowser登录后取得Cookie并计算出GTK,即可操作空间的POST. 源代码中引用了苏飞的Http类库,自己修改添加了一些拓展方法. 下载地址:htt ...
- hihocoder 1186
1186 : Coordinates 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Give you two integers P and Q. Let all div ...