您的位置:首页 > 编程语言 > Java开发

PAT 乙级 1058 选择题(20) Java版

2016-09-27 21:26 387 查看

由于之前此题java超时,最后曲线救国,刷了无数题之后,找到了一个java快速io

源码先贴出来 已ac

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {

static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException{in.nextToken(); return (int) in.nval;}
static String next() throws IOException{in.nextToken(); return in.sval;}

public static void main(String[] args) throws IOException {
int gradePb[], gradePs = 0, n, m, wrongCnt[];
int maxCnt = 0;
String str, ansStr[];
StringBuilder sb = new StringBuilder();
char ch;
n = nextInt();
m = nextInt();
gradePb = new int[m];
wrongCnt = new int[m];
ansStr = new  String[m];

int a, b, c, ttemp;
for(int i = 0; i < m; i++)
{
a = nextInt();
b = nextInt();
c = nextInt();
ttemp = c;
sb.append(c);
while(ttemp-- > 0)
{
str = next();
sb.append(str);
}
ansStr[i] = sb.toString();
gradePb[i] = a;
sb.delete(0, sb.length());

}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
in.nextToken();
while((char)in.ttype != ')')
{
if(in.ttype == StreamTokenizer.TT_NUMBER) sb.append((int)in.nval);
if(in.ttype == StreamTokenizer.TT_WORD) sb.append(in.sval);
in.nextToken();

}
str = sb.toString();
sb.delete(0, sb.length());
if(ansStr[j].equals(str)) gradePs+=gradePb[j];
else wrongCnt[j]++;
}
System.out.println(gradePs);
gradePs = 0;
}
for(int i = 0; i < m; i++)
{
if(maxCnt < wrongCnt[i]) maxCnt = wrongCnt[i];
}
if(maxCnt > 0)
{
System.out.print(maxCnt);
for(int i = 1; i <= m; i++)
{
if(wrongCnt[i-1] == maxCnt)
System.out.print(" "+i);
}
}
else
{
System.out.print("Too simple");
}
//      System.out.println("all time is :"+(end-start)+"ms");
}
}


思悟

提交次数就不说了。。。简直要怀疑人生了。用尽各种javaIO的操作,优化再优化,无奈,java无法像c/c++可以非常便捷轻快的边读取数据边处理,但是不得不说javaIO的操作还是很舒服的,如果没有这么大时间消耗的话会更好吧!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 源码 string class