模拟测试题解

题目链接:https://vjudge.csgrandeur.cn/contest/557480

A OJ维护中

计算 a+b(0<=a,b<=10)。

点击查看代码
#include<iostream>
using namespace std;

int main(){
    int a,b; cin>>a>>b;
    cout<<a+b;
    return 0;
}

B 不支持python

队列报数,直接模拟即可。

点击查看代码
#include<iostream>
#include<queue>
using namespace std;
queue<int> que;
int main(){
    int n,m; cin>>n;
    for(int i=1; i<=n; i++){
        queue<int> que;
        cin>>m;
        for(int j=1; j<=m; j++) que.push(j);
        int flag=2;
        while(que.size()>3){
            if(flag==2){
                int len = que.size();
                for(int k=1; k<=len; k++){
                    if(k%2!=0) que.push(que.front());
                    que.pop();
                }
                flag=3;
            }else if(flag==3){
                int len = que.size();
                for(int k=1; k<=len; k++){
                    if(k%3!=0) que.push(que.front());
                    que.pop();
                }
                flag=2;
            }
        }
        while(que.size()>1){
            cout<<que.front()<<" "; que.pop();
        } cout<<que.front()<<endl;
    }
    return 0;
}

C 不支持python

找一个可以去重和排序的容器就可以,比如 set

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10, INF=0x3f3f3f3f;

int main(){
    int n,m,x;
    while(cin>>n>>m){
        set<int> s;
        for(int i=1; i<=n+m; i++) cin>>x, s.insert(x);
        int f=0;
        for(auto u:s){
            if(f) cout<<" ";
            cout<<u;
            f=1;
        }
        cout<<endl;
    }
    return 0;
}

D OJ维护中

编写一个C程序,实现两个分数的加减法。
分析:gcd 模板题

点击查看代码
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e4+10, inf=0x3f3f3f3f;

int gcd(int a,int b){
    return b?gcd(b, a%b):a;
}
int main() {
    char s[20];
    while(scanf("%s", s)!=EOF){
        int a,b,c,d,f=1,ans1,ans2,temp;
        char o;
        sscanf(s, "%1d/%1d%c%1d/%1d",&a,&b,&o,&c,&d);
        if(o=='+') ans1 = a*d + b*c, ans2=b*d;
        else if(o=='-') ans1 = a*d - b*c, ans2=b*d;

        if(ans1<0) f=-1, ans1=-ans1;
        temp = gcd(ans1, ans2);
        ans1 = f*ans1/temp;
        ans2 = ans2/temp;
        if(ans1==0) puts("0");
        else if(ans2==1) printf("%d\n", ans1);
        else printf("%d/%d\n",ans1,ans2);
    }
    return 0;
}

E OJ维护中

编写一个C函数mod(int n, int m),实现取模运算%
分析:数据很小,直接取模就行,这个代码不知道是好久的古董了

点击查看代码
#include<iostream>
#include<cstdio>
//#define DEBUG
using namespace std;
const int N=2e5+10, INF=0x3f3f3f3f;

int mod(int n,int m) {
    return n%m;
}
int main() {
#ifdef DEBUG
    freopen("data.in", "r", stdin);
//    freopen("data.out", "w", stdout);
#endif

    int n,m;
    while(cin>>n>>m){
        cout<<mod(n,m)<<endl;
    }
    fclose(stdin), fclose(stdout);
    return 0;
}

F 可以提交任何语言

一个西瓜的重量为 w,问是否可以将其分成两部分,使得两部分的重量都为正偶数。
分析:直接判断就行,注意细节

点击查看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int main() {
    int n;
    while(~scanf("%d",&n)) {
        if(n%2==0 && n/2+ceil(n/2)==n && n/2>1) puts("YES");
        else puts("NO");
    }
    return 0;
}

G 可以提交任何语言

计算 a+b (0≤a,b≤9).
分析:scanf 格式化处理很好

点击查看代码
#include<bits/stdc++.h>
using namespace std;

int main() {
    int t,a,b; scanf("%d", &t);
    while(t--){
        scanf("%d+%d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;
}
posted @ 2023-05-12 11:04  HelloHeBin  阅读(18)  评论(0编辑  收藏  举报