我正在尝试解决问题,但要获得内存限制 我认为这是输出前缓冲的原因 我有整数,我怎么能用最少的内存使用输出它们? 现在我正在使用PrintWriter。还有其他更好的方法吗?
import java.io.*;
import java.util.*;
class stack_ {
public char index;
public int value;
public stack_(char ind, int val) {
index = ind;
value = val;
}
}
public class timous {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer( new BufferedReader (new InputStreamReader(System.in)) );
Vector<stack_> numbers = new Vector<stack_>();
PrintWriter out = new PrintWriter(System.out);
in.nextToken();
char num = (char)in.nval;
for (char i = 0, k; i<num; i++) {
in.nextToken();
if (in.sval.equals("POP")) {
k = (char)(numbers.size()-1);
in.nextToken();
while (numbers.elementAt(k).index!=(char) in.nval)
k--;
out.println(numbers.elementAt(k).value);
numbers.removeElementAt(k);
}
else {
in.nextToken();
k = (char)in.nval;
in.nextToken();
numbers.add(new stack_(k, (int) in.nval));
}
}
out.flush();
}
}
内存限制测试3;时间0.078;内存834 KB
import java.io.*;
import java.util.*;
public class timous {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer( new BufferedReader (new InputStreamReader(System.in)) );
int[] numbers_int = new int[20000];
char[] numbers_char = new char[20000];
PrintWriter out = new PrintWriter(System.out,true);
in.nextToken();
char num = (char)in.nval;
for (char i = 0, k, cur=0; i<num; i++) {
in.nextToken();
if (in.sval.charAt(1)=='O') {
k = cur;
k--;
in.nextToken();
while (numbers_char[k]!=(char) in.nval)
k--;
out.println(numbers_int[k]);
for (; k<19999; k++){
numbers_int[k] = numbers_int[k+1];
numbers_char[k] = numbers_char[k+1];
}
cur--;
}
else {
in.nextToken();
numbers_char[cur] = (char)in.nval;
in.nextToken();
numbers_int[cur] = (int)in.nval;
cur++;
}
}
}
}
答案 0 :(得分:0)
创建缓冲区时,缓冲区是固定大小。这意味着无论您如何使用它或填充它,它都不会使用更多内存。默认值为8 KB,这不太可能导致内存不足。
它更有可能成为堆栈对象的Vector(一个集合的奇怪选择),尽管你必须创建数百万这些才会导致问题
如果没有至少1.1 MB的堆(这是此大小的程序的一小部分),BTW Java 7将无法启动。即你甚至无法打印它的版本。
$ java -mx1000k -version
Error occurred during initialization of VM
Too small initial heap for new size specified
$ java -mx1100k -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)
如果您想知道为什么Java“需要如此多的内存”,我会说现在大多数运行Java的系统都没有。您可以以200美元的价格购买32 GB,因此1 MB的价值仅为0.6美分且可以重复使用。
鉴于它确实与问题有关,您可以使用这些方法解析和输出int值。
static int readInt() throws IOException {
int num = 0, ch;
while ((ch = System.in.read()) > 0)
if (ch > ' ')
break;
if (ch < 0)
return -1;
do {
num = num * 10 + ch - '0';
ch = System.in.read();
} while (ch > ' ');
return num;
}
static void writeInt(int i) {
if (i == 0) {
System.out.write('0');
System.out.write('\n');
return;
} else if (i < 0) {
System.out.write('-');
writeInt(-i);
return;
}
int tens = 1000000000;
for (; tens > i; tens /= 10) ;
for (; tens > 0; tens /= 10)
System.out.write((char) ('0' + i / tens % 10));
System.out.write('\n');
}
public static void main(String... args) throws IOException {
int count = readInt();
for (int i = 0; i < count; i++) {
int pushPop = readInt();
switch (pushPop) {
case -1: // OEF
return;
case 36074: // PUSH
case 71626: // push
push(readInt(), readInt());
break;
case 3542: // PUSH
case 7094: // pop
writeInt(pop(readInt()));
break;
}
}
}