今日头条在线笔试2018-03-25

简介: 1题目描述在n个元素的数组中,找到值为k的数字对去重后的个数image.pngimage.png//参考代码 30% casepublic class Main { public static void m...

1题目描述
在n个元素的数组中,找到值为k的数字对去重后的个数


img_b460aa1fa1b9c95a8e39d8ff10b57c50.png
image.png

img_11831d0296b9e5a222d1ab1a043a6a19.png
image.png
//参考代码 30% case
public class Main {
    public static void main(String[] args) {
        
    Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
       
        int arr[] = new int[n];
        
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
        } 
        
        System.out.println(solve(arr,k));
    }
    
    static int solve(int arr[], int k){
        int len = arr.length;
        ArrayList<Integer> rest = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
              if (Math.abs(arr[i] - arr[j]) == k && (!rest.contains(arr[i] + arr[j]))) {
                  rest.add(arr[i] + arr[j]);
              }
            }
          }
        
        return rest.size();
    }
}


2题目描述
打印输出下面的效果:
输入描述
第一行是一个整数n,接下来n行都是 由数字6构成的数学表达式(+,-,*法运算)
输出描述
用图形输出数学表达式的结果,如下效果图


img_3a9b7a38c6bb7bd2fb4cfcddfa23ba21.png
image.png

img_c5fe41b8bf53cefdbde5df3a0b5f3644.png
image.png
//AC 代码
public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        String arr[] = new String[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.next();
        }

        for (int i = 0; i < n; i++) {
            int num = cal(arr[i]);
            String sNum = String.valueOf(num);
            int k = sNum.length();
            // System.out.println(sNum +"--"+k);
            int len = 5 * k + (k - 1) * 2;
            char[][] chars = new char[5][len];
            for (int p = 0; p < 5; p++) {
                for (int q = 0; q < len; q++) {
                    chars[p][q] = '.';
                }
            }

            for (int j = 0; j < k; j++) {
                // 取出字符后减去'0'(注意是字符0)就可以得到数值了,比如'1'-'0'=1;
                int shu = sNum.charAt(j) - '0';
                switch (shu) {
                case 0:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4) {
                                chars[p][q] = '6';
                            }
                            if (q == 7 * j || q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 1:

                    for (int p = 0; p < 5; p++) {
                        chars[p][7 * j + 4] = '6';
                    }

                    break;
                case 2:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p > 1) {
                                chars[p][q] = '6';
                            }

                            if (p < 2 && q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }

                    break;
                case 3:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 4:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p < 1) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 2) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 5:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p < 1) {
                                chars[p][q] = '6';
                            }

                            if (p > 1 && q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 6:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j) {
                                chars[p][q] = '6';
                            }

                            if (p > 1 && q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 7:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 8:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 4 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;
                case 9:
                    for (int p = 0; p < 5; p++) {
                        for (int q = 7 * j; q <= 7 * j + 4; q++) {
                            if (p == 0 || p == 2) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j && p < 1) {
                                chars[p][q] = '6';
                            }

                            if (q == 7 * j + 4) {
                                chars[p][q] = '6';
                            }
                        }
                    }
                    break;

                }

            }

            for (int ii = 0; ii < chars.length; ii++) {
                System.out.println(new String(chars[ii]));
            }
        }
    }

    /**
     * 计算表达式的值
     * 
     * @param str
     * @return
     */
    static int cal(String str) {

        char[] cs = str.toCharArray();
        int ans = 6;
        for (int i = 1; i < cs.length; i++) {
            switch (cs[i]) {

            case '+':
                ans = ans + 6;
                break;
            case '-':
                ans = ans - 6;
                break;
            case '*':
                ans = ans * 6;
                break;
            case '6':
                break;
            }
        }
        return ans;
    }
}


3题目描述
字符串操作


img_a1213de450bcc433ec5f8215e3df517b.png
image.png

img_48b7afca5692c117016d0c09f81d4fa4.png
image.png


4题目描述


img_de8b820b66a85e0c49114a1c1b7a7b31.png
image.png
目录
相关文章
|
7天前
|
Java 程序员 微服务
黑马头条D1 导学
黑马头条D1 导学
9 0
|
2月前
|
SQL 算法 安全
面试美团、头条、百度、京东,一名3年Java开发经验的面试总结
毕业转行做开发3年以来, 学到了很多, 加上自己的兴趣爱好, 个人认为已经成为了一个合格的程序员. 与刚开始找工作面试相同的是都会问一些相同的问题, 不同的是现在面试官会更注重为什么, 也就是说注重深度而非广度. 3年, 5年, 10年分别是个人从事技术方面职业规划中的一个坎, 3年大部分时间应对了业务逻辑, 培养良好的规范和思想, 基础知识还是欠缺.
新头条 2020.05.16
1、构造手机验证码:使用random对象生成要求的随机数作为验证码,例如4位验证码:1000~9999之间随机数;
新头条 2020.05.16
|
算法 搜索推荐 大数据
趣头条”去趣头条“
趣头条”去趣头条“
144 0
趣头条”去趣头条“
|
SQL 缓存 NoSQL
原创 | 2020年阿里、头条、百度、美团、新浪、旷世、VIVO的Java大数据面经
原创 | 2020年阿里、头条、百度、美团、新浪、旷世、VIVO的Java大数据面经
阿里云市场头条投稿指南
阿里云市场头条投稿指南
5285 0
|
定位技术
微博进入肉搏时代:腾讯明争新浪,网易微博暗斗
   在2009年到2010年,新浪将微博市场大局紧紧攥在自己手里。但从2010年末,新浪之外的其他三家门户开始发力,腾讯、网易、搜狐各携自家杀手锏,将中国微博推向真正的肉搏时代。时至今日,中国微博战争的硝烟,已经轰轰烈烈地遍布每个角落。
1481 1
|
新零售 搜索推荐 NoSQL
今日头条这么牛逼,用了什么技术?
今日头条创立于2012年3月,到目前仅 6 年时间。从十几个工程师开始研发,到上百人,再到200余人。产品线由内涵段子,到今日头条,今日特卖,今日电影等产品线。
3680 0
|
NoSQL 网络协议 程序员
今日头条研发面经
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a724888/article/details/82701510 今日头条研发面经 本文首发于微信公众号:程序员江湖     今日头条上海   后台开发工程师         今日头条 后端研发工程师 找牛客大佬要了白金码,跳过死亡笔试,直接视频面,从3点开始,断断续续到晚上8点结束。