import java.util.Scanner;
class queue {
int len;
int outnumber;
int innumber;
int staynumber=len-outnumber;
int Queue[]=new int[len];
int Innumber[]=new int[innumber];
int Outnumber[]=new int[outnumber];
public void queuelength() {
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
}
public void inqueue(){
for(int i=0;i<len;i++){
Scanner sc=new Scanner(System.in);
Queue[i]=sc.nextInt();
}
}
public void setOutnumber(){
System.out.print("out ");
Scanner sc=new Scanner(System.in);
outnumber=sc.nextInt();
}
public void setInnumber(){
System.out.print("in ");
Scanner sc=new Scanner(System.in);
for(int i=0;i<innumber;i++){
Innumber[i]= sc.nextInt();
}
}
public void changequeue(){
if(innumber>outnumber){len+=innumber=outnumber;}
for(int i=0;i<outnumber;i++)
Queue[i]=Queue[i+outnumber];
for(int i=staynumber;i<len;i++){
Queue[i]=Innumber[i-staynumber];
}
for(int i=0;i<outnumber;i++){
Outnumber[i]=Queue[i];
}
}
public void printqueue(){
System.out.print("len= "+len+",data= ");
for(int i=0;i<len;i++){
System.out.print(Queue[i]+" ");
}
System.out.print("\n");
System.out.print("len= "+outnumber+",data= ");
for(int i=0;i<=outnumber;i++){
System.out.print(Outnumber[i]+" ");
}
}
}
public class queuetest {
public static void main(String[] args) {
queue queue1 = new queue();
queue1.queuelength();
queue1.inqueue();
queue1.setOutnumber();
queue1.setInnumber();
queue1.changequeue();;
queue1.printqueue();;
}
}
java OJ题,写了一个下午没结果,已经裂开,求解答
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- CSDN专家-sinJack 2022-04-29 12:56关注
import java.util.ArrayList; import java.util.Scanner; class Queue { public ArrayList<Integer> list=new ArrayList<>(); public ArrayList<Integer> outList=new ArrayList<>(); public void inQueue(int i){ this.list.add(i); } public ArrayList outQueue(int len){ int outLen=this.list.size()<len?this.list.size():len; for(int j=0;j<outLen;j++){ outList.add(list.get(j)); } for(int j=outLen-1;j>=0;j--){ list.remove(j); } return outList; } } public class Main { public static void main(String[] args) { Queue q=new Queue(); Scanner scan = new Scanner(System.in); String line = scan.nextLine(); int len = Integer.parseInt(line); String[] s = scan.nextLine().split(" "); for (String str:s) { q.inQueue(Integer.parseInt(str)); } String line1 = scan.nextLine(); String line2 = scan.nextLine(); int outNum = 0; ArrayList out=new ArrayList(); if(line1.startsWith("in")){ String[] ss = line1.split(" "); for(int i=1;i<ss.length;i++){ q.inQueue(Integer.parseInt(ss[i])); } }else if(line1.startsWith("out")){ outNum= Integer.parseInt(line1.split(" ")[1]); out=q.outQueue(outNum); } if(line2.startsWith("in")){ String[] ss = line2.split(" "); for(int i=1;i<ss.length;i++){ q.inQueue(Integer.parseInt(ss[i])); } }else if(line2.startsWith("out")){ outNum= Integer.parseInt(line2.split(" ")[1]); out=q.outQueue(outNum); } System.out.print("len = "+ q.list.size()); if(q.list.size()>0){ System.out.print(", data ="); for(int i : q.list){ System.out.print(" "+i); } } System.out.println(); System.out.print("len = "+ out.size()); if (out.size()>0){ System.out.print(", data ="); for(int i=0;i<out.size();i++){ System.out.print(" "+out.get(i)); } } scan.close(); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
- ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
- ¥15 FileNotFoundError 解决方案
- ¥15 uniapp实现如下图的图表功能
- ¥15 u-subsection如何修改相邻两个节点样式
- ¥30 vs2010开发 WFP(windows filtering platform)
- ¥15 服务端控制goose报文控制块的发布问题
- ¥15 学习指导与未来导向啊
- ¥15 求多普勒频移瞬时表达式
- ¥15 如果要做一个老年人平板有哪些需求