【HDOJ】ACM steps 题解目录

整理了一下当时做ACM steps的题解。

写几个关键字让爬虫过来:HDOJ ACM steps 题解 解答 答案

放在这里做成目录:

【ACM】ACM steps 1.1.8

桃花潭水深千尺,不及AC送我情~
A+B for Input-Output Practice (VIII)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40850 Accepted Submission(s): 12244

Problem Description
Your task is to calculate the sum of some integers.

Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.

Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output
10

15

6
解:

#include
void main()
{
int n,i,j,t,sum,a,out[1000],q;
sum=0;
scanf(“%dn”,&n);
for (i=0;i<n;i++)
{
scanf(“%d”,&j);
for (t=0;t<j;t++)
{
scanf(“%d”,&a);
sum=sum+a;
out[i]=sum;
}
sum=0;
}
q=n-1;
for (i=0;i<q;i++)
{
printf(“%dnn”,out[i]);
}
printf(“%dn”,out[q]);
}

唔,写的比较麻烦。一开始试提示有格式错误.检查了一下,发现是最后面多了一行空行,RT。

当时源代码是这样的:
后来没有很好的解决方法,于是就用了一个笨办法:
于是就变成了这样:
但是HDOJ还是提示格式错误,于是突发奇想在最后的输出那里加了一个n。于是就AC了……
格式错误的原因是最后没有换行……
感觉这个代码好乱,又整理了另外的一个写法:
#include
int main()
{
    int a,b,i,j,l[1000],k;
    scanf(“%d”,&i);
    getchar();
    for(j=1;j<=i;j++)
        l[j]=0;
    for(j=1;j<=i;j++)
    {
        scanf(“%d”,&a);
        getchar();
        for(k=1;k<=a;k++)
        {
            scanf(“%d”,&b);
            getchar();
            l[j]+=b;
        }
    }
    for(j=1;j<=i-1;j++)
        printf(“%dnn”,l[j]);
    printf(“%dn”,l[i]);
}

至此,ACM steps1 系列的题目全部AC~撒花~

【ACM】ACM steps 1.1.7

A+B for Input-Output Practice (VII)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19199 Accepted Submission(s): 12214

Problem Description
Your task is to Calculate a + b.

Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.

Sample Input
1 5
10 20

Sample Output
6

30
参考题解:

#include
void main()
{
int a,b,sum;
while(scanf(“%d%d”,&a,&b)!=EOF)
{
sum=a+b;
printf(“%dn”,sum);
printf(“n”);
}
}
 很简单的一道题……
但是有更简单的做法
#include
 main()
 {
    int a,b;
   while(scanf(“%d%d”,&a,&b)!=EOF)
   printf(“%dnn”,a+b);
 }

这貌似是这道题最简单的做法了……

【ACM】ACM steps 1.1.6

A+B for Input-Output Practice (VI)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18437 Accepted Submission(s): 12465

Problem Description
Your task is to calculate the sum of some integers.

Input
Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.

Output
For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.

Sample Input
4 1 2 3 4
5 1 2 3 4 5

Sample Output
10
15
参考题解:

#include
int main()
{
int n,i,sum,t;
sum=0;
while(scanf(“%d”,&n)!=EOF)
{
for(i=0;i

这道题纠结了好久……一开始的写法是这样的,但是一直没有找到错误。

#include
int main()
{
int n,i,sum,t;
sum=0;
while(scanf(“%d”,&n)!=EOF);
{
for(i=0;i

但是HDOJ一直提示WA,也没有找到那里有错误。请教了一下学长,找了好久终于发现while语句后面不应该有分号,否则不会有任何输出…………

鲜血的教训啊……

另附一个正确的写法:

#include
void main()
{
int n,a,i,sum;
while(scanf(“%d”,&n)!=EOF)
{
sum=0;
for(i=0;i
	

【ACM】ACM steps 1.1.5*

A+B for Input-Output Practice (V)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18072 Accepted Submission(s): 13110

Problem Description
Your task is to calculate the sum of some integers.

Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Sample Input
2
4 1 2 3 4
5 1 2 3 4 5

Sample Output
10
15

Author
lcy

参考题解:

#include
int main()
{
    int n,a,b,i,j,sum;
    sum=0;
    while(scanf("%dn",&n)!=EOF)
    {
        for(i=0;i

唔,九点了。感觉有点不在状态。本来打算今天刷完第一系列的,现在看看能刷到多少吧……
这道题也不是很难,尤其是有前一题做铺垫,注意有时候该写注释的写注释,定义的变量太多的话自己会晕掉的= =  。还有就是要注意最后还要对sum进行初始化。

【ACM】ACM steps 1.1.4*

A+B for Input-Output Practice (IV)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25266 Accepted Submission(s): 13962

Problem Description
Your task is to Calculate the sum of some integers.

Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Sample Input
4 1 2 3 4
5 1 2 3 4 5
0

Sample Output
10
15

参考题解:

#include 
int main()
{
    int n,sum,i,t;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
            sum=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&t);
            sum=sum+t;
        }
        printf("%dn",sum);
    }
}

!=EOF的作用就是一直读到文件的末尾,还有就是要注意if(n==0) break;要直接放在while语句的下面。且这个语句后面要加分号。

【ACM】ACM steps 1.1.3

A+B for Input-Output Practice (III)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28263 Accepted Submission(s): 15357

Problem Description
Your task is to Calculate a + b.

Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input
1 5
10 20
0 0

Sample Output
6
30

解:

#include
 int main()
 {
    int a,b;
    scanf("%d %d",&a,&b);
    while(!(a==0&&b==0))
    {
    printf("%dn",a+b);
     scanf("%d %d",&a,&b);
    }
 }
华丽丽的一次过,没什么好说的……貌似之前的两题在wp上发表的时候格式有点问题……已经修正这个问题了~

【ACM】 ACM steps 1.1.2

1.1.2 A+B for Input-Output Practice (II)

 

Problem Description

Your task is to Calculate a + b.

 

Input

Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

2

1 5

10 20

Sample Output

6

30

Author

lcy

Recommend

JGShining

解:

#include
#define M 1000
int main()
{
int a,b,i,j,sum[M];//一维数组来解
scanf("%d",&i);
for(j=0;j

简单的一维数组的应用,注意while语句不要直接把自增循环写进去,这道题又没有一次过,原因又是低级错误,一开始我还以为HDOJ不能把注释写进去呢……

【ACM】ACM steps 1.1.1

从今天开始做steps上面的题目了。大体看了一下难度,感觉不是很难,现在都是一些A+B的题目,争取一次过掉。把源码发在这里,再写几句解题心得什么的……

1.1.1A+B for Input-Output Practice (I)

 

Problem Description

Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

 

 

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5

10 20

Sample Output

6

30

Author

lcy
参考代码:

#include
 int main()
 {
    int a,b;
   while(scanf("%d%d",&a,&b)!=EOF)
   printf("%dn",a+b);
 }

 

最简单的题目了,没什么好说的,注意while语句后面不要加分号,一开始居然错在这上面了………………