蓝桥杯基础练习

数列排序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <bitset>
using namespace std;
// 大数,内存处理
#define INF 0x3f3f3f3f
#define lln long long 
#define MEM(a) memset(a, 0, sizeof(a))
#define MEMM(a) memset(b, -1, sizeof(b))
#define DEB(x, n) cout << (x) << " " << (n) << endl
#define CR printf("\n")
// 调试用
    template <class Type>
void debug(Type a[], int len)
{
    for(int i = ; i < len ; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}
int main()
{
#ifdef DEBUG
    // freopen("input", "r", stdin);       //从input文件中读入
    // freopen("output", "w", stdout);     //输出到output文件
#endif
    int n;
    const int maxn = 200 + 10;
    int a[maxn];
    while(cin >> n)
    {
        for(int i = ; i < n; i++)
            cin >> a[i];
        sort(a, a+n);
        for(int i = ; i < n; i++)
            printf("%d%c", a[i], i == n-1? '\n': ' ');
    }
    return ;
}

十六进制转八进制

转换的时候,先转换成二进制,再转换成十六进制。

这道题目还是比较有意思的,使用string只需要78ms,使用char*则超时。可能strcat每次都需要从头遍历数组,但是string则不需要。

当然,如果是数字没有这么大,完全可以这样处理

lln a;
scanf("%x", &a);
printf("%o", a);

是不是很精髓= =

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <bitset>
using namespace std;
// 大数,内存处理
#define INF 0x3f3f3f3f
#define lln long long 
#define MEM(a) memset(a, 0, sizeof(a))
#define MEMM(a) memset(b, -1, sizeof(b))
#define DEB(x, n) cout << (x) << " " << (n) << endl
#define CR printf("\n")
// 调试用
    template <class Type>
void debug(Type a[], int len)
{
    for(int i = ; i < len ; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}
const char t2[16][9] =
{
    "0000", "0001", "0010", "0011",
    "0100", "0101", "0110", "0111",
    "1000", "1001", "1010", "1011",
    "1100", "1101", "1110", "1111"
};
const char t8[8][8] =
{
    "000", "001", "010",
    "011", "100", "101",
    "110", "111"
};
const int maxn = 400000+10;
void trans(char *num)
{
    string temp;
    // char temp[maxn] = {'\0'};
    int len = strlen(num);
    int len2 = ;
    for(int i = ; i < len;i ++)
    {
        int t;
        if(num[i] >= '0' && num[i] <= '9')
            t = num[i] - '0';
        else
            t = num[i] - 'A' + 10;
        // strcat(temp, t2[t]);
        temp += t2[t];
    }
    int cnt = ;
    len = temp.length();
    // len = strlen(temp);
    int i;
    for(i = len-3; i >= ; i-=3)
        for(int j = ; j < 8; j++)
        {
            bool test = 1;
            for(int k = ; k < 3; k++)
                if(t8[j][k] != temp[i+k])
                {
                    test = ;
                    break;
                }
            if(test)
            {
                num[cnt++] = j+'0';
                break;
            }
        }
    if(i == -1)
        for(int j = ; j < 7;j ++)
        {
            bool test = 1;
            for(int i = 1; i < 3; i++)
            {
                if(t8[j][i] != temp[i-1])
                {
                    test = ;
                    break;
                }
            }
            if(test)
            {
                num[cnt++] = j+'0';
                break;
            }
        }
    else if(i == -2)
        for(int j = ; j < 7;j ++)
        {
            if(t8[j][2] == temp[])
            {
                num[cnt++] = j+'0';
                break;
            }
        }
    num[cnt] = '\0';
}
int main()
{
#ifdef DEBUG
    // freopen("input", "r", stdin);       //从input文件中读入
    // freopen("output", "w", stdout);     //输出到output文件
#endif
    int n;
    cin >> n;
    char num[maxn];
    for(int i = ; i < n;i ++)
    {
        scanf("%s", num);
        trans(num);
        int len = strlen(num);
        int j;
        for(j = len-1; j >= ; j--)
            if(num[j] != '0')
                break;
        for(;j >= ; j--)
            cout << num[j];
        cout << endl;
    }
    return ;
}

十六进制转十进制

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <bitset>
using namespace std;
// 大数,内存处理
#define INF 0x3f3f3f3f
#define lln long long int
#define MEM(a) memset(a, 0, sizeof(a))
#define MEMM(a) memset(b, -1, sizeof(b))
#define DEB(x, n) cout << (x) << " " << (n) << endl
#define CR printf("\n")
// 调试用
    template <class Type>
void debug(Type a[], int len)
{
    for(int i = ; i < len ; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}
int main()
{
#ifdef DEBUG
    // freopen("input", "r", stdin);       //从input文件中读入
    // freopen("output", "w", stdout);     //输出到output文件
#endif
    lln a;
    while(scanf("%I64X", &a) != EOF)
    {
        printf("%I64d\n", a);
    }
    return ;
}

十进制转十六进制

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <bitset>
using namespace std;
// 大数,内存处理
#define INF 0x3f3f3f3f
#define lln long long int
#define MEM(a) memset(a, 0, sizeof(a))
#define MEMM(a) memset(b, -1, sizeof(b))
#define DEB(x, n) cout << (x) << " " << (n) << endl
#define CR printf("\n")
// 调试用
    template <class Type>
void debug(Type a[], int len)
{
    for(int i = ; i < len ; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}
int main()
{
#ifdef DEBUG
    // freopen("input", "r", stdin);       //从input文件中读入
    // freopen("output", "w", stdout);     //输出到output文件
#endif
    lln a;
    while(scanf("%I64d", &a) != EOF)
    {
        printf("%I64X\n", a);
    }
    return ;
}

由此也可以想到,如果是二进制的相关转换(数值范围允许的情况),我们可以写一个简单二进制转十六进制,然后利用sscanf来做进一步转换。

当然,正规的做法还是除二取余,逆序排列。

特殊回文数

#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
int main()
{
    int n;
    while(cin >> n) {
        for(int i = 1; i < 10;i  ++)
            for(int j = ; j < 10; j++)
                for(int k = ; k < 10; k++)
                {
                    if(((i+j)*2+k) == n)
                        printf("%d%d%d%d%d\n", i, j, k, j, i);
                }
        for(int i = 1; i < 10;i  ++)
            for(int j = ; j < 10; j++)
                for(int k = ; k < 10; k++)
                {
                    if((i+j+k) *2== n)
                    {
                        printf("%d%d%d", i, j, k);
                        printf("%d%d%d", k, j, i);
                        printf("\n");
                    }
                }
    }
    return ;
}

回文数

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i < 10;i ++)
        for(int j = ; j < 10; j++)
            printf("%d%d%d%d\n", i, j, j, i);
    return ;
}

特殊的数字


#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i < 10;i ++)
        for(int j = ; j < 10; j++)
            for(int k = ; k < 10; k++)
            {
                int ans = i *i*i + k *k*k + j *j*j;
                if(i*100+j*10+k == ans)
                    printf("%d\n", ans);
            }
    return ;
}

只要读题不出问题就没有问题了。。。不刷了