最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目:

Problem Description
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
 
Input
The input will consist of a series of integers n, one integer per line
 
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
 
Sample Input
1 100
Sample Output
1 5050
 #include<stdio.h>
int main()
{
int n,sum;
while(scanf("%d",&n)!=EOF)
{
sum=(n+)*n/;
printf("%d\n\n",sum);
}
}
 
刚开始认为题目很简单,如果直接暴力求和,是可以的,但是我门学过等差数列的求和公式,
这是高斯10岁时的发现。为了代码简洁,我采用了公式法求解。在本地测试时,输出数据完全正确。但是,上边代码在杭电上submit之后总是WA(Wrong Answer)掉。
 
 

我就无语了。然后很长时间想不出原因,最后经过我百度一下才知道问题出在哪里。

在n*(n+1)乘法的时候,会溢出。题目要求“You may assume the result will be in the range of 32-bit signed integer ”,要求的是求和结果是32位有符号整数。OJ给出的测试数据的求和结果(n*(n+1)/2)一定是32位整数范围内的,但是(n*(n+1))就不一定了。可以推断WA的原因应该在此。可以将公式巧妙地变动一下:

  把

sum=n*(n+1)/2;

  改为

    if(n%2==0)

      sum=n/2*(n+1);

    else

      sum=(n+1)/2*n;

整个代码如下:

 #include<stdio.h>
int main()
{
int n,sum;
while(scanf("%d",&n)!=EOF)
{
if(n%==)
sum=n/*(n+);
else
sum=(n+)/*n; printf("%d\n\n",sum);
}
}

接着就可以如愿以偿的AC了。感觉被这个水题差点玩死了- -.

acm入门 杭电1001题 有关溢出的考虑的更多相关文章

  1. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  2. Help Johnny-(类似杭电acm3568题)

    Help Johnny(类似杭电3568题) Description Poor Johnny is so busy this term. His tutor threw lots of hard pr ...

  3. 杭电oj2093题,Java版

    杭电2093题,Java版 虽然不难但很麻烦. import java.util.ArrayList; import java.util.Collections; import java.util.L ...

  4. ACM 五一杭电赛码"BestCoder"杯中国大学生程序设计冠军赛小记

    对于这项曾经热爱的竞赛,不得不说这是我最后一年参加ACM比赛了,所以要珍惜每一次比赛的机会. 五一去杭电参加了赛码"BestCoder"杯中国大学生程序设计冠军赛,去的队伍包括了今 ...

  5. 杭电dp题集,附链接还有解题报告!!!!!

    Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f ...

  6. 【ACM】杭电ACM题一直WA求高手看看代码

    数据测试了好几个都没问题,可以就是WA不让过,检测了2个小时还是没发现有什么问题T_T!!求高手看看代码,小弟在此谢谢各位哦! #include <stdio.h> #include &l ...

  7. Dijk入门(杭电2544题)

    #include<iostream> #include<cstring> using namespace std; #define INF 0x3f3f3f3f int n,m ...

  8. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  9. 杭电的题,输出格式卡的很严。HDU 1716 排列2

    题很简单,一开始写代码,是用整数的格式写的,怎么跑都不对,就以为算法错了,去看大佬们的算法STL全排列:next_permutation(); 又双叒叕写了好几遍,PE了将近次,直到跑了大佬代码发现, ...

随机推荐

  1. soft-margin SVM

    1. soft-margin SVM的形式 其中ξn表示每个点允许的犯错程度(偏离margin有多远),但是犯错是有代价的,也就是目标函数里面要最小化的.c控制对犯错的容忍程度. 2. 推导soft ...

  2. powershell使用

    主要语法点: -match -notmatch -replace -join -split -and -or -xor -not ! +.-.*./.% =.+=.-=.*=./=.%= -eq.-n ...

  3. How do I get the path of the current executed file in Python?

    First, you need to import from inspect and os from inspect import getsourcefile from os.path import ...

  4. iOS 企业设备管理 补充中。。。

    公司的设备都有一个统一的管理方法,以前不太明白,今天看了一下资料. 这里解释了什么是设备管理 Profile Manager creates and distributes configuration ...

  5. Python: 处理mongodb文档,怎么让UTC时间转换为本地时间?

    存储数据到MongoDB数据库时,一般我们会加一个更新数据的时间update_time.这时在python代码中 会用到datetime模块以便获取当前系统时间,但是存入到MongoDB数据库时,存储 ...

  6. LeetCode 167 Two Sum II - Input array is sorted

    Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...

  7. PE读写

    // 仿PE文件.cpp : Defines the entry point for the console application.// #include "stdafx.h"# ...

  8. Clang与libc++abi库安装

    系统ubuntu64位 Clang4.0 参考: 1 https://github.com/yangyangwithgnu/use_vim_as_ide#0.1 其中 第7章 工具链集成 2. htt ...

  9. Delphi的分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同

    转载自:http://www.cnblogs.com/qiusl/p/4028437.html?utm_source=tuicool 我估摸着内存分配+释放是个基础的函数,有些人可能没注意此类函数或细 ...

  10. tornado高效开发必备之源码详解

    前言:本博文重在tornado源码剖析,相信读者读完此文能够更加深入的了解tornado的运行机制,从而更加高效的使用tornado框架. 本文参考武sir博客地址:http://www.cnblog ...