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


    很多小伙伴为了刷题发愁
    今天为大家推荐一款刷题神奇哦:刷题面试神器牛客
    各大互联网大厂面试真题。从基础到入阶乃至原理刨析类面试题 应有尽有,赶快来装备自己吧!助你面试稳操胜券,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 ) //首次出现
  • 相关阅读:
    ISO20000认证一般要多少钱
    强大博客搭建全过程(1)-hexo博客搭建保姆级教程
    智慧城市平台的技术路线和系统要求
    SpringBoot--参数校验--注解
    java中线程池的使用+优雅的spring释放资源
    不同MySQL服务的表以及库的数据迁移(/备份)
    游戏AI的创造思路-技术基础-深度学习(5)
    【Python基础篇】运算符
    git远程仓库分支推送与常见问题
    小程序开发流程详细是什么呢?
  • 原文地址:https://blog.csdn.net/yin_ming_hui/article/details/127659295