在赛码网上笔试,本地找不出问题来,但是一提交就是百分之0
在赛码网上笔试,本地找不出问题来,但是一提交就是百分之0,真的好多题目了,实在不知道怎么解决,恳请java大神指点一二
这个是携程笔试的那个二分查找: /** * 请写一个二分查找算法查找一个数最先出现的index,如果数不在集合中需要返回(-1)-当前数应该出现的位置。 * 例如 [1,3,6],查找5,5应该是在index=2的位置但并不在集合中。返回(-1)-2 = -3。 */ import java.util.Scanner; public class BinarySearch { public static void main(String[] arg) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int num=scan.nextInt(); int [] array=new int[num]; for(int i=0;i<num;i++) { array[i]=scan.nextInt(); } int number=-1; int front = 0; int tail = array.length - 1; while (front <= tail) { int middle = (front + tail) / 2; if (array[middle] == n) { number=middle; break; } else if (array[middle] > n) { tail = middle - 1; } else { front = middle + 1; } } if(number==-1) { number = (-1) - front; } System.out.println(number); } }
下面这个是今天网易互娱的第一道题目:
import java.util.*; public class DanCi { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNext()) { int lines=scan.nextInt(); scan.nextLine(); HashMap<String,Integer> map=new HashMap<String,Integer>(); for(int i=0;i<lines;i++) { String str=scan.nextLine(); String[] strs=str.split(" "); int num=Integer.parseInt(strs[0]); for(int j=1;j<=num;j++) { String s=strs[j]; if(map.containsKey(s)) { int temp=map.get(s); temp++; map.put(s,temp); } else map.put(s,1); } } int count=scan.nextInt(); Set<Map.Entry<String,Integer>> set=map.entrySet(); Iterator<Map.Entry<String,Integer>> it=set.iterator(); ArrayList<Integer> list=new ArrayList<Integer>(); while(it.hasNext()) { Map.Entry<String,Integer> entry = it.next(); list.add(entry.getValue()); } Collections.sort(list); Collections.reverse(list); int countt=0; for(int i=0;i<count&&countt<count;i++) { ArrayList<String> strsb=new ArrayList<String>(); Set<Map.Entry<String,Integer>> set1=map.entrySet(); Iterator<Map.Entry<String,Integer>> it1=set1.iterator(); while(it1.hasNext()) { Map.Entry<String,Integer> entry = it1.next(); if(entry.getValue()==list.get(i)) { strsb.add(entry.getKey() + " " + entry.getValue()); } } Collections.sort(strsb); if(countt<count) { if(strsb.size()==1) { System.out.println(strsb.get(0)); countt++; } else { for (int j = 0; j < strsb.size(); j++) { System.out.println(strsb.get(j)); countt++; if(countt>=count) break; } } } } } } }

