zoukankan      html  css  js  c++  java
  • 网易2018实习生招聘笔试题

    可能题目的顺序不一样,但是题目应该是一样的。

    1.一条街有n个位置,每个位置需要照亮的话用 . 表示,不需要照亮的话用 X 表示,每盏路灯能照亮,i - 1, i , i + 1三个位置,问最少用多少路灯,能照亮所有要求的位置。

    /*************************************************************************
        > File Name: A.cpp
        > Author: LyuCheng
        > Created Time: 2018-03-27 19:04
        > Description: 问题的实质到最后实际上就是遍历一遍,遇到一个.不管后边两
            个是什么,统统在i + 1的位置按上一个路灯,思考一下是不是?
     ************************************************************************/
    
    #include <bits/stdc++.h>
    
    #define MAXN 1234
    
    using namespace std;
    
    int t;
    int n;
    char str[MAXN];
    int ans;
    
    inline void init() {
        memset(str, '', sizeof str);
        ans = 0;
    }
    
    int main(int argc, char**argv) {
        freopen ("in.txt", "r", stdin);
        scanf ("%d", &t);
        while (t --) {
            init();
            scanf ("%d", &n);
            scanf ("%s", str);
            for (int i = 0; i < n; i ++) {
                if (str[i] == 'X')
                    continue;
                else {
                    ans ++;
                    for (int j = i; j < n && j < i + 3; j ++)
                        str[j] = 'X';
                }
            }    
            printf("%d
    ", ans);
        }    
        return 0;
    }

    2.有这样一种序列,1,12,123, ... 123456910111213

    问你区间[l, r]中有多少项不能被3整除。

    /*************************************************************************
        > File Name: B.cpp
        > Author: LyuCheng
        > Created Time: 2018-03-27 20:09
        > Description: 打表找个规律,第i项:i % 3 == 0这一项就能被3整除
     ************************************************************************/
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int l, r;
    int a, b;
    
    inline void init() {
        a = 0;
        b = 0;
    }
    
    int main(int argc, char**argv) {
        init();
        scanf ("%d %d", &l, &r);    
        l --;
        a += (l / 3) * 2;
        if (l % 3 != 0)
            a += (l % 3 - 1);
        b += (r / 3) * 2;
        if (r % 3 != 0)
            b += (r % 3 - 1);
        printf("%d
    ", b - a);
        return 0;
    }

    3.去野餐有n种物品,一个容量为w的背包,(1 <= w <= 2e9) 每件物品 v[i] (0 <= v[i] <= 1e9),有多少种装法,是的物品总体积不超过w

    /*************************************************************************
        > File Name: C.cpp
        > Author: LyuCheng
        > Created Time: 2018-03-27 20:21
        > Description: 这个题正解应该是每次枚举到一个大于w的状态i,就能排除后边
            很多状态(如果 (j & i) == i (j > i),那么这个状态就被排除了),所以
            时间负责度会减少很多,记忆化搜索就可以了。
            但是笔试时间不多了,我耍了个小聪明,只记录最后一个不能的状态,用来
            排除状态,结果就卡过了。
     ************************************************************************/
    
    #include <bits/stdc++.h>
    
    #define MAXN 34
    #define LL long long
    
    using namespace std;
    
    int n;
    LL w;
    LL v[MAXN];
    LL sum;
    LL ans;
    
    inline void init() {
        sum = 0;
        ans = 0;
    }
    
    int main(int argc, char**argv) {
        init();
        scanf ("%d %lld", &n, &w);
        for (int i = 0; i < n; i ++) {
            scanf ("%lld", &v[i]);
            sum += v[i];    
        }    
        if (sum <= w) {
            printf("%lld
    ", (LL)(1 << n));
            exit(0);
        }
        LL pos = (1 << n);
        for (LL i = 0; i < (1 << n); i ++) {
            if ((i & pos) == pos) {
                continue;
            }
            LL s = 0;
            for (LL j = 0; j < n; j ++) {
                if ((i & (1 << j)) != 0) {
                    s += v[j];
                }
                if (s > w) {
                    pos = i;
                    break;
                }
            }
            if (s <= w)
                ans ++;
        }
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    js数组去重五种方法
    wm_concat 多行字符串拼接
    ORACLE WITH AS 简单用法
    layui laytpl 语法
    看懂Oracle执行计划
    GIT RM -R --CACHED 去掉已经托管在GIT上的文件
    sourceTree使用教程--拉取、获取
    SourceTree忽略文件和文件夹
    layui table 详细讲解
    利用POI实现下拉框级联
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/8660473.html
Copyright © 2011-2022 走看看