山东理工大学第四届团体程序设计天梯赛暨第六届“中国高校计算机大赛-团体程序设计天梯赛”校内选拔赛 题解

74 篇文章 12 订阅
53 篇文章 8 订阅

R7-1 后天 (5 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

int main()
{
    int n;
    cin>>n;
    if(n>=1&&n<=5) cout<<n+2<<endl;
    else if(n==6) cout<<1<<endl;
    else if(n==7) cout<<2<<endl;
    return 0;
}

R7-2 整除光棍 (20 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;

bool vis[N];
int dp[N];

int main()
{
    int x;
    cin>>x;
    int tot=0;
    ll sum=0;
    while(sum<x){
        sum=sum*10+1;
        tot++;
    }
    while(1){
        cout<<sum/x<<"";
        sum%=x;
        if(!sum) break;
        sum=sum*10+1;
        tot++;
    }
    cout<<" "<<tot<<endl;
    return 0;
}

R7-3 判断素数 (10 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

bool judge(int x){
    bool flag=0;
    if(x==0||x==1) return 0;
    else if(x==2) return 1;
    else{
        for(int i=2;i<=sqrt(x);i++){
            if(x%i==0){
                flag=1;
                break;
            }
        }
        if(flag) return 0;
        else return 1;
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(judge(n)) puts("Yes");
        else puts("No");
    }
    return 0;
}

R7-4 调和平均 (10 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

double dp[N];

int main()
{
    int n;
    cin>>n;
    double sum=0;
    for(int i=1;i<=n;i++){
        cin>>dp[i];
        sum+=1.0/dp[i];
    }
    sum/=n;
    printf("%.2f\n",1.0/sum);
    return 0;
}

R7-5 猫是液体 (5 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

double dp[N];

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

R7-6 情人节 (15 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

char s[N][11];
int tot=1;
char s1[11];
char s2[11];

int main()
{
    while(cin>>s[tot]){
        if(!strcmp(s[tot],".")) break;
        if(tot==2) strcpy(s1,s[tot]);
        else if(tot==14)  strcpy(s2,s[tot]);
        tot++;
    }
    tot--;
    if(tot>=14) printf("%s and %s are inviting you to dinner...\n",s1,s2);
    else if(tot>1&&tot<13) printf("%s is the only one for you...\n",s1);
    else cout<<"Momo... No one is for you ..."<<endl;
    return 0;
}

R7-7 N个数求和 (20 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a,b;
    char c;
    cin>>a>>c>>b;
    int x=a;
    int y=b;
    int g;
    for(int i=2;i<=n;i++){
        cin>>a>>c>>b;
        g=__gcd(b*x+a*y,b*y);
        x=(b*x+a*y)/g;
        y=b*y/g;
    }
    int p=x/y;
    x-=p*y;
    if(!p){
        if(!x) cout<<0<<endl;
        else cout<<x<<"/"<<y<<endl;
    }
    else{
        if(!x) cout<<p<<endl;
        else cout<<p<<" "<<x<<"/"<<y<<endl;
    }
    return 0;
}

R7-8 倒数第N个字符串 (15 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e4 + 10;
using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    ll len=pow(26,n);
    len-=k;
    char s[11];
    ll tot=0;
    for(ll i=1;i<=n;i++){
        ll p=len%26;
        s[tot++]=p+'a';
        len/=26;
    }
    for(ll i=tot-1;i>=0;i--){
        cout<<s[i];
    }
    cout<<endl;
    return 0;
}

R7-9 关于堆的判断 (25 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e6 + 10;
using namespace std;

int dp[N];
int n;
int tot;
int t;

void up(){
    int now = tot;
    while(now!=1&&dp[now]<dp[now/2])
        swap(dp[now],dp[now/2]),now/=2;
}
void add(){
    dp[++tot] = t;
    up();
}

map<int,int>id;

int main()
{
    int m,n;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&t);
        add();
    }
    for(int i=1;i<=n;i++){
        //printf("%d ",dp[i]);
        id[dp[i]] = i;
    }
    for(int i=1;i<=m;i++){
        int k;
        scanf("%d",&k);
        char str[33];
        scanf("%s",str);
        if(str[0] == 'a'){
            int c;
            char a[33],b[33];
            scanf("%d",&c);
            scanf("%s%s",a,b);
            //puts(b);
            if(id[k]/2 == id[c]/2){
                puts("T");
            }
            else puts("F");
        }
        else {
            char p[33],y[33],v[33];
            scanf("%s%s",p,v);
            if(p[0] == 'a'||v[0] == 'p'){
                scanf("%s",y);
                int h ;scanf("%d",&h);
                if(v[0] == 'p')swap(h,k);
                int fa = id[k]/2;
                if(fa == id[h]){

                    puts("T");
                }
                else puts("F");
            }
            else {
                if(id[k] == 1)puts("T");
                else puts("F");
            }
        }
    }
    return 0;
}

R7-10 人以群分 (25 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e5 + 10;
using namespace std;

int dp[N];

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>dp[i];
    sort(dp+1,dp+1+n);
    int sum1=0;
    int sum2=0;
    if(n%2==0){
        for(int i=1;i<=n/2;i++) sum1+=dp[i];
        for(int i=n/2+1;i<=n;i++) sum2+=dp[i];
        cout<<"Outgoing #: "<<n/2<<endl;
        cout<<"Introverted #: "<<n/2<<endl;
        cout<<"Diff = "<<sum2-sum1<<endl;
    }
    else{
        int len=n/2;
        for(int i=1;i<=len;i++) sum1+=dp[i];
        for(int i=n;i>=n-len;i--) sum2+=dp[i];
        cout<<"Outgoing #: "<<n/2+1<<endl;
        cout<<"Introverted #: "<<n/2<<endl;
        cout<<"Diff = "<<sum2-sum1<<endl;
    }
    return 0;
}

R7-11 分而治之 (25 分)

法一:

#include <bits/stdc++.h>
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define ll long long
using namespace std;

bool is_(ll p){
    if(p==1)return false;
    rep(i,2,sqrt(p))if(p%i==0)return false;
    return true;
}
const int N = 1e5+10;
int tot ,arr[N],t;
void up(){
    int now = tot;
    while(now!=1&&arr[now]<arr[now/2])swap(arr[now],arr[now/2]),now/=2;
}
void add(){
    arr[++tot] = t;
    up();
}
map<int,int>id;
int deg[10001];
bool st[10001];
pair<int,int>f[10001];
void solve(){
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,1,m){
        int u,v;
        scanf("%d%d",&u,&v);
        f[i] = {u,v};
    }
    int q;
    scanf("%d",&q);
    rep(i,1,q){
        memset(st,0,sizeof st);
        int k;
        scanf("%d",&k);
        int c;
        rep(j,1,k){
            scanf("%d",&c);
            st[c] = true;
        }
        bool flag = true;
        rep(j,1,m){
            int x = f[j].first,y = f[j].second;
            if(st[x]||st[y])continue;
            else {
                flag = false;
                break;
            }
        }
        printf("%s\n",flag == true?"YES":"NO");
    }
}
int main()
{
    solve();
    return 0;
}

法二:

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-9
#define x first
#define y second
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 1e5 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;

int n,m;
int mp[N][2];

void solve(){
    mem(mp,0);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>mp[i][0]>>mp[i][1];
    }
    int k;
    cin>>k;
    while(k--){
        set<int>st;
        int t;
        cin>>t;
        while(t--){
            int x;
            cin>>x;
            st.insert(x);
        }
        int tot=0;
        for(int i=0;i<m;i++){
            if(st.count(mp[i][0])==1||st.count(mp[i][1])==1) tot++;
        }
        if(tot==m) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
	return 0;
}

R7-12 简单计算器 (25 分)

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
const int N = 1e5 + 10;
using namespace std;

stack<int>st;
stack<char>op;

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int k;
        cin>>k;
        st.push(k);
    }
    for(int i=1;i<n;i++){
        char c;
        cin>>c;
        op.push(c);
    }
    while(!op.empty()){
        int b=st.top(); st.pop();
        int a=st.top(); st.pop();
        char c=op.top(); op.pop();
        int sum=0;
        if(c=='+') sum=a+b;
        else if(c=='-') sum=a-b;
        else if(c=='*') sum=a*b;
        else if(c=='/') {
            if(!b) {
                printf("ERROR: %d/0\n",a);
                return 0;
            }
            sum=a/b;
        }
        st.push(sum);
    }
    int top=st.top();
    cout<<top<<endl;
    return 0;
}

R7-13 垃圾箱分布 (30 分)

R7-14 二叉搜索树的结构 (30 分)

#include <bits/stdc++.h>
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define ll long long
using namespace std;

bool is_(ll p){
    if(p==1)return false;
    rep(i,2,sqrt(p))if(p%i==0)return false;
    return true;
}
const int N = 1e5+10;
int tot,arr[N],t;
struct node{
    int l,r;
    int data;
};
node a[N];
map<int,int> fa,depth;
void add(int id){
    if(tot<id){a[++tot] = {0,0,t};return ; }
    if(a[id].data<t){
        if(!a[id].r)a[id].r = ++tot,a[tot] = {0,0,t},fa[t] = id,depth[t] = depth[a[id].data]+1;
        else add(a[id].r);
    }
    else {
        if(!a[id].l)a[id].l = ++tot,a[tot] = {0,0,t},fa[t] = id,depth[t] = depth[a[id].data]+1;
        else add(a[id].l);
    }
    return ;
}
void solve(){
    int n;
    scanf("%d",&n);
    rep(i,1,n){
        scanf("%d",&t);
        add(1);
    }
    int q;
    scanf("%d",&q);
    while(q--){
        int x,y;
        char a1[55],a2[44],a3[44],a4[44],a5[44],a6[44];
        scanf("%d",&x);
        scanf("%s",a1);
        if(a1[0] == 'a'){
            scanf("%d",&y);
            scanf("%s%s",a2,a3);
            if(a3[0] == 's'){
                if(fa[x]&&fa[x] == fa[y]){puts("Yes"); }
                else puts("No");
                continue;
            }
            scanf("%s%s%s",a4,a5,a6);
            if(depth[x]&&depth[x] == depth[y])puts("Yes");
            else puts("No");
        }
        else {
            scanf("%s%s",a2,a3);
            if(a3[0] == 'p'){
                scanf("%s",a4);
                scanf("%d",&y);
                if(fa[y]){
                    if(a[fa[y]].data == x ){
                    puts("Yes");
                    continue;
                    }
                }
                puts("No");
            }
            else if(a3[0] == 'l'){
                scanf("%s%s",a4,a5);
                scanf("%d",&y);
                if(fa[x]){
                    if(a[fa[x]].data == y&&a[fa[x]].l&&a[a[fa[x]].l].data == x){
                        puts("Yes");continue;
                    }
                }
                puts("No");
            }
            else if(a3[0] == 'r'&&a3[1] == 'i'){
                scanf("%s%s",a4,a5);
                scanf("%d",&y);
                if(fa[x]){
                    if(a[fa[x]].data == y&&a[fa[x]].r&&a[a[fa[x]].r].data == x){
                        puts("Yes");continue;
                    }
                }
                puts("No");
            }
            else {
                if(a[1].data == x)puts("Yes");
                else puts("No");
            }
        }
    }
}
int main()
{
    solve();
    return 0;
}

法二

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-9
#define x first
#define y second
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 1e6 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;

vector<int>vp;

int maxn=0;

struct node{
    int num;
    int id;
    node *l,*r;
    node(int x,int y){
        num=x;
        id=y;
        l=r=nullptr;
    }
};

void Insert(node *&root,int num,int id){
    if(!root){
        maxn=max(maxn,id);
        root=new node(num,id);
    }
    else if(root->num>num) Insert(root->r,num,2*id+1);
    else Insert(root->l,num,2*id);
}

void order(node *root){
    queue<node*> que;
    que.push(root);
    bool flag=0;
    while(que.size()){
        auto top=que.front();
        que.pop();
        if(!flag){
            cout<<top->num;
            flag=1;
        }
        else cout<<" "<<top->num;
        if(top->l) que.push(top->l);
        if(top->r) que.push(top->r);
    }
}

void solve(){
    int n;
    cin>>n;
    node *root=nullptr;
    int k;
    for(int i=0;i<n;i++){
        cin>>k;
        Insert(root,k,1);
    }
    order(root);
    cout<<endl;
    if(maxn==n) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
	return 0;
}

R7-15 传送门 (30 分)

待补

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值