杭电1001

这道题是前n项数求和的问题,与直接求和不同,这道题要求能连续求和,故而多了一条循环语句。下面是两种代码:

#include<stdio.h>
  main()
{
   int n,i,s=0;
    scanf("%d",&n);
     for(i=0;i<=n;i++)
     s+=i;
    printf("%d\n\n",s);
}

//这是代码原形

#include<stdio.h>
main()
{
int n,i,s=0;
while(scanf("%d",&n)!=EOF)   //循环语句
{
for(i=0;i<=n;i++)
s+=i;
printf("%d\n\n",s);
s=0;//将s归零,用于下次求和运算
}
}

时间: 2024-08-09 06:20:52

杭电1001的相关文章

acm入门 杭电1001题 有关溢出的考虑

最近在尝试做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

杭电 1001

#include<stdio.h> int main(){ int i; int n; int sum; while(scanf("%d",&n) != EOF){ sum = 0; for(i = 1;i <= n; ++ i){ sum += i; } printf("%d\n\n", sum); } return 0; } 注意: 题中要求的是follow by a blank line 所以在输出的时候需要输出两次\n\n

杭电1001 Sum Problem

先看题目 这题,按照题目意思,我理解,应该是先是输入,每组输入一行,然后是对应的各个输出,输出间多加一条空白行,但是问题是怎么就知道他已经输入完毕了,下面应该显示输出.受题目输入输出事例误解.后来改为了一行输出,紧接着就是这行的输出,然后再是输入,输出,这样交替着. #include<iostream> using namespace std; int Sum(int); int main(){ int inputA; while(cin>>inputA) { cout<&l

杭电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

HDU 4907 (杭电BC#3 1001)Task schedule(水)

题目地址:HDU 4907 水题...不多说..哈希后从后往前遍历一遍就行了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include<a

杭电oj 1001

#include<iostream> using namespace std; int main() { int n = 0, sum; while (cin>>n) { sum = 0; // 这里要清零 for (int i =0; i <= n; i++) { sum += i; } cout << sum <<endl <<endl; } return 0; } 杭电oj 1001

杭电2018多校第一场(2018 Multi-University Training Contest 1) 1001.Maximum Multiple (HDU6298)-数学思维题(脑子是个好东西,可惜我没有)

暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.Maximum Multiple 题目意思就是给你一个n,找出来三个数x,y,z, 使得n=x+y+z,而且x,y,z都是n的因数,并且x*y*z为最大值,让你输出来x*y*z的最大值.如果没有满足条件的情况就输出-1. 由1=1/2+1/3+1/6=1/3+1/3+1/3=1/2+1/4+1/4,所

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

杭电 1087 Super Jumping! Jumping! Jumping!

http://acm.hdu.edu.cn/showproblem.php?pid=1087 Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21884    Accepted Submission(s): 9589 Problem Description Nowaday