杭电ACM A+B for Input-Output Practice (I-VIII))C语言答案解析

今天做了一些杭电ACM的题,不得不说,做了这些题才发现自己实在基础太过薄弱了,还是得多加练习。在这里分享一下自己的答案吧。


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

#include

int main()

{
	int a, b;

	while (scanf("%d%d", &a, &b) != EOF)//需要多次验证==也可以写成while (scanf("%d %d", &a,&b) == 2)

		printf("%d\n", a + b);

	return 0;
}

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

#include
	
int main()
{
	int a, b, n;
	int i = 0;
	scanf("%d", &n);

	while (i < n)
	{
		scanf("%d %d", &a, &b);

		printf("%d\n", (a + b));

		i++;
	}return 0;
}

1.1.3A+B for Input-Output Practice(III)

#include

int main()

{
	int a, b;
	
	while (scanf("%d %d",&a,&b) == 2)
	{
		if (a == b && a == 0)exit();
		printf("%d\n", (a + b));
	}
	return 0;
}

1.1.4A+B for Input-Output Practice(IV)

#include
int main()

{
	int n, i;

	int sum=0;
	while(1)
	{
	scanf("%d", &n);

	if (n == 0)exit();

	while (n != 0)
	{
		scanf("%d", &i);
		sum = sum + i;
		n--;
	}
	printf("%d\n", sum);
	sum = 0;
}
	return 0;
}

1.1.5A+B for Input-Output Practice(V)

#include
int main()

{
	int n, i,m;

	int sum = 0;

	scanf("%d",&m);

	while (m>0)
	{
			scanf("%d",&n);
			while (n != 0)
			{
				scanf("%d",&i);
				sum = sum + i;
				n--;
			}
			printf("%d\n", sum);
			sum = 0;
			m--;
	}
	return 0;
}

1.1.6A+B for Input-Output Practice(VI)

#include
int main()

{
	int n, i = 0;

	int sum = 0;
	while (scanf("%d", &n) != EOF)
	{
		while (n > 0)
		{
			scanf("%d", &i);
			sum = sum + i;
			n--;
		}
		printf("%d\n", sum);
		sum = 0;
	}
	return 0;
}

1.1.7A+B for Input-Output Practice(VII)

#include//无意义的弱智抠字眼题
int main()

{
	int a, b;
	while (scanf("%d %d", &a, &b) != EOF)
		printf("%d\n\n", (a + b));
	return 0;
}

1.1.8A+B for Input-Output Practice(VIII)

#include
int main()
{
    int n, a[100], sum, m, i;
    scanf("%d", &n);
    while (n--)
    {
        sum = 0;
        scanf("%d", &m);
        for (i = 1; i <= m; i++)
        {
            scanf("%d", &a[i]);
            sum = sum + a[i];
        }
        if (n != 0)
            printf("%d\n\n", sum);
        if (n == 0)
            printf("%d\n", sum);
    }
}

 

你可能感兴趣的:(c语言,acm竞赛)