做了51的大佬求编程AC代码啊

第一次看到要写多线程,不知道咋输入啊







全部评论
第一题,直接输出2,ac了50%,这测试用例太玄学了
点赞 回复 分享
发布于 2017-09-18 21:03
虽然我也没写线程a了,但感觉没什么用,应该是考察多线程编程的,会去看的
点赞 回复 分享
发布于 2017-09-18 21:03
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.atomic.AtomicInteger;public class Main22 { private static AtomicInteger count = new AtomicInteger(0); private static final int THREAD_COUNT = 3; public static void main(String[] args) throws IOException, InterruptedException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; // Scanner scan = new Scanner(System.in); BlockingQueue<String> bq = new ArrayBlockingQueue<>(200009); while ((line = br.readLine()) != null) { if ("".equals(line)) break; bq.put(line); } bq.add("__QUIT__"); bq.add("__QUIT__"); bq.add("__QUIT__"); CountDownLatch cdl = new CountDownLatch(THREAD_COUNT); Runnable task = new Runnable() { public void run() { while (true) { try { String str = bq.take(); if("__QUIT__".equals(str)){ cdl.countDown(); break; } if (str.contains("u51")) { count.incrementAndGet(); } } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; Thread[] threads = new Thread[THREAD_COUNT]; for (int i = 0; i < THREAD_COUNT; i++) { threads[i] = new Thread(task); threads[i].start(); } cdl.await(); System.out.println(count.get()); }}
点赞 回复 分享
发布于 2017-09-18 21:28
不用写线程
点赞 回复 分享
发布于 2017-09-18 20:50
不用写线程,就是个噱头
点赞 回复 分享
发布于 2017-09-18 20:58
单线程一下a了
点赞 回复 分享
发布于 2017-09-18 21:01
没有多线程
点赞 回复 分享
发布于 2017-09-18 21:07
虽然只有3个线程 但是我还是用了线程池 = = 
点赞 回复 分享
发布于 2017-09-18 21:29
import java.util.*; public class Main{     public static void main(String args[]){         int count = 0;         Scanner sc =new Scanner (System.in);         while(sc.hasNext()){         String s = sc.nextLine();         int a = s.indexOf("u51");         if(a!=-1){count++;}        }        System.out.print(count);     } }
点赞 回复 分享
发布于 2017-09-18 21:34
我会告诉你我在网吧写不进去了,随便这样写了 结果过了(虽然没卵用,多线程肯定会看代码)
点赞 回复 分享
发布于 2017-09-18 21:34
写了多线程 本地输入测试可以通过 提交一直0 浪费了很久时间
点赞 回复 分享
发布于 2017-09-18 22:45
循环调用kmp匹配ac的
点赞 回复 分享
发布于 2017-09-19 21:55
import java.util.Scanner; import java.util.Vector; import java.util.concurrent.*; public class Main { public static void main(String[] args) throws ExecutionException, InterruptedException { Vector<String> queue=new Vector<String>(); CountDownLatch latch=new CountDownLatch(1); Produce produce = new Produce(queue,latch); ExecutorService service = Executors.newFixedThreadPool(4); service.execute(produce); Future<Integer> submit1 = service.submit(new Consumer(queue,latch)); Future<Integer> submit2 = service.submit(new Consumer(queue,latch)); Future<Integer> submit3 = service.submit(new Consumer(queue,latch)); System.out.println(submit1.get()+submit2.get()+submit3.get()); service.shutdown(); } } class Produce implements Runnable{ Vector<String> queue=null; Scanner sc=new Scanner(System.in); CountDownLatch latch; Produce(Vector<String> queue,CountDownLatch latch){ this.queue=queue; this.latch=latch; } @Override public void run() { while(sc.hasNext()){ String s = sc.nextLine(); if (s.equals("110")){break;} queue.add(s); } latch.countDown(); } } class Consumer implements Callable<Integer>{ Vector<String> queue; CountDownLatch latch; Consumer(Vector<String> queue,CountDownLatch latch){ this.queue=queue; this.latch=latch; } int count=0; @Override public Integer call() throws Exception { latch.await(); while(!queue.isEmpty()){ String s = queue.remove(0); if (s.contains("u51")){ count++; } } return count; } }
点赞 回复 分享
发布于 2017-09-19 22:02

相关推荐

斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务