写算法时,总会遇到这种测试例子java
例如:c++
输入 输入包括多组数据。以n等于0结束 每组数据中: 第一行为一个整数n,表示整数的数量。 第二行输入n个整数。 全部输入的数均小于100000。 输出 输出答案。 样例输入 5 1 2 3 3 2 7 1 2 3 4 5 5 6 0 样例输出 1 5
则能够这样实现:算法
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc =new Scanner(System.in); while(true){ int n =sc.nextInt(); if(n == 0){ break; } //...这里为单组测试数据 } } }
输入windows
输入包含多组数据,以EOF结束,测试
每组首先输入三个正整数,n,m,k,其中(n,m,k<100)unix
后输入n个数表示每种零食的价格ai(ai<1000)blog
输出io
输出一个正整数,表示最小花费class
样例输入import
4 3 2
1 2 3 4
7 3 8
1 2 3 4 5 6 7
样例输出
8
21
Java中实现以EOF结束:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { //...这里为单组测试数据 } sc.close(); } }
c或c++中实现以EOF结束:
#include <stdio.h> int main(){ int n; while (scanf("%d",&n)!=EOF){ //循环读入n,直到文件末位 //...这里是单组测试数据 } return 0; }
在windows平台,通常模拟EOF的输入是在一个新行的开头输入ctrl + z
在unix环境下,是在一个新行的开始出输入ctrl + D
在MacOS下,须要在一个新行的开始输入control+Q,再control + D