美文网首页
百度在线笔试

百度在线笔试

作者: 研途更疯狂 | 来源:发表于2015-04-20 12:41 被阅读258次

算法才是一个程序员的灵感。

以前一直以为作为一个程序员编出牛逼的应用才是真正伟大的事情,经过昨晚的baidu在线笔试后,觉得这个想法有点空洞,不否认,编写一个受万千人欢迎的APP确实是一件牛逼的事情,但是你知道么?牛逼的应用也是有牛逼的算法作为支撑,也是一点一点的基础知识作为开发应用的基石。

大学期间代码量的稀少造成了现在在线编程的困难,事实是,书本上的依然重要,但是实践确实更重要的事情,一直以来我把实践放在理论学习的靠后位置,本来就是这样,如果没有理论的依据,实践也是没有意义的,但现实是,我把实践放的太靠后,与理论学习差了十万八千里。

昨晚的编程题确实不是很难,但在有限的时间内,我却编不出来,实在差太多,所以下一步,打算刷题,好的程序员应该是编代码编出来的而不是啃书本看出来的。

下面把baidu笔试的最后两道附加题写一下,理论很简单,但是从问题描述到抽象模型确是比较难,当时就是卡在这,写不出来。

第一道

其实就是比较两个字符串,返回一个字符串在另一个字符串中出现了几次。
input:
1
A B C
AABCABCABC
output:
3

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner scan=new Scanner(new BufferedInputStream(System.in));
        int count=Integer.valueOf(scan.nextLine());
        while(count-->0){
            String strTmp=scan.nextLine();
            //取消输入字符串的空格
            String str=strTmp.replace(" ", "");
            String str1=scan.nextLine();
            //定义子字符串的索引位置
            int index=str1.indexOf(str),n=0;
            while(index!=-1){
                n++;
                //从本次出现开始检索,返回子字符串的下一索引位置
                index=str1.indexOf(str,index+str.length());
            }
            System.out.println(n);      
        }
        scan.close();
    }
}

第二题

题目中描述是页面跳转,个人感觉就是对字符串的操作
input:
2
1
1
6
1 2 3 3 3 3
output
1
3->2->1

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner scan=new Scanner(new BufferedInputStream(System.in));
        int count=Integer.valueOf(scan.nextLine());
        while(count-->0){
            int n=Integer.valueOf(scan.nextLine());
            String str=scan.nextLine();
            String[] arr=str.split(" ");
            String str1=str.replace(" ", "");

            int index=str1.indexOf(new String(arr[n-1]),0);
            
            for(int i=index;i>=0;i--){
                System.out.print(arr[i]);
                if(i!=0){
                    System.out.print("->");
                }       
            }
            System.out.println();
        }
        
        scan.close();
    }
}

相关文章

网友评论

      本文标题:百度在线笔试

      本文链接:https://www.haomeiwen.com/subject/rgayfttx.html