大家好,又见面了,我是你们的朋友全栈君。
1 输入输出重定向
FileInputStream inputStream = new FileInputStream("输入文件名");
System.setIn(inputStream);
//这个放在下面
Scanner input = new Scanner(System.in);
PrintStream outputStream = new PrintStream("输出文件");
System.setOut(outputStream);
Scanner input = new Scanner(System.in);
2 ACM中java快速输入输出
P3367 【模板】并查集:https://blog.csdn.net/qq_40794973/article/details/103204372
java的Scanner 比较慢(出了名的了),如果需要输入数量级的数据并输出同样数量级的数据,使用Scanner和System.out耗时将很可能超过1s
2.1 使用 StreamTokenizer 和 PrintWriter
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
2.1.1 判断是否到达文件结尾
Java的while(scanner.hasNext()),或者C的while(scanf(“%d”,&n)!=EOF)
while (in.nextToken() != StreamTokenizer.TT_EOF) {
//
}
import java.io.*;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws IOException {
// io流包装
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//StreamTokenizer.TT_EOF这个是个参数,就是EOF
while (in.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) in.nval;
// 记录出现次数
HashMap<Integer, Integer> appears = new HashMap<>();
int max = 0;
for (int i = 0; i < n; i++) {
in.nextToken();//指向下一个
int num = (int) in.nval;
if (appears.containsKey(num)) {
Integer tempNum = appears.get(num);
appears.replace(num, tempNum + 1);
if (tempNum >= (n + 1) / 2) {
max = num;
}
}else {
appears.put(num, 1);
}
}
out.println(max);
out.flush();//刷新,不然max会留在缓冲区
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141672.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...