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

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

于是,我自己写了一个版本,主要是考虑(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. ALERT日志中常见监听相关报错之中的一个:ORA-609错误的排查

    參考MOS文档有: Troubleshooting Guide ORA-609 : Opiodr aborting process unknown ospid (文档 ID 1121357.1) Al ...

  2. XML文件

    XML 指可扩展标记语言(eXtensible Markup Language) XML 被设计用来传输和存储数据. 什么是 XML? XML 指可扩展标记语言 XML 是一种标记语言,非常类似 HT ...

  3. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp

    C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. HOG特征-理解篇

    网上去找关于HOG的资料,发现理解性的较少,并且较为冗长,为方便大家理解便自己写了篇,希望能对奋斗在特征提取第一线的同志们有所帮助: HOG即histogram of oriented gradien ...

  5. [Practical Git] Diagnose which commit broke something with git bisect

    Sometimes you find a bug in your project that has been around for a while without being noticed; it ...

  6. [Effective C++ --020]宁以pass-by-reference-to-const替换pass-by-value

    前言: 我们都用过C的值传递方式,那么在C++情况下,值传递是怎样工作的呢? 比如: int foo(int x); int i; foo(i); 1.程序内部先取得i的一个副本 2.将副本传递到fo ...

  7. [Effective C++ --017]以独立语句将newed对象置入智能指针

    这一节也比较简单,先假设我们有如下的函数: int foo(); void memFoo(shared_ptr<T> pw, int foo); 现在假设我们要调用memFoo函数: me ...

  8. [转]详述DHCP服务器的三种IP分配方式

    DHCP就是动态主机配置协议(Dynamic Host Configuration Protocol),它的目的就是为了减轻TCP/IP网络的规划.管理和维护的负担,解决IP地址空间缺乏问题.这种网络 ...

  9. java_客户端防表单重复提交和服务器端session防表单重复提交

    用户输入FormServlet链接 FormServlet-〉form.jsp->DoFormServlet FormServlet:产生token,放在session中 form.jsp:hi ...

  10. 利用Multipeer Connectivity框架进行WiFi传输

    什么是Multipeer Connectivity? 在iOS7中,引入了一个全新的框架——Multipeer Connectivity(多点连接).利用Multipeer Connectivity框 ...