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

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

于是,我自己写了一个版本,主要是考虑(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. 解读BOM与COM

    概念: 1.BOM(Browser Object Model): 浏览器对象模型,从名字上就能知道它和浏览器关系密切. 浏览器的非常多行为是通过JavaScript控制的.比如打开新窗体.打开关闭标签 ...

  2. CPU相关信息

    unit untCpuInfo;interface{ 获取 CPU 制造商 }function GetCpuFactory: String;{ 获取 CPU 家族系统 }function GetCpu ...

  3. iOS开发——高级技术&支付宝功能的实现

    支付宝功能的实现   现在不少app内都集成了支付宝功能 使用支付宝进行一个完整的支付功能,大致有以下步骤: 1>先与支付宝签约,获得商户ID(partner)和账号ID(seller) (这个 ...

  4. android学习日记03--常用控件checkbox/radiobutton

    常用控件3.checkbox 复选框,确定是否勾选,点击一下勾选,点击第二下取消,当有一系列备选项时适合用checkbox控件,方便用户提交数据. 贴上例子Activity的java代码 packag ...

  5. 微软雅黑字体IE6 opacity改变,字体会变样子

    微软雅黑字体IE6 opacity改变,字体会变样子,换个字体就好了

  6. 文件I/O(不带缓冲)之I/O的效率

    程序清单3-3中的程序使用read和write函数复制文件.关于该程序应注意下列各点: 它从标准输入读,写至标准输出,这就假定在执行本程序之前,这些标准输入.输出已由shell安排好.确实,所有常用的 ...

  7. Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)

    MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...

  8. Memcached source code analysis (threading model)--reference

    Look under the start memcahced threading process memcached multi-threaded mainly by instantiating mu ...

  9. Authentication

    Authentication Introduction Configuration Storing Passwords Authenticating Users Basic Usage Introdu ...

  10. Ⅲ.spring的点点滴滴--赋值

    承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...