0
点赞
收藏
分享

微信扫一扫

【QT】容器类的迭代

晒大太阳了 2023-12-06 阅读 51

目录

​编辑

一、试题A

二、试题B

三、试题C

四、试题D

五、试题E

六、试题F

 七、试题G

八、试题H 

九、试题 I

十、试题 J


 

一、试题A

没啥好说的,这就是一个小学数学题,36 ×30 / 10 =108

答案:108

二、试题B

%1000就是要求 2^2023后三位,因为只是一个填空题,所有我们直接用计算器就可以求出来

 答案:608

三、试题C

 可以使用暴力或者写程序的方法进行求解

#include<bits/stdc++.h>

#define num first
#define y second

using namespace std;

typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

// 十进制数转换为任意n进制数
// num:十进制数,n:转换成n进制数

string Itoa(int num,int n) 
{
    string str;
    int rem;
    char ch;
    if(num == 0)
        str = "0";
    while(num > 0) 
    {
        rem = num % n;
        ch = (rem < 10) ? (rem + '0') : (rem - 10 + 'A');
        str = ch + str;
        num /= n;
     }
    return str;
}
// 计算每一位上的和
int sum(string str) 
{
    int sum = 0;
    for(int i = 0;i < str.length();i ++) 
    {
        sum += str[i] - '0';
    }
    return sum;
}
string str1;
string str2;
int main() 
{
    int cnt = 0;
    int i = 1;
    while(1) 
    {
        str1 = Itoa(i,2);
        str2 = Itoa(i,8);
        // cout<<i<<' '<<str1<<' '<<str2<<endl;
        if(sum(str1) == sum(str2)) 
        {
            cnt ++;
            cout << "数字为" << i << endl;
            cout << str1 << endl;
            cout << str2 << endl;
            cout << "cnt的值是" << cnt << endl;
        }
        if(cnt == 23) 
        {
            cout << "找到了!!!:>" << i << endl;
            cout << str1 << endl;
            cout << str2 << endl;
            break;
        }
        i ++;
    }
return 0;
}

答案: 4169

四、试题D

约数个数模版题 

#include<bits/stdc++.h>

using namespace std;

const int N = 100010;

int primes[N], cnt;
bool st[N];

void init(int n)
{
    for (int i = 2; i <= n; i ++)
    {
        if (!st[i]) primes[cnt ++] = i;
        for (int j = 0; primes[j] * i <= n; j ++)
        {
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}

void divide(int x)
{
     int res = 0;
     for (int i = 0; i < cnt; i ++)
     {
        int p = primes[i];
        if (x % p == 0)
        {
            while (x % p == 0)
            {
                res ++;
                {
                    x /= p;
                }
            }
        }
     }
     cout << res << endl;
}

int main()
{
    init(N);
    int m = 30;
    while (m--)
    {
        int x;
        cin >> x;
        divide(x);
    }
    return 0;
}

答案: 901440

五、试题E

 经典BFS(广度优先搜索)求联通块,由题意可知要求所有0的联通块中0的个数

#include<bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;
const int N = 45;
char g[N][N];
bool st[N][N];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int ans;

void bfs(int x, int y)
{
    queue<PII> q;
    st[x][y] = true;
    q.push({x, y});
    ans++;
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int a = dx[i] + t.x, b = dy[i] + t.y;
            if (a < 0 || a >= 30 || b < 0 || b >= 40) continue;
            if (st[a][b]) continue;
            if (g[a][b] != '0') continue;
            ans++;
            st[a][b] = true;
            q.push({a, b});
        }
    }
}
int main()
{
    for (int i = 0; i < 30; i++) cin >> g[i];
    bfs(0, 0);
    cout << ans << endl;
    return 0;
}

答案:541 

六、试题F

 直接字符串,简单模拟即可,我只能说这道题就是一道经典送分题,如果这都做不出来的话那就是真白学了

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin >> s;
    for(int i = 1; i < s.size(); i ++)
        cout << s[i];
    cout << s[1];
    
    return 0;
}

 七、试题G

这道题应该和上一道题的难度不相上下吧,就是一个简单的循环遍历字符串模拟,从后往前输出第一个元音字母即可

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin >> s;
    for(int i = s.size() - 1; i >= 0; i--)
    {
        if(s[i] == 'a' || s[i] == 'o' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u')
        {
            cout << s[i] << endl;
            break;
        }
    }
    return 0;
}

八、试题H 

 简单模拟即可

#include <bits/stdc++.h>

using namespace std;

int main()
{
    
    long long int n;
    cin >> n;
    long long int sum = 11; 
    while(sum >= 10)
    {
        sum = 1;
        while(n)
        {
            if((n % 10) != 0)
                sum *= n % 10;
            n /= 10;
        }
        cout << sum << " "<< endl;
        n = sum; 
    }
    return 0;
}

九、试题 I

BFS模版题,就是加了求最大公约数的步骤 

#include<bits/stdc++.h>

#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int g[N][N];
int n, m;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int ans;
bool st[N][N];

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
void bfs(int x, int y)
{
    queue<PII> q;
    st[x][y] = true;
    q.push({x, y});
    ans++;
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int a = dx[i] + t.x, b = dy[i] + t.y;
            if (a < 1 || a > n || b < 1 || b > m) continue;
            if (st[a][b]) continue;
            if (gcd(g[a][b], g[t.x][t.y]) <= 1) continue;
            st[a][b] = true;
            ans ++;
            q.push({a, b});
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
    cin >> g[i][j];
    int x, y;
    cin >> x >> y;
    bfs(x, y);
    cout << ans << endl;
    return 0;
}

 

十、试题 J

 这个就是一个简单的滑动窗口,前缀和模版题,有一点需要注意,那就是开long long,要不然会超出范围

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    cin >> n >> k;
    long long int s[100000];
    for(int i = 1; i <= n; i ++)
    {
        cin >> s[i];
        s[i] += s[i - 1];
    }
    long long int m;
    
    for(int i = 1; i + k - 1 <= n; i ++)
    {
        m = max(s[i + k - 1] - s[i - 1],m);
    }
    cout << m << endl;
    
    return 0;
}
举报

相关推荐

0 条评论