51信用的编程题有意思,要写多线程

RT,还是第一次在线笔试写多线程的代码,不知道牛客是怎么判题的

edit 2017-9-18 21:04:30

我乱写的多线程,不知道会不会有并发问题,反正AC了

import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

class TASK extends Thread {

  private static int maxn = 0;
  private static AtomicInteger idx = new AtomicInteger(0);
  private static AtomicInteger count = new AtomicInteger(0);
  private static String[] strs = new String[maxn];

  TASK() {}

  TASK(String[] strs, int n) {
    this.strs = strs;
    this.maxn = n;
  } 
  @ Override 
  public void run() {
    super.run();
    String str;
    while ((str = getString()) != null) {
      if (str.contains("u51")) {
        addCount();
      }
    }
  }

  public String getString() {
    int idxx = idx.getAndIncrement();
    if (idxx < maxn) {
      return strs[idxx];
    } else {
      return null;
    }
  }

  public void addCount() {
    count.incrementAndGet();
  }

  public int getRes() {
    return count.get();
  }
}

public class Main {

  public static void main(String[] args) {
    String[] strs = new String[200000];
    Scanner scanner = new Scanner(System.in);
    int cnt = 0;
    while (scanner.hasNextLine()) {
      strs[cnt++] = scanner.nextLine();
    }
    TASK T1 = new TASK(strs, cnt);
    TASK T2 = new TASK();
    TASK T3 = new TASK();
    T1.start();
    T2.start();
    T3.start();
    try {
      T1.join();
      T2.join();
      T3.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println(T1.getRes());
  }


}

全部评论
import java.util.ArrayList;import java.util.Scanner;import java.util.List;import java.util.concurrent.*; public class Main { private static volatile int index = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); final List<String> list = new ArrayList<String>(); while (sc.hasNext()){ list.add(sc.nextLine()); } ExecutorService service = Executors.newFixedThreadPool(3); Future<Integer> result = service.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { for (int j = 0; j < list.size(); j++) { for (int i = 0; i < list.get(j).length(); i++) { if (list.get(j).charAt(i) == 'u') { if (i + 1 < list.get(j).length() && list.get(j).charAt(i + 1) == '5') { if (i + 2 < list.get(j).length() && list.get(j).charAt(i + 2) == '1') { index++; } } } } } return index; } }); try { System.out.println(result.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } finally { service.shutdown(); } } }这样子AC了~
点赞 回复 分享
发布于 2017-09-18 21:01
这题不用多线程也能AC
点赞 回复 分享
发布于 2017-09-18 20:57
第二题 import java.util.*; public class Main{ public static void main(String[] args){ Scanner s=new Scanner(System.in); int a[]=new int[20000],count=0,max=-1,maxId=-1; while (s.hasNext()){ a[count++]=s.nextInt(); } int value[]=new int[20001],length=count/2,get[]=new int[20001]; for (int i=0;i<length;i++){ value[a[i]]+=a[i+length]; get[a[i]]++; if (value[a[i]]>max){ max=value[a[i]]; maxId=a[i]; }else if (value[a[i]]==max){ if (get[a[i]]>get[maxId]){ maxId=a[i]; } } } System.out.println(maxId); } } 第一题代码没了,撤回不回去。。。关于输入问题,大家如果用IDE的话就不要在IDE里面进行输入了,直接出去命令行跑  java Main,然后输入,再按CTL+C关闭了输入流,这样程序就会执行了
点赞 回复 分享
发布于 2017-09-18 20:56
import java.util.Scanner; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; //消费者 class Task implements Runnable {     private final CountDownLatch startGate;     private final CountDownLatch endGate;     private final BlockingQueue<String> queue;     private final AtomicInteger count;     Task(CountDownLatch startGate, CountDownLatch endGate, BlockingQueue<String> queue, AtomicInteger count) {         this.startGate = startGate;         this.endGate = endGate;         this.queue = queue;         this.count = count;     }     @Override     public void run() {         try {             startGate.await();             while (!queue.isEmpty()) {                 try {                     if (queue.take().contains("u51")) {                         count.incrementAndGet();                     }                 } finally {                     endGate.countDown();                 }             }         } catch (InterruptedException e) {             e.printStackTrace();         }     } } public class Main {     public static void main(String[] args) throws InterruptedException {         final BlockingQueue<String> queue = new LinkedBlockingQueue<>();         final AtomicInteger count = new AtomicInteger();         Scanner scanner = new Scanner(System.in);         //闭锁         final CountDownLatch startGate = new CountDownLatch(1);         final CountDownLatch endGate = new CountDownLatch(3);         while (scanner.hasNextLine()) {             queue.put(scanner.nextLine());         }         Task task = new Task(startGate, endGate, queue, count);         new Thread(task).start();         new Thread(task).start();         new Thread(task).start();         startGate.countDown();         endGate.await();         System.out.println(count);     } } 请教一下这样可以吗?因为没有原题也没法测了
点赞 回复 分享
发布于 2017-10-03 15:58
测试也是这题目,表示完全不会
点赞 回复 分享
发布于 2017-09-18 21:04
用的python 输入用for循环进行20W次输入并存入list中。。。然后在单线程的情况下,对list进行遍历判断返回结果,AC了; 后来用了多线程,开了3个线程,加了锁,也AC了; 所以我觉得这道题在系统判断上只是看结果,并没有判断是否真的用了多线程。。。。。不知道是不是这样的
点赞 回复 分享
发布于 2017-09-18 20:59
这题所有的测试用例都可以,本质很简单,但是加上了多线程不知道怎么评判输入的,搞不懂
点赞 回复 分享
发布于 2017-09-18 20:50
你们的是啥题,还要多线程?
点赞 回复 分享
发布于 2017-09-18 20:49
我很好奇这题怎么在线评分,又不能自己上传文件,自己输入字符串通过了0%,累觉不爱
点赞 回复 分享
发布于 2017-09-18 20:49
有写出来的么
点赞 回复 分享
发布于 2017-09-18 20:48
刚交了 
点赞 回复 分享
发布于 2017-09-18 20:47
楼主写出来了?
点赞 回复 分享
发布于 2017-09-18 20:38
请问第一题还款怎么弄?
点赞 回复 分享
发布于 2017-09-18 20:35
不知道什么意思
点赞 回复 分享
发布于 2017-09-18 20:28
是呀,感觉两题都挺简单的
点赞 回复 分享
发布于 2017-09-18 20:25

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务