23届秋招百度笔试算法题

import java.util.Scanner;

//23届秋招百度笔试第一题:暴力(100%)
public class Main10 {
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int res1=0,res2=0;
		int mid=sc.nextInt();
		for (int i = 0; i < n; i++) {
			if(mid==1) {
				res1++;
			}else if(mid==2) {
				res2++;
			}else if(mid==3) {
				if(res1>res2) {
					res1++;
				}else if(res1==res2) {
					res1++;
					res2++;
				}else {
					res2++;
				}
			}
			if(i!=n-1) {
				mid=sc.nextInt();
			}
		}
		System.out.println(res1+" "+res2);
	}

}
import java.util.Arrays;
import java.util.Scanner;

//23届秋招百度笔试第二题:暴力(44.44%)
public class Main11 {

	public static void f11(int n,int[] arr) {
//		System.out.println(Arrays.toString(arr));
		long res=0L;
		for (int i = 0; i < n/2-1; i++) {
			if(arr[i]==arr[n-1-i]) {
				continue;
			}else if(arr[i]<arr[n-1-i]) {
//				arr[i]+=(arr[n-1-i]-arr[i]);
				arr[i+1]+=(arr[n-1-i]-arr[i]);
				res+=(arr[n-1-i]-arr[i]);
			}else {
//				arr[n-1-i]+=(arr[i]-arr[n-1-i]);
				arr[n-1-i-1]+=(arr[i]-arr[n-1-i]);
				res+=(arr[i]-arr[n-1-i]);
			}
		}
		
		//判断
		if(n%2==0) {
			if(arr[n/2-1]!=arr[n/2]) {
				System.out.println(-1);
			}else {
				System.out.println(res);
			}
		}else {
			if(arr[n/2-1]!=arr[n/2+1]) {
				System.out.println(res+Math.abs(arr[n/2-1]-arr[n/2+1]));
			}else {
				System.out.println(res);
			}
		}
//		System.out.println(Arrays.toString(arr));
		
	}
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[] arr=new int[n];
		for (int i = 0; i < n; i++) {
			arr[i]=sc.nextInt();
		}
		f11(n,arr);
	}

}
import java.util.*;

//23届秋招百度笔试第三题:暴力(?%)
public class Main12 {

	//提取名称的方法
	public static void f12(String str) {
		int mid=str.indexOf("new");
		while (mid!=-1) {
			int sub1=str.lastIndexOf(",",mid);
			int sub2=str.lastIndexOf("=",mid);
			if(sub1!=-1) {
				set.add(str.substring(sub1+1,sub2).trim());
			}else {
				int sub3=str.indexOf("Node");
				int sub4=str.indexOf("Node[]");
				if(sub3>mid||sub4>mid) {
					set.add(str.substring(0,sub2).trim());
				}else {
					int sub5=str.indexOf(" ");
					set.add(str.substring(sub5,sub2).trim());
				}
			}
			
			mid=str.indexOf("new",mid+1);
		}
	}
	
	static Set<String> set=new HashSet<String>();
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
    	sc.nextLine();
    	sc.nextLine();//去除头两行
    	String str=sc.nextLine();
    	boolean flag=false;//判断/**/
    	String laststr="";
    	while (!"}".equals(str)) {
			if("/*".equals(str.trim())) {
				flag=true;
			}
			if("*/".equals(str.trim())) {
				flag=false;
				str=sc.nextLine();
				continue;
			}
			
			int sub=str.indexOf("//");
			if(sub!=-1) {
				str=str.substring(0,sub);
			}
			str=str.trim();
			str=laststr+str;

    		if(!flag&&!"".equals(str)&&!"}".equals(str)) {
    			
				//注意:字符char不能和字符串String比较equals
    			if(!";".equals(str.charAt(str.length()-1)+"")) {
//    				System.out.println("-------"+str);
    				laststr=str;
    			}else {
//    				System.out.println("======="+str);
    				f12(str);

					laststr="";
	    		}
    		}
			
    		str=sc.nextLine();
		}
    	System.out.println(set.size());
//    	System.out.println(set);
	}

}

//测试数据
//class Main{
//    public static void main(String[] args){
//        Node x,y = new Node(5), z=new Node();  //'new' can lots of things.
//        //b=new Node();
//        Node[] xx = new Node[5];
//        xx[3]=new Node();
//        xx[3] = new Node();
//        /*
//        Node yy=new Node(11);
//        Node zz=new Node(yy.x);
//        */
//        y=      //there is nothing.
//        new Node();
//    }
//}
//

//预期结果
//4
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BB-X

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值