菜鸟的HDOJ1089~1096练习(部分)

做一下OJ入菜鸟题。部分emmm没什么收获的就不放上来了

HDOJ1089

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

错了几次,想起来之前朋友的博客说不是简单的进行一次加法运算。尝试以后做了以下:

#include <stdio.h>
int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
	while(getchar()!=EOF){
		printf("%d\n",a+b);
		scanf("%d %d",&a,&b);
	}
	return 0;
}

HDOJ1090

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
一开始以为要求是一次性输入所有要计算的数据,再一次性输出所有数据和。然后就想用数组实现。然后就CE了。。。因为静态数组长度不能为变量(ps:这种东西dev居然可以跑···)。看了朋友的博客发现并不是这样。他说oj上数据输入流和输出流是分开的。好吧。

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

    return 0;
}

想了想觉得用动态数组应该还是可以实现的,就搜了一下动态数组的用法。(参考博客https://blog.csdn.net/zhanshen112/article/details/80758850)
我的依葫芦画瓢:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,i;
    int *array,*b;
    scanf("%d",&n);
    array=(int*)calloc(n,sizeof(int));
    b=(int*)calloc(n,sizeof(int));
    for(i=0;i<n;i++)
    {
         scanf("%d %d",&array[i],&b[i]);
    }
    for(i=0;i<n;i++)
    {
        printf("%d\n",array[i]+b[i]);
    }
    free(array);
    free(b);//释放第一维指针 
    return 0;
}

emmmm后者跑得快很多是为什么?

HDOJ1094

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<stdio.h>
int main(){
	int i,n,a,sum;
	while(scanf("%d",&n)==1){
		sum=0;
		for(i=0;i<n;i++){
			scanf("%d",&a);
			sum=sum+a;
		}
		printf("%d\n",sum);
	} 
    return 0;
    }

这题我一直没有明白,它的“ Each case starts with an integer N, and then N integers follow in the same line”,那个in the same line究竟怎么实现。原来实际输入的时候不用同行输入也可以。可是emmm题目和Sample Input怎么可以这样描述呢

HDOJ1096

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

PE一趟:

#include<stdio.h>
int main(){
	int n,m,i,a,sum,j;
	while(scanf("%d",&n)==1){
		for(j=0;j<n;j++){
			scanf("%d",&m);
			sum=0;
			for(i=0;i<m;i++){
				scanf("%d",&a);
				sum=sum+a;
			}
			printf("%d\n\n",sum);
		}
	}
    return 0;
    }

原来是题目没有读清楚。。要求输出的空行是between outputs,也就是说最后一个sum的输出是不输出空行的。

#include<stdio.h>
int main(){
	int n,m,i,a,sum,j;
	while(scanf("%d",&n)==1){
		for(j=0;j<n;j++){
			scanf("%d",&m);
			sum=0;
			for(i=0;i<m;i++){
				scanf("%d",&a);
				sum=sum+a;
			}
			printf("%d\n",sum);
			if (j<n-1)
				printf("\n");
		}
	}
    return 0;
    }
发布了24 篇原创文章 · 获赞 10 · 访问量 5002

猜你喜欢

转载自blog.csdn.net/weixin_44559752/article/details/88799964