合并鏈表
//已知兩個鏈表head1 和head2 各自從小到大有序,請把它們歸并成一個鏈表依然從小到大有序
class Node{
int data
Node next
}
Node Merge(Node head1 , Node head2) {
//TODO
}
多執行緒
1、吃桔子
共計9個桔子,有3個小朋友,小朋友A每次拿2個桔子,小朋友B每次拿3個桔子,小朋友C每次拿1個桔子,小朋友10s吃1個桔子,吃完后繼續去拿。
小朋友每次拿桔子之前和拿了桔子之后,都會對桔子數量進行報數。如果剩余的桔子不夠小朋友每次拿的數量,小朋友停止拿桔子,喊一聲“不拿了”并退出游戲。
請用java多執行緒程式表述上面的程序。
2、跑步
3個小朋友參加百米跑步比賽,A小朋友每秒跑5米,B小朋友每秒跑4米,C小朋友每秒跑3米。大家做好準備之后喊一聲“我準備好了”,然后在聽到發令槍響后一起起步,并在開始跑步時喊一聲“我開始跑步了”,當一個小朋友跑到終點時,喊一聲“我到終點了”,此時記錄跑了多久。其他小朋友停止跑步,并報出自己跑了多宣告,例如“x小朋友跑了yy米”。
請用java程式表述上面的程序。
3、分析字串
寫字串的分析函式,分析一個由字母和數字組成的字串,例如aBc12D9
1、輸出字串中字符和數字的個數。
2、相連的數字不能分為2個,即12是作為一個數字統計,輸出字串中字符和數字的個數。
3、字符大小寫不區分,統計字符的個數及出現次數。
4、統計出現次數最多的字符和數字。
按照上面的要求,寫4個函式
4、洗掉字符
要求:洗掉一個字串中出現次數最多的字符,如果多個字符出現次數一樣,則都洗掉。要求字符順序不變。
public class RemoveMostCharTest {
@Test
public void testRemoveMostChar(){
String source = "ABCCDEER";
String result = "ABDR";
Assert.assertEquals(result, getRightString(source));
}
/**
* 洗掉字符
* 洗掉一個字串中出現次數最多的字符,如果多個字符出現次數一樣,則都洗掉。要求字符順序不變。
* @param sorource
* @return
*/
private String getRightString(String source) {
return null;
}
}
uj5u.com熱心網友回復:
合并鏈表
public static Node Merge(Node head1 , Node head2) {
Node node1;
Node node=new Node();
node1=node;
while(head1!=null&&head2!=null){
if(head1.data>head2.data){
node.next=head2;
node=node.next;
head2=head2.next;
}else{
node.next=head1;
node=node.next;
head1=head1.next;
}
}
if(head1==null)
node.next=head2;
if(head2==null)
node.next=head1;
return node1.next;
}
@Test
public void Testnode(){
Node node = new Node(new Node(new Node(5),4),3);
Node node1 = new Node(new Node(new Node(7),5),3);
Node merge = Merge(node, node1);
while (merge!=null){
System.out.println(merge.data);
merge=merge.next;
}
}
class Node{
int data;
Node next;
public Node(){}
public Node(int id){this.data=https://bbs.csdn.net/topics/id;}
public Node(Node n,int id){this.next=n;this.data=https://bbs.csdn.net/topics/id;}
}
uj5u.com熱心網友回復:
吃桔子 我用單元測驗 執行緒一休眠就退出了 才知道不可以用這個啊 。。。
還是用main吧
public static void main(String[] args) {
Data data = new Data();
data.setCount(9);
for (int i=1;i<4;i++){
int finalI = i;
new Thread(()-> {
while(true){
if(data.getCount()< finalI){
System.out.println("不拿了");
return;
}
synchronized (data){
if(data.getCount()< finalI){
System.out.println("不拿了");
return;
}
data.setCount(data.getCount()-finalI);
System.out.println("拿到了個"+finalI+"橘子");
System.out.println("還有"+data.getCount());
}
try {
TimeUnit.SECONDS.sleep(finalI*1);
System.out.println("吃了"+finalI+"橘子");
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
}
class Data{
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
uj5u.com熱心網友回復:
跑步 我感徑訓是有問題 只是改變一下輸出陳述句的地方 額 。。。。
public static volatile boolean flag=true;
public static void main(String[] args) {
for (int i=3;i<6;i++){
int finalI = i;
new Thread(()-> {
Data data = new Data();
while(data.getCount()<100&&nospringtest.flag){
synchronized (Thread.class){
if (!nospringtest.flag)
break;
data.setCount(data.getCount()+finalI);
System.out.println(Thread.currentThread().getName()+"跑了"+data.getCount()+"還有"+(100-data.getCount())+"米");
if(data.getCount()>=100){
System.out.println(Thread.currentThread().getName()+"跑完了");
nospringtest.flag=false;
return;
}
}
try {
TimeUnit.SECONDS.sleep(1);
}catch (Exception e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"跑了"+data.getCount()+"還有"+(100-data.getCount())+"米");
}).start();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/29508.html
標籤:Java EE
上一篇:springboot validation校驗引數如何同時支持分組校驗和順序校驗
下一篇:關于react的!!!求指點
