蓝桥杯基础训练

题目:http://lx.lanqiao.cn/problemset.page?code=BASIC-&userid=188230

一、数列排序

time:0ms

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    int g, item;
    cin >> g;
    vector<int> v;
    while (g--)
    {
        cin >> item;
        v.push_back(item);
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); ++i)
        cout << v[i] << " ";
    cout << endl;
    return 0;
}

二、十六进制转八进制

思路:将16进制先转为2进制,然后将2进制再转为8进制

time:46ms

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        string in_out, t;
        cin >> in_out;
        for (int i = 0; i < in_out.size(); ++i)
        {
            switch (in_out[i])
            {
            case '0':t += "0000"; break;
            case '1':t += "0001"; break;
            case '2':t += "0010"; break;
            case '3':t += "0011"; break;
            case '4':t += "0100"; break;
            case '5':t += "0101"; break;
            case '6':t += "0110"; break;
            case '7':t += "0111"; break;
            case '8':t += "1000"; break;
            case '9':t += "1001"; break;
            case 'A':t += "1010"; break;
            case 'B':t += "1011"; break;
            case 'C':t += "1100"; break;
            case 'D':t += "1101"; break;
            case 'E':t += "1110"; break;
            case 'F':t += "1111"; break;
            }
        }
        if (t.length() % 3 == 1)
            t = "00" + t;
        if (t.length() % 3 == 2)
            t = "0" + t;
        bool key = false;
        for (int i = 0; i <= t.length() - 3; i+=3)
        {
            int num = 4 * (t[i] - '0') + 2 * (t[i+1] - '0') + t[i+2] - '0';
            if (num)
                key = true;
            if (key)
                cout << num;
        }
        cout << endl;
    }
    return 0;
}

三、十六进制与八进制互转

这里我们可以写一个函数,用于m进制转n进制

两个题都可以直接套统一的算法模板

算法模板

void Exchange_JZ(string str1, string& str2, int form, int to)
{
    int i, len, k;
    int t[maxn] = { 0 }, A[maxn] = { 0 };

    len = str1.size();
    for (i = len - 1; i >= 0; i--)
    {
        t[len - 1 - i] = str1[i] - (str1[i]<58 ? 48 : str1[i]<97 ? 55 : 61);
    }
    for (k = 0; len;)
    {
        for (i = len - 1; i >= 1; i--)
        {
            t[i - 1] += t[i] % to*form;
            t[i] /= to;
        }
        A[k++] = t[0] % to;
        t[0] /= to;
        while (len>0 && !t[len - 1])
        {
            len--;
        }
    }
    for (i = k - 1; i >= 0; i--)
        str2 += A[i] + (A[i]<10 ? 48 : A[i]<36 ? 55 : 61);
}

此算法更适用于大数任意进制之间的转换

上一题不能用此算法的原因是,8进制与16进制均为2进制的组合,利用此关系求解更为快捷

time:十六->十进制:0ms       十->十六进制:15ms

int main()
{
        string str1, str2;
        cin >> str1;
        Exchange_JZ(str1, str2, 10, 16);
        cout << str2 << endl;
    
    return 0;
}

四、特殊回文数

遍历构造解

time:0ms

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 10; i < 100; ++i)
    {
        int sum = 2 * (i % 10 + i / 10);
        if (n < sum)continue;
        else if (n - sum>=10)continue;
        else cout << i << n - sum << i % 10 << i / 10 << endl;
    }
    for (int i = 100; i < 1000; ++i)
        if (n == 2 * (i % 10 + i / 100 + i / 10 % 10))
            cout << i << i % 10 << i / 10 % 10 << i / 100 << endl;
    return 0;
}

五、回文数

遍历构造解

time:0ms

#include<iostream>
using namespace std;

int main()
{
    for (int i = 10; i < 100; ++i)
        cout << i << i % 10 << i / 10 << endl;
}

六、特殊的数字

水仙花数

遍历构造解

time:0ms

#include<iostream>
using namespace std;

int fun(int x)
{
    int a = x % 10;
    int b = x / 10 % 10;
    int c = x / 100;
    return a*a*a + b*b*b + c*c*c;
}

int main()
{
    for (int i = 100; i < 1000; ++i)
        if (i == fun(i))
            cout << i << endl;
    return 0;
}

七、杨辉三角

time:15ms

直角三角形式

#include<iostream>
using namespace std;

const int MAX = 50;
int N[MAX][MAX];

int f(int r, int c)
{
    if (c == 0 || r == c)
        N[r][c] = 1;
    if (N[r][c] == 0)
        N[r][c] = f(r - 1, c - 1) + f(r - 1, c);
    return N[r][c];
}

int main()
{
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
            for (int j = 0; j <= i; j++)
            {
                cout << f(i, j);
                if (j == i)cout << endl;
                else cout << " ";
            }
    }
    return 0;
}

等腰三角形式

#include<iostream>
using namespace std;

const int MAX = 50;
int N[MAX][MAX];

int f(int r, int c)
{
    if (c == 0 || r == c)
        N[r][c] = 1;
    if (N[r][c] == 0)
        N[r][c] = f(r - 1, c - 1) + f(r - 1, c);
    return N[r][c];
}

int main()
{
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            for (int k = 0; k < n - i; ++k)
                cout << " ";
            for (int j = 0; j <= i; j++)
            {
                cout << f(i, j);
                if (j == i)cout << endl;
                else cout << " ";
            }
        }
    }
    return 0;
}

 

八、查找整数

遍历

time:0ms

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int num, tar;
    cin >> num;
    vector<int> V;
    while (num--)
    {
        int i;
        cin >> i;
        V.push_back(i);
    }
    cin >> tar;
    vector<int>::iterator it = V.begin();
    while (it != V.end())
    {
        if (*it == tar)
        {
            cout << it - V.begin() + 1 << endl;
            break;
        }
        it++;
    }
    if (it == V.end())cout << -1 << endl;
    return 0;
}

九、数列特征

排序+遍历求和

time:15ms

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    int num, result = 0;
    cin >> num;
    vector<int>V;
    while (num--)
    {
        int i;
        cin >> i;
        V.push_back(i);
    }
    sort(V.begin(), V.end());
    cout << *(V.end() - 1) << endl << *V.begin() << endl;
    for (int i = 0; i < V.size(); ++i)
        result += V[i];
    cout << result << endl;
    return 0;
}

十、字母图形

双端队列

time:15ms

#include<iostream>
#include<deque>
using namespace std;

void fun(deque<char>& V)
{
    char ch = V[0] + 1;
    V.pop_back();
    V.push_front(ch);
}

int main()
{
    deque<char> v;
    int n, m;
    cin >> n >> m;
    for (int i = 'A'; i < 'A' + m; ++i)
        v.push_back(i);
    for (int i = 0; i < n; ++i)
    {
        for (int i = 0; i < v.size(); ++i)
            cout << v[i];
        fun(v);
        cout << endl;
    }
    return 0;
}

感谢您的阅读,生活愉快~

原文地址:https://www.cnblogs.com/lv-anchoret/p/8610761.html