【22年真题】这是一道并不完美的题解,还有很多纰漏
但已经是我的极限了...
记录一下
import java.io.StreamTokenizer;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Main{
public static void main(String[] args)throws IOException {
Read r = new Read();
int N = r.NextInt();
Deque<Integer> deque_tuisongqi = new ArrayDeque<Integer>(N);
int M = r.NextInt();
Deque<Integer> deque_hezi = new ArrayDeque<Integer>(M);
int K = r.NextInt();
Deque<Integer> deque_songzhi = new ArrayDeque<Integer>(M);
for (int i = 0; i < N; i++) {
int a = r.NextInt();
deque_tuisongqi.addLast(a);
}
while(!deque_tuisongqi.isEmpty() || !deque_hezi.isEmpty()) {
if(!deque_tuisongqi.isEmpty() && deque_songzhi.size()==0) {
if(!deque_hezi.isEmpty()) {
int a = deque_hezi.getFirst();
deque_songzhi.addFirst(a);
deque_hezi.removeFirst();
}else {
int a = deque_tuisongqi.getFirst();
deque_songzhi.addFirst(a);
deque_tuisongqi.removeFirst();
}
continue;
}
if(deque_tuisongqi.getFirst()>deque_songzhi.getFirst()){
deque_hezi.addFirst(deque_tuisongqi.getFirst());
deque_tuisongqi.removeFirst();
}
else if(!deque_hezi.isEmpty() && deque_hezi.getFirst()<=deque_songzhi.getFirst()) {
deque_songzhi.add(deque_hezi.getFirst());
deque_songzhi.removeFirst();
deque_hezi.removeFirst();
}
else if(deque_tuisongqi.getFirst()<=deque_songzhi.getFirst()) {
deque_songzhi.addFirst(deque_tuisongqi.getFirst());
deque_tuisongqi.removeFirst();
}
//手中的松枝干上已经插满了松针,将之放到成品篮里,开始下一根松枝的制作。
if(deque_songzhi.size()==K) {
Iterator<Integer> iterator = deque_songzhi.descendingIterator();
while (iterator.hasNext()) {
int num = iterator.next();
System.out.print(num);
if (iterator.hasNext()) {
System.out.print(" ");
}
}
System.out.println();
deque_songzhi.clear();
}
//小盒子中最上面的松针不满足要求,但推送器上已经没有松针了。此时将手中的松枝放到成品篮里,开始下一根松枝的制作。
else if(!deque_hezi.isEmpty() && deque_hezi.getFirst()>deque_songzhi.getFirst() && deque_tuisongqi.size()==0){
Iterator<Integer> iterator = deque_songzhi.descendingIterator();
while (iterator.hasNext()) {
int num = iterator.next();
System.out.print(num);
if (iterator.hasNext()) {
System.out.print(" ");
}
}
System.out.println();
deque_songzhi.clear();
}
//小盒子已经满了,但推送器上取到的松针仍然不满足要求。此时将手中的松枝放到成品篮里,推送器上取到的松针压回推送器,开始下一根松枝的制作。
else if(deque_hezi.size()==M && deque_tuisongqi.getFirst()>deque_songzhi.getFirst()) {
for (int i = 0; i < M; i++) {
deque_tuisongqi.addFirst(deque_hezi.getLast());
deque_hezi.removeLast();
}
Iterator<Integer> iterator = deque_songzhi.descendingIterator();
while (iterator.hasNext()) {
int num = iterator.next();
System.out.print(num);
if (iterator.hasNext()) {
System.out.print(" ");
}
}
System.out.println();
deque_songzhi.clear();
deque_hezi.clear();
}
//双空
else if(deque_hezi.isEmpty() && deque_tuisongqi.isEmpty()) {
Iterator<Integer> iterator = deque_songzhi.descendingIterator();
while (iterator.hasNext()) {
int num = iterator.next();
System.out.print(num);
if (iterator.hasNext()) {
System.out.print(" ");
}
}
System.out.println();
deque_songzhi.clear();
}
continue;
}
}
}
class Read{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public int NextInt()throws IOException {
st.nextToken();
return (int)st.nval;
}
}
标签:deque,JAVA,插松枝,iterator,int,hezi,System,L2,songzhi
From: https://blog.csdn.net/Yoonaling520/article/details/137565859