我想解決一個問題,但獲取內存限制 我認爲這是輸出前緩衝的原因 我有整數,我如何以最小的內存使用量輸出它們? 現在我正在使用PrintWriter。還有其他更好的方法嗎?java輸出的最小內存使用情況
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++;
}
}
}
}
你的問題是什麼?代碼是做什麼的?你能給出更多的上下文嗎?如果你需要低內存佔用,你選擇了錯誤的語言。 – Augusto
[問題1220](http://acm.timus.ru/problem.aspx?space=1&num=1220) – MAB
閱讀標題後,顯然您選擇了錯誤的語言「內存限制:0.75 MB」。您需要在不使用虛擬機運行的情況下構建它。 – Augusto