9.2奇安信算法笔试

给两个小时 就出两道题

好了看你们都讨论题目 我贴一下自己的解法 一共两道编程 17分钟就出来了

第一题 走楼梯 坑点是输入0的时候也要输出0 中间盲猜很多特判 甚至数据类型改成了long 最后才猜到输入0的这个条件 所以代码比较长有很多多余的内容
ac 60->80->100
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] line = sc.nextLine().trim().split("\\s+");
        if (line.length != 1) {
            System.out.println(0);
            return;
        }
        for (int i = 0; i < line[0].length(); i++) {
            if (!Character.isDigit(line[0].charAt(i))) {
                System.out.println(0);
                return;
            }
        }
        int n = Integer.parseInt(line[0]);
        if (n < 0 || n >36) {
            System.out.println(0);
            return;
        }
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long[] dp = new long[n+1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 2; i < n+1; i++) {
            dp[i] = dp[i-1] + dp[i-2];
        }
        System.out.println(dp[n]);
    }
}






第二题 就是分糖果 题意改成扌斥迁户分房了 真的是极具社会主义特[色]
public class Solution {
    /**
     *
     * @param person int整型一维数组
     * @return int整型
     */
    public int house (int[] person) {
        // write code here
        int n = person.length;
        int[] houses = new int[n];
        Arrays.fill(houses, 1);
        for (int i = 1; i < n; i++) {
            if (person[i] > person[i-1] && houses[i] <= houses[i-1]) {
                houses[i] = houses[i-1]+1;
            }
        }
        for (int i = n-2; i >= 0 ; i--) {
            if (person[i] > person[i+1] && houses[i] <= houses[i+1]) {
                houses[i] = houses[i+1]+1;
            }
        }
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += houses[i];
        }
        return sum;
    }
}


#笔试题目##奇安信#
全部评论
服务端 20道单选 10道多选 2道编程  编程应该和你们一样的
2 回复
分享
发布于 2020-09-02 16:06
547856 对应 212312
1 回复
分享
发布于 2020-09-02 16:34
联想
校招火热招聘中
官网直投
第二题思路
点赞 回复
分享
发布于 2020-09-02 15:44
挺无语的
点赞 回复
分享
发布于 2020-09-02 15:45
第二图啥思路唉
点赞 回复
分享
发布于 2020-09-02 15:46
第二题 一共就5个数 直接手动推,就推出来了
点赞 回复
分享
发布于 2020-09-02 15:57
一共就两道编程题吧     是不是没有选择,我应该没有漏答吧
点赞 回复
分享
发布于 2020-09-02 16:03
安全岗,也是2个编程,第二道住户分房一开始0.8,后来想到45765这种减了重复过了。
点赞 回复
分享
发布于 2020-09-02 16:14
分享一下代码
点赞 回复
分享
发布于 2020-09-02 16:32
    int house(int* person, int personLen) {         // write code here         int index = 0;         vector<int> res(personLen, 0);         while (index + 1 < personLen) {          // 降序过程          int num = 1;          int startIndex = index;          while (index + 1 < personLen && person[index] > person[index + 1]) {          ++num;          ++index;          }          res[startIndex] = max(res[startIndex], num--);          for (int i = startIndex + 1; i <= index; ++i) {          res[i] = num--;          }          // 升序过程          num = 1;          while (index + 1 < personLen && person[index] < person[index + 1]) {          res[++index] = ++num;          }          // 相同          while (index + 1 < personLen && person[index] == person[index + 1]) {          res[++index] = 1;          }         }         return accumulate(res.begin(), res.end(), 0);     }
点赞 回复
分享
发布于 2020-09-02 16:40

相关推荐

1 3 评论
分享
牛客网
牛客企业服务