美文网首页
C++笔试算法解题思路-爬坑笔记

C++笔试算法解题思路-爬坑笔记

作者: JianLee | 来源:发表于2022-01-17 17:46 被阅读0次
    1、猴子吃桃问题

    一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住又吃了一个;第二天又吃了一半,再加上一个;后面每天都是这样吃。到第10天的时候,小猴子发现只有一个桃子了。问小猴子第一天共摘了多少个桃子。
    使用递归思路解决,

    #include <iostream>
    
    using namespace std;
    
    int num(int n) {
        int i;
        if (n == 1)i = 1;
        else i = 2 * (num(n - 1) + 1);
        return i;
    }
    
    int main() {
        cout << "猴子一共摘了" << num(10) << "个桃子";
        return 0;
    }
    
    2、判断闰年

    被4整除,并且不是100个倍数
    被4整除,是100且是400倍数

    #include <iostream>
    using namespace std;
     
    int main()
    {
        int year;
     
        cout << "输入年份: ";
        cin >> year;
     
        if (year % 4 == 0)
        {
            if (year % 100 == 0)
            {
                // // 这里如果被 400 整除是闰年
                if (year % 400 == 0)
                    cout << year << " 是闰年";
                else
                    cout << year << " 不是闰年";
            }
            else
                cout << year << " 是闰年";
        }
        else
            cout << year << " 不是闰年";
     
        return 0;
    }
    
    3、绘制三角形图案

    解题思路:
    正直角和反直角,for循环嵌套,外层控制行,内层控制星的个数。
    正三角,首先判断每一行空白处,在非空白处打*(个数为n行+2),一行打印结束换行。

    #include <iostream>
    
    using namespace std;
    int main() {
        cout << "请输入行数";
        int rows;
        cin >> rows;
        cout << "正直角三角" << endl;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j <= i; j++) {
                cout << "*";
            }
            cout << endl;
        }
        cout << "反直角三角" << endl;
        for (int i = 0; i < rows; i++) {
            for (int j = rows; j > i; j--) {
                cout << "*";
            }
            cout << endl;
        }
        cout << "正三角" << endl;
        int n = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = 0; j < rows - i; j++) {
                cout << " ";
            }
            for (int k = 0; k < n; k++) {
                cout << "*";
            }
            n += 2;
            cout << endl;
        }
        return 0;
    }
    
    4、最大公约数和最小公倍数

    最大公约数:取min数做判断,满足同时整除两数的最大一个数
    最小公倍数:取max数做判断,满足同时整除两数的最小一个数 解法2:(a*b)/最大公约数

    #include <iostream>
    using namespace std;
     
    int main()
    {
        int n1, n2;
     
        cout << "输入两个整数: ";
        cin >> n1 >> n2;
        
        while(n1 != n2)
        {
            if(n1 > n2)
                n1 -= n2;
            else
                n2 -= n1;
        }
     
        cout << "HCF = " << n1;
        return 0;
    }
    
    
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int n1, n2, max;
     
        cout << "输入两个数: ";
        cin >> n1 >> n2;
        
        // 获取最大的数
        max = (n1 > n2) ? n1 : n2;
     
        do
        {
            if (max % n1 == 0 && max % n2 == 0)
            {
                cout << "LCM = " << max;
                break;
            }
            else
                ++max;
        } while (true);
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++笔试算法解题思路-爬坑笔记

          本文链接:https://www.haomeiwen.com/subject/pwnchrtx.html