首页 > 网易实习笔试
头像
牛客988320449号
编辑于 2021-04-17 10:18
+ 关注

网易实习笔试

网易笔试第一题,  矩阵中有一些工厂产生污染, 求离污染最远的地方. 曼哈顿距离
第二题, 求数组中一个子集,sum最大同时被6整除.
第三题, 给 T,   给不定长的输入,(这里我为了排序用pripority  ) 求最接近T的
   public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	int T = Integer.parseInt(in.nextLine());
    	int y = Integer.parseInt(in.nextLine());
    	System.out.println(T +y);
    	String [] ss = new String[10010];
    	String s = in.nextLine();
    	ss =  s.split(" ");
    	System.out.println(ss[0]);
    	Vector<Integer> vt = new Vector<Integer>() ;//怎么是一个有序数组???
    	PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
    	for(int i = 0 ; i < ss.length; i ++) {
    		pq.add(Integer.parseInt(ss[i]));
    	}
    	System.out.print(pq.peek());
    	//怎么遍历啊!!!遍历一个有序数组
    	while(!pq.isEmpty()) {    		//find closest num
    		vt.add(pq.poll());// 获得一个有序数组
    	}
    	int i = 0 ;
    	for(;i< vt.size() && vt.indexOf(i) < T;i++){    	}
    	//找到最接近的大一点点.
    	int closest;
    	if(i == vt.size()) {
  		  closest  = Integer.MAX_VALUE;// all smaller than T,那就照常处理
    	}	
    	else {
    		closest = vt.elementAt(i);
		}
    	if(closest == T) {
    		System.out.print(T-y);
    		return;
    	}
    	boolean flag = false;
    	for(int j = i-1; j> 0 ; j -- ) {
    		// j一定小于T
    		int temp = vt.elementAt(j);//之前也写indexof了 
    		for(int k = j ;k > 0 ; k --) {
    			temp  += vt.elementAt(k)  ;//从最大开始加
    			if(temp >=T) {
    				if(temp  < closest ) {
    					closest = temp;
    				}//更新closest
    				temp -= vt.elementAt(k);
    			}
    		}
    	}
    	System.out.print(T-y);
    	in.close();
}
不定长怎么办?
但是因为sc.nextInt()方法只读取空白符前面的值,会把空白符继续留在缓存区,而sc.nextLine()会把空白符也读取并清除,所以每次用完sc.nextInt()方法最好在后面加个sc.nextLine(),但最好舍弃这个方法,每行都采用sc.nextLine()方法读取。【在这里两个方法混用,提交代码的时候很容易经常出现数组越界的问题】
    while(cin>>a){
        sum=0;
        sum+=a;
        while(cin.get()!='\n'){
            cin>>a;
            sum+=a;        
        }
        cout<<sum<<endl;
    }
  while(scanner.hasNext()){
            String s = scanner.nextLine();
            String[] array = s.split(" ");
            int sum = 0;
            for(int i = 0; i < array.length; i++){
                sum += Integer.parseInt(array[i]);
            }
            System.out.println(sum);
        }
或者python
#不确定输入有多少行,用while循环
import sys
seq = [ ]
while True:
    line = sys.stdin.readline().strip()     #line此时是字符串列表,并已去掉前后空格 回车符
    if line:
        line = map(int,line.split()) #把line的空格元素去掉,转成字符串列表list,并转成整型int
    else:
        break
    seq += line
print(seq)



笔试第四题
给[a, b] 求范围中是回文数同时又 是回文数的三次方的数.
public class Main {

    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	String start  = in.nextLine();
    	String end = in.nextLine();
    	//Set<Integer> huiwen  = new HashSet<Integer>();
    	int s = Integer.parseInt(start);
    	int e =  Integer.parseInt(end);// 三次方想不到啊 !!
    	//找到所有三次方可能在范围中的数.
    	if(huiwen("9"))
    //		System.out.println("111");
    	System.out.print("[");
    	int count = -1 ;
    	for(int i = 1 ; i <= Math.sqrt(e) ;i++ ) {
    		if(huiwen(String.valueOf(i) ) ) {
    			//huiwen.add(i);
    			int j = i*i*i;
    			if( j<= e && j >= s && huiwen(String.valueOf(j))) { 
    				count++;
    				if(count == 0)
    					System.out.print(j);// 到底怎么输出啊!!
    				else 
    					System.out.print(","+j);
    			}
    		}
    	}
    	System.out.print("]");
    	in.close();
    	//        Scanner in = new Scanner(System.in);
//        Vector<Integer> id  = new Vector<Integer>();//不能用int
//        Vector<String> old  = new Vector<String>();//不能用int
//        String line1  = in.nextLine();
//        for(int i = 0 ; i < line1.length(); ) {
//        	String c = line1.substring(i,i+1);
//        	id.add( Integer.parseInt(c));
        }
    	private static boolean huiwen(String str){
        if(str.isEmpty()) return false;
        int slow = 0,fast = str.length();
        //if first = last
        while(slow <= fast){
           // System.out.println(str.indexOf(slow)); 
            if(str.indexOf(slow) == str.indexOf(fast)){
                slow++;//应该是charAt 我写错了写indexOF了, 好多都写成indexof了
                fast --;
            }
            else{
                return false;
            }
        }
        return true;
    }
        
}





全部评论

(4) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐