XTU 程序设计实践模拟考试题1

题目链接:点击此处打开链接
题号分别是:1248/1249/1250/1251/1252/1253。

1248思路:枚举出所有可能,判结果即可。详见代码。

AC代码如下:

#include <bits/stdc++.h>
using namespace std;
int a[3], b[3];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    scanf("%d", &T);
    while (T--){
        for (int i=0; i<3; ++i)
            scanf("%d", a+i);
        for (int i=0; i<3; ++i)
            scanf("%d", b+i);
        sort(a, a+3);
        sort(b, b+3);
        int t1=0, t2=0;
        if (a[0] == a[2])
            t1 = 1;
        else if (a[0]==a[1] || a[1]==a[2])
            t1 = 2;
        if (b[0] == b[2])
            t2 = 1;
        else if (b[0]==b[1] || b[1]==b[2])
            t2 = 2;
        if (t1==1 && t2==1){
            if (a[0] == b[0])
                printf("Draw\n");
            else if (a[0] == 1)
                printf("Alice\n");
            else if (b[0] == 1)
                printf("Bob\n");
            else if (a[0] > b[0])
                printf("Alice\n");
            else
                printf("Bob\n");
        }
        else if (t1==0 && t2==0){
            if (a[0]+a[1]+a[2] == b[0]+b[1]+b[2])
                printf("Draw\n");
            else if (a[0]+a[1]+a[2] > b[0]+b[1]+b[2])
                printf("Alice\n");
            else
                printf("Bob\n");
        }
        else if (t1==2 && t2==2){
            int i, j, k, s;
            if (a[0] == a[1])
                i=0, k=2;
            else
                i=1, k=0;
            if (b[0] == b[1])
                j=0, s=2;
            else
                j=1, s=0;
            if (a[i] == b[j]){
                if (a[k] == b[s])
                    printf("Draw\n");
                else if (a[k] == 1)
                    printf("Alice\n");
                else if (b[s] == 1)
                    printf("Bob\n");
                else if (a[k] > b[s])
                    printf("Alice\n");
                else
                    printf("Bob\n");
            }
            else if (a[i] == 1)
                printf("Alice\n");
            else if (b[j] == 1)
                printf("Bob\n");
            else if (a[i] > b[j])
                printf("Alice\n");
            else
                printf("Bob\n");
        }
        else if (t1 == 1)
            printf("Alice\n");
        else if (t2 == 1)
            printf("Bob\n");
        else if (t1 == 2)
            printf("Alice\n");
        else
            printf("Bob\n");
    }
    return 0;
}

1249思路:题目中说明了只有83个,那么暴力将83个结果输出到文件,再复制结果存储在数组里,直接输出即可。详见代码。

输出结果到文件的代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
const int maxm = 1005;
bool vis[maxn] = {true, true};

void filter(){
    for (int i=2; i<maxm; ++i)
        if (!vis[i])
            for (int j=i*i; j<maxn; j+=i)
                vis[j] = true;
}

bool is_prime(int x){
    if (0==x || 1==x)
        return false;
    int t = (int)sqrt(x+0.5);
    for (int i=2; i<=t; ++i)
        if (x%i == 0)
            return false;
    return true;
}

int main(){
    freopen("output.txt", "w", stdout);
    filter();
    int cnt = 0;
    for (int i=2; i<80000000; ++i)
        if ((i<1000005&&!vis[i]) || is_prime(i)){
            bool ok = true;
            int t = i;
            t /= 10;
            while (t){
                if (!((t<1000005&&!vis[t]) || is_prime(t))){
                    ok = false;
                    break;
                }
                t /= 10;
            }
            if (ok){
                printf("%d, ", i);++cnt;
            }
        }
    printf("\n%d\n", cnt);
    return 0;
}

AC代码如下:

#include <bits/stdc++.h>
using namespace std;
int ans[84] = {0, 2, 3, 5, 7, 23, 29, 31, 37, 53, 59,
                71, 73, 79, 233, 239, 293, 311, 313,
                317, 373, 379, 593, 599, 719, 733, 739,
                797, 2333, 2339, 2393, 2399, 2939, 3119,
                3137, 3733, 3739, 3793, 3797, 5939, 7193,
                7331, 7333, 7393, 23333, 23339, 23399, 23993,
                29399, 31193, 31379, 37337, 37339, 37397, 59393,
                59399, 71933, 73331, 73939, 233993, 239933, 293999,
                373379, 373393, 593933, 593993, 719333, 739391, 739393,
                739397, 739399, 2339933, 2399333, 2939999, 3733799, 5939333,
                7393913, 7393931, 7393933, 23399339, 29399999, 37337999,
                59393339, 73939133};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    for (int i=1; i<=83; ++i)
        printf("%d %d\n", i, ans[i]);
    return 0;
}

给出一份正式点的代码。就是利用已满足条件的素数,来寻找下一个满足条件的素数,而且只要判断已满足条件的素数末尾加一位是否是素数即可。为什么呢?因为它已经满足条件了,再分下去肯定满足条件。末尾肯定不可能为偶数,不然能被2整除,不是素数,也不可能是5,因为能被5整除。

#include <bits/stdc++.h>
using namespace std;
int p[85] = {0, 2, 3, 5, 7};

bool is_prime(int x){
    int t = int(sqrt(x+0.5));
    for (int i=2; i<=t; ++i)
        if (x%i == 0)
            return false;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int cnt=4, t;
    for (int i=1; i<=83; ++i){
        for (int j=1; j<=9; j+=2){
            t = p[i]*10+j;
            if (is_prime(t))
                p[++cnt] = t;
        }
        printf("%d %d\n", i, p[i]);
    }
    return 0;
}

1250思路:拓扑排序。不知道是什么原因,先找出一个拓扑序再处理老是T,而一边拓扑一边处理就A了。如果实在不懂拓扑排序,就去百度一下吧,这个一下子讲不清楚。详见代码。

AC代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005;
int vis[maxn], bonus[maxn];
vector<int> g[maxn];
int n, m;

int dfs(int u){
    vis[u] = -1;
    for (int v=0; v<g[u].size(); ++v){
        int t = g[u][v];
        if (vis[t] < 0)
            return -1;
        if (!vis[t])
            bonus[t] = dfs(t);
        if (bonus[t] < 0)
            return -1;
        if (bonus[u] < bonus[t]+1000)
            bonus[u] = bonus[t]+1000;
        }
    vis[u] = 1;
    return bonus[u];
}

bool toposort(){
    for (int i=1; i<=n; ++i)
        vis[i] = 0;
    for (int u=1; u<=n; ++u)
        if (!vis[u] && dfs(u)<0)
            return false;
        return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    scanf("%d", &T);
    while (T--){
        scanf("%d%d", &n, &m);
        int a, b;
        for (int i=1; i<=n; ++i){
            g[i].clear();
            bonus[i] = 888;
        }
        while (m--){
            scanf("%d%d", &a, &b);
            g[a].push_back(b);
        }
        if (toposort()){
            int sum = 0;
            for (int i=1; i<=n; ++i)
                sum += bonus[i];
            printf("%d\n", sum);
            for (int i=1; i<n; ++i)
                printf("%d ", bonus[i]);
            printf("%d\n", bonus[n]);
        }
        else{
            printf("%d\n", 888*n);
            for (int i=1; i<n; ++i)
                printf("888 ");
            printf("888\n");
        }
    }
    return 0;
}

1251思路:要求没有数和它的数码和等于n,只要将n的位数求出来,然后假设每位都为9,那么数码和就为9倍n的位数,最后只要枚举n-1到n-数码和即可。为什么呢?大于等于n,显然不可能,因为本身就不小于n,数码和必大于0。而如果小于假设的那个值,数码和就算够了,它本身也小了,故不可能。

AC代码如下:

#include <bits/stdc++.h>
using namespace std;
int n;

int sum(int x){
    int ans = 0;
    while (x){
        ans += x%10;
        x /= 10;
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    scanf("%d", &T);
    while (T--){
        scanf("%d", &n);
        int i=n-1, t=ceil(log10(n+1));
        t = n-9*t;
        for (; i>=t; --i)
            if (i+sum(i) == n)
                break;
        if (i >= t)
            puts("No");
        else
            puts("Yes");
    }
    return 0;
}

1252思路:行列最大为100,那么对于一个矩阵,判断每个元素是否需要输出即可,即只要判断它是否是它所在行列唯一的那个即可,是就需要输出,否就不需要。详见代码。

AC代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
char str[maxn][maxn];
bool vis[maxn][maxn];
int n, m;

bool check(int i, int j){
    for (int k=0; k<m; ++k)
        if (j!=k && str[i][k]==str[i][j])
            return false;
    for (int k=0; k<n; ++k)
        if (i!=k && str[k][j]==str[i][j])
            return false;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (~scanf("%d%d", &n, &m)){
        for (int i=0; i<n; ++i)
            scanf("%s", str+i);
        for (int i=0; i<n; ++i)
            for (int j=0; j<m; ++j)
                vis[i][j] = check(i, j);
        for (int i=0; i<n; ++i)
            for (int j=0; j<m; ++j)
                if (vis[i][j])
                    putchar(str[i][j]);
        putchar('\n');
    }
    return 0;
}

1253思路:n最大为1000,数据保证有解,那么最坏情况下也就只要枚举10^6次,所以直接暴力枚举即可。详见代码。

AC代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int num[maxn];
bool vis[maxn];
int n;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (~scanf("%d", &n)){
        for (int i=0; i<n; ++i)
            scanf("%d", num+i);
        memset(vis, false, sizeof(bool)*n);
        int cnt=0, ans=-1;
        bool ok = true;
        do{
            if (ok){
                for (int i=0; i<n; ++i)
                    if (!vis[i] && num[i]<=cnt){
                        vis[i] = true;
                        ++cnt;
                    }
            }
            else{
                for (int i=n-1; i>=0; --i)
                    if (!vis[i] && num[i]<=cnt){
                        vis[i] = true;
                        ++cnt;
                    }
            }
            ++ans;
            ok = !ok;
        } while (cnt != n);
        printf("%d\n", ans);
    }
    return 0;
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值