• 牛客刷题系列(汽水瓶,跳台阶扩展问题,斐波那契凤尾)


    很多小伙伴为了刷题发愁
    今天为大家推荐一款刷题神奇哦:刷题面试神器牛客
    各大互联网大厂面试真题。从基础到入阶乃至原理刨析类面试题 应有尽有,赶快来装备自己吧!助你面试稳操胜券,solo全场面试官.

    一:汽水瓶

    题目链接

    image-20221012104134222

    常规写法

    #include 
    using namespace std;
    
    int main() {
        int n;
        while (cin >> n) {
            int count = 0;
            if(n == 0)
            break;
            while (n != 0) {
                if (n == 2) 
                {
                    count += 1;
                    n = 0;
                } 
                else if(n == 1)
                    break;
                else 
                {
                    count += n / 3;
                    n = n / 3 + n % 3;
                }
            }
           cout<< count<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    即按照题目的常规思路写下去

    简便写法

    • 注意题目要求:

    某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。

    • 所以我们可以将二个空瓶子 + 借老板的一个 -> 换得 一瓶
    • 再归还这一瓶
    • 所以我们等于 两个 空瓶子 == 喝一瓶

    二.跳台阶扩展问题

    • 题目链接:链接

    • 代码实现:

    class Solution {
    public:
        int jumpFloorII(int number) {
             return pow(2,number-1);
            }
        
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 改进写法:
    class Solution {
    public:
        int jumpFloorII(int number) {
             return 1<<(number-1);
            }
        
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 总结:

    以后遇到2的次方运算时,应用位运算来提高效率

    三:斐波那契凤尾

    image-20221102211104643

    • 代码:
    // write your code here cpp
    #include 
    #include 
    using namespace std;
    
    int main() {
        int board = -1;
        long long ans[100000];
        ans[0] = 1;
        ans[1] = 2;
        for (int i = 2; i < 100000; i++) {
            long long next = ans[i - 1] + ans[i - 2];
            if( board == -1 && next >= 1000000 ) //首次出现
            {
                board  = i+1;
            }
            ans[i] = next % 1000000;
        }
        
        int n;
        while (cin >> n) {
            long long f = ans[n - 1];
            if(n>=board)
                printf("%06d\n",f);
            else
            printf("%d\n", f);
            }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 思路:
    1. 利用一个数组来保存从1~100000的数
    2. 用的时候再去取就好了
    3. 时间复杂度低,空间复杂度高。属于用空间来换时间
    • 注意事项:
    1. 注意首次出现大于1000000的数,后面的数就要输出后6位了,假如不做处理,后面的数有可能低于6位。if( board == -1 && next >= 1000000 ) //首次出现
  • 相关阅读:
    超全整理,性能测试——数据库索引问题定位+分析(详细)
    spring boot MongoDB实战
    蓝桥杯单片机串口初始化加上之后,数码管就不能正常显示了,为什么
    Java贪吃蛇
    应用案例 | 使用dataFEED OPC Suite将汽车零部件工厂数据集成到SAP Business Suite
    云原生爱好者周刊:在 Linux 系统中运行 FreeBSD 子系统 | 2022-09-05
    想学计算机编程从什么学起?零基础如何自学计算机编程?中文编程开发语言工具箱之渐变标签组构件
    java配置GDAL
    英国博士后招聘|林肯大学—植物-土壤相互作用
    低代码 系列 —— 中后台集成低代码预研
  • 原文地址:https://blog.csdn.net/yin_ming_hui/article/details/127659295