这一套题做错了几次,按理说直接用等差数列求和公式就行了,主要是要考虑一些运算符的结核性问题:

四则运算符(+、-、*、/)和求余运算符(%)结合性都是从左到右。

于是,我自己写了一个版本,主要是考虑(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的更多相关文章

  1. 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 ...

  2. 1001 Sum Problem [ACM刷题]

    这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...

  3. HDU 1001 Sum Problem(AC代码)

    #include <stdio.h> int main(){ int k,sum; while(scanf("%d",&k)!=EOF){ ==){ sum=( ...

  4. HDU 1001 Sum Problem

    /* 注意可以是负整数,而且在过程中会超过int,所以要用longlong */ #include <cstdio> int main() { long long n; while (sc ...

  5. 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 ...

  6. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  7. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  9. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

随机推荐

  1. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  2. 2015北京网络赛 J Clarke and puzzle 求五维偏序 分块+bitset

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc20 ...

  3. Web.xml 中增加log4j

    配置文件例如以下.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...

  4. multi-threads synchronization use conditional mutex

    #include <pthread.h> int thread_flag; pthread_cond_t thread_flag_cv; pthread_mutex_t thread_fl ...

  5. Fortify规则与CERT JAVA 安全编程规范的对照表

    Fortify规则与CERT JAVA 安全编程规范的对照表http://www.automationqa.com/forum.php?mod=viewthread&tid=4353& ...

  6. centos中安装jdk方法

    RPM安装方法一: 1.检验系统原版本[root@zck ~]# java -versionjava version "1.6.0_24"OpenJDK Runtime Envir ...

  7. Redis主备自动切换

    Sentinel(哨兵)是用于监控redis集群中Master状态的工具. 一.Sentinel作用  1.Master状态检测   2.如果Master异常,则会进行Master-Slave切换,将 ...

  8. 标签的innerHTML属性和html()

    在新公司开发编码的时候,经常写js代码:有时候就需要往某个标签里添加一些html脚本或者要拿到某个标签里的html脚本,那么就会用到innerHTML和html. 1.innerHTML属性 w3sc ...

  9. 利用CodeSmith生成抽象工厂步骤

    其实CodeSmith挺好的,帮我们主动生成不少代码,并且代码质量不错,下面就来介绍一下利用CodeSmith生成抽象工厂步骤 打开codesmith模板的buildall 注意path的设置,因为后 ...

  10. 初学android:四大组件之contentprovider

    一.ContentProvider的概念ContentProvider:为存储和获取数据提供统一的接口.可以在不同的应用程序之间共享数据.Android已经为常见的一些数据提供了默认的ContentP ...