『ACM入门』蓝桥杯ACM训练系统基本输入输出教程

『ACM入门』蓝桥杯ACM训练系统基本输入输出教程

在介绍训练场的OJ系统之前,首先为大家介绍一下ACM:

ACM原代表美国计算机协会,因其举办的ICPC即国际大学生程序设计竞赛而闻名全世界,此项赛事要求学生的在五小时内解决全英文问题,并在效率和速度以及代码的审查上要求非常严格以至近乎苛刻,被誉为是计算机界的“奥林匹克”。在大学中,因其含金量、认可度等非常之高,故而在大学生名企就业、保研、留学等方面都有着极大的帮助。ACM也因其独有的比赛趣味也在今天的高校中也得到了广泛的推广,许多大学生都为之着迷、甚至大学四年都为之献身。足以说明ACM的魅力所在。

OJ简介:

ACM比赛中主要以OJ(即Online Judge)判题为主,用来在线检测程序的正确性。OJ采用后台黑箱测试,测试数据非常全面,涵盖各种特殊情况。并且在结果的比对上也不放过一个空格和回车,这就要求程序员要有非常严谨的思维。著名的OJ有POJ、HOJ、UVA等。

轻量级入门OJ ACM训练平台:http://www.dotcpp.com

在各大OJ的ACM比赛赛题上,往往都会给出问题的描述(Description)、问题的输入和输出要求,并会给出几组样例数据。所以选手要在完全理解的基础上至少通过了样例数据才再提交代码。

下面介绍几种常见的输入输出格式。

A+B Ⅰ

这种输入的典型题目就是A+B
A+B for Input-Output Practice (I)

时间限制: 1Sec 内存限制: 64MB

题目描述
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
输入
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
输出
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.
样例输入
1 5
10 20
样例输出
6
30

此题只要求用户求A+B的和,但此类题目往往说明测试数据有多组。则默认是到文件(后台测试用例在文件里)末尾结束。我们则可以采用循环的方式不断接收测试用例,并且每接收一组输出一组(不必全部输入再全部输出,因为OJ只比对一次最后的结果)。

参考C代码:

#include<stdio.h>
int main()
{
 int a,b;
 while(scanf(“%d%d”,&a,&b)==2) //利用scanf的返回值
 {
 printf(“%d\n”,a+b);
 }
 return 0;
}

参考Java代码:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			System.out.println(a+b);	
		}	
	}
}

A+BⅡ

依然是A+B,我们可以看一下这道题
A+B for Input-Output Practice (II)

时间限制: 1Sec 内存限制: 64MB

题目描述
The first line integer means the number of input integer a and b. Your task is to Calculate a + b.
输入
Your task is to Calculate a + b. The first line integer means the numbers of pairs of input integers.
输出
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.
样例输入
2
1 5
10 20
样例输出
6
30

依然是求A+B,但它的输入数据为:

2

1 5

10 20

输出为:

6

30

此题相比第一道而言,会提前告诉你是几组数据,第一行的2就表示有两组数据。则此时我们可以考虑这样写:
C:

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

Java:

while

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
	
		Scanner scanner = new Scanner(System.in);
		int n  = scanner.nextInt();
		while(n!=0) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			System.out.println(a+b);	
			n--;
		}
	}
	
}

for

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
	
		Scanner scanner = new Scanner(System.in);
		int n  = scanner.nextInt();
		for(int i=0;i<n;i++) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			System.out.println(a+b);
			}
	}
}

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

时间限制: 1Sec 内存限制: 64MB

题目描述
Your task is to Calculate a + b.
输入
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.
输出
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.
样例输入
1 5
10 20
0 0
样例输出
6
30

继续是A+B,不同的是题目中说明,有多组数据,但是以A和B都为0时结束,则此时,就要做判断。不能依然输出0。

参考答案:

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

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

时间限制: 1Sec 内存限制: 64MB

题目描述
Your task is to Calculate the sum of some integers.
输入
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.
输出
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.
样例输入
4 1 2 3 4
5 1 2 3 4 5
0
样例输出
10
15

题目描述:此类题目是求n个数的和,输入为先输入一个数组n,然后后面跟n个数字,求这n个数字的和。同样有多组数据,当n=0时结束。
样例输入:

4 1 2 3 4

5 1 2 3 4 5

0

样例输出:

10

15

参考写法:

#include<stdio.h>
int main()
{
 int n;
 int sum,temp;
 while(scanf("%d",&n) && n)
 {
 sum=0;
 while(n--)
 {
 scanf("%d",&temp);
 sum+=temp;
 }
 printf("%d\n",sum);
 }
 return 0;
}

总结:

常见的ACM的输入输出格式如这些。

多组数据可能还会用到EOF、NULL这些宏。比如scanf、getchar、gets的返回值。

万变不离其宗,有些题目可能是这些格式的组合,亦或者是个别情况的变种,比如输出格式要求的变化等等,就需要各位ACMer灵活多变了。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM 输入输出主要是指在ACM编程竞赛中,处理输入和输出数据的方式。 在C#中,处理ACM输入输出通常需要使用Console.ReadLine()和Console.WriteLine()方法来获取输入和输出数据。通过使用这些方法,我们可以读取标准输入流中的数据并将结果输出到标准输出流中。 根据引用,在处理ACM输出时,如果题目要求在输出样例中每组样例之间有特定的分隔符(如换行或空格),我们可以在输出每组样例后判断是否是最后一组,如果不是则输出相应的分隔符,如果是最后一组则直接结束。 根据引用,在处理ACM输出时,我们可以使用一个布尔变量来判断是否是第一组输出。如果是第一组,则输出后将布尔变量设置为false;则,输出每组样例前先输出分隔符,并输出每组样例。 以下是一个示例代码,演示了如何使用C#处理ACM输入输出: using System; using System.Collections.Generic; using System.Linq; namespace ACMExample { class Program { static void Main() { int n = int.Parse(Console.ReadLine()); bool isFirst = true; while (n-- > 0) { List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll(i => int.Parse(i)); if (isFirst) { isFirst = false; } else { Console.WriteLine(); } Console.WriteLine(a + a); } Console.ReadKey(); } } } 在这个示例中,我们首先读取一个整数n,表示有多少组输入。然后,使用一个布尔变量isFirst来判断是否是第一组输出。接下来的while循环中,我们使用Console.ReadLine()方法读取每组输入数据,并使用Console.WriteLine()方法输出每组数据的运算结果。最后,使用Console.ReadKey()方法来等待用户按下任意键以结束程序。 希望以上的解答能帮助到您。如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值