赞
踩
刚接触牛客网的在线编程,和IDE上开发还是有些许不同的。特开一文,记下自己碰到的坑。
1.必须将类名写成Main。
2.需要自己引入包import。不然会编译失败。
下面是牛客网发布的输入输出的一些总结。
预先不输入数据的组数——读到文件结尾
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
预先知道数据组数——读数据组数然后循环
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);
}
只有一组数据直接读
int a = scanner.nextInt();
不需要输出case数
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
要输出case数
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("Case"+(i+1)+" "+(a+b));
}
每个case之后要有空行
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("Case"+(i+1)+" "+(a+b)+"\n");
}
两个case之间有空行
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();
if(i>0){
System.out.println();
}
System.out.println("Case"+(i+1)+" "+(a+b)+"\n");
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。