第四次双周赛

1.

基础题,直接输出即可

#include <iostream>
using namespace std;
int main()
{
    int t=3;
    while(t--)
    {
        cout<<"I'm gonna WIN!"<<'\n';
        
    }
    return 0;
}

2.

也是基础题,用字符数组记录当前数据按要求输出即可。

#include <iostream>
using namespace std;
char a[15];
int main()
{
    for(int i=1;i<=10;i++)
        cin>>a[i];
    for(int i=7;i<=10;i++)
        cout<<a[i];
    cout<<a[3];
    cout<<a[1]<<a[2];
    cout<<a[3];
    cout<<a[4]<<a[5];
    return 0;
}

3.

本题也是基础题,理清题意即可

#include <iostream>
using namespace std;
char a[10];
int main()
{
    for(int i=1;i<=5;i++)
        cin>>a[i];
    int t=(a[1]-'0')*10+a[2]-'0';
    int n=(a[4]-'0')*10+a[5]-'0';
    if(n>0)
        t++;
    if(t<=12)
    {
        cout<<"Only ";
        for(int i=1;i<=5;i++)
            cout<<a[i];
        cout<<".  Too early to Dang.";
    }
    else
    {
        for(int i=1;i<=t-12;i++)
            cout<<"Dang";
    }
    return 0;
}

4.

阶乘基础题

#include <iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    long long ans=1;
    a+=b;
    while(a!=1)
    {
        ans*=a;
        a--;
    }
    cout<<ans;
    return 0;
}

5.基础题,用数组记录即可

#include <iostream>
#include <string.h>
using namespace std;
int a[10];
char b[1005];
int main()
{
    cin>>b;
    int x=strlen(b);
    for(int i=0;i<x;i++)
    {
a[b[i]-'0']++;
    }
    for(int i=0;i<=9;i++)
    {
        if(a[i]!=0)
        {
            cout<<i<<":"<<a[i]<<'\n';
        }
    }
    return 0;
}

6.

本题也是一道比较简单的分析题,注意对数据是否合法的判断即可

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int a = 0, p = 0, b = 0, q = 0, t = 0;
    char c;
    c = getchar();
    while (c != '\n')
    {
        if (c == ' '&& t == 0)

        {
            t = 1;
        }
        else if (t == 0 && p == 0)
        {
            if (c >= '0' && c <= '9')
            {
                if (a == 0 && c == '0')
                    p = 1;
                else
                {
                    a = a * 10 + c - '0';
                }
            }
            else
                p = 1;
        }
        else if (t == 1 && q == 0)
        {
            if (c >= '0' && c <= '9')
            {
                if (b == 0 && c == '0')
                    q = 1;
                else
                {
                    b = b * 10 + c - '0';
                }
            }
            else
                q = 1;
        }
        c = getchar();
    }
    if(p==0)
    {
        if(a<1||a>1000)
            p=1;
    }
    if(q==0)
    {
        if(b<1||b>1000)
            q=1;
    }
    if (p == 0)
    {
        cout << a;
    }
    else
    {
        cout << "?";
    }
    cout << " + ";
    if (q == 0)
    {
        cout << b;
    }
    else
    {
        cout << "?";
    }
    cout << " = ";
    if (p == 0 && q == 0)
    {
        int x = a + b;
        cout << x;

    }
    else
    {
        cout << "?";
    }
    return 0;
}

7.

一道简单的模拟题,需要注意的是0也需要输出

#include <iostream>
using namespace std;
int a;
char b;
int main()
{
    cin>>a>>b;
    if(a==1)
    {
        cout<<b<<'\n'<<a-1;
    }
    else if(a>1&&a<7)
    {
        cout<<b<<'\n'<<a-1;
    }
    else
    {
        int t=6,p=1;
        a--;
        while((a-t)>=0)
        {
            a-=t;
            t+=4;
            p++;
        }
        for(int i=p;i>=1;i--)
        {
            for(int j=p-i;j>=1;j--)
                cout<<" ";
            for(int j=1;j<=2*i-1;j++)
                cout<<b;
            cout<<'\n';
        }
        for(int i=2;i<=p;i++)
        {
             for(int j=p-i;j>=1;j--)
                cout<<" ";
            for(int j=1;j<=2*i-1;j++)
                cout<<b;
            cout<<'\n';
        }
            cout<<a;
    }
    cout<<'\n';
    return 0;
}

8.

本题如果读懂题意的话非常简单,设有n行m列,去除x行y列后答案为(n-x)*(m-y);

#include <iostream>
using namespace std;
const int N=1e5+5;
int n,m,q,p,ans,u,i;
int a[N],b[N];
int main()
{
    cin>>n>>m>>q;
    while(q--)
    {
        cin>>p>>i;
        if(p==0&&a[i]==0)
        {
            n--;
            a[i]=1;
        }
        if(p==1&&b[i]==0)
        {
            m--;
            b[i]=1;
        }
    }
    cout<<n*m;
    return 0;
}

9.

本题主要考察的是并查集,但我不太会,所以用了另外一种方法,即将相互是朋友的人建成一个链,然后判断即可

#include <iostream>
using namespace std;
int n, m, k, x, y, z,b=0;
int a[105][105];
int tt[105][105];
void lian(int p,int b)//链表
{
    for(int i=1;i<=n;i++)
    {
        if(i==p)
            continue;
        if(a[i][p]==1)
        {
            if(a[i][p]==0)
                a[i][p]=1;
            if(tt[b][i]==0)
            {
                tt[b][i]=1;
                lian(i,b);
            }
        }
    }
}
int main()
{
    cin >> n >> m >> k;
    while (m--)
    {
        cin >> x >> y >> z;
        a[x][y] = z;
        a[y][x] = z;
    }
for(int i=1;i<=n;i++)
{
    for(int j=i+1;j<=n;j++)
    {
        if(a[i][j]==1)
        {
            if(tt[b][i]==0)
            {
                tt[b][i]=1;
                lian(i,b);
            }
            if(tt[b][j]==0)
            {
                tt[b][j]=1;
                lian(j,b);
            }
            b++;
        }
    }
}

    while (k--)
    {
        cin >> x >> y ;
        if (a[x][y] == 1)
        {
            cout << "No problem" << '\n';
        }
        else if (a[x][y] == 0)
        {
            int pp = 0;
for(int i=1;i<=b-1;i++)
{
    if(tt[i][x]==1)
    {
        if(tt[i][y]==1)
            pp=1;
    }
}
            if (pp == 0)
                cout << "OK" << '\n';
            else
                cout<<"No problem"<<'\n';
        }
        else
        {
            int pp = 0;
for(int i=1;i<=b-1;i++)
{
    if(tt[i][x]==1)
    {
        if(tt[i][y]==1)
            pp=1;
    }
}
            if (pp ==0)
                cout << "No way" << '\n';
            else
            {
                cout<<"OK but..."<<'\n';
            }
        }
    }

    return 0;
}

10.

本题会对进行结构题排序的话就非常简单了

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
long long n, g, k, ans,t=1,f;
struct aa
{
    int x;
    string b;
} a[10005];
bool cmp(aa p, aa y)//结构体排序函数
{
    if (p.x == y.x)
        return p.b < y.b;
    return p.x > y.x;
}
int main()
{
    cin >> n >> g >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].b >> a[i].x;
        if (a[i].x >= g)
        {
            ans += 50;
        }
        else if (a[i].x < g && a[i].x >= 60)
            ans += 20;
    }
    sort(a+1, a + n + 1, cmp);
  
    cout << ans << '\n';
    for (int i = 1; i <= n; i++)
    {
        if (t<=k)
        {
            cout <<t<<" "<< a[i].b<<" " << a[i].x << '\n';
            if (a[i].x != a[i + 1].x)
            {
                t++;
                t+=f;
                f=0;
            }
            else
            {
                f++;
            }
   
        }
   }
    return 0;

}

11.

一道比较简单的模拟题,用数组和栈都可解决

#include <iostream>
#include <stack>
using namespace std;
stack<char>q;
int a[105], n, m, p;
unsigned s;
char b[105][1005], c[100005];
int main()
{
 
    cin >> n >> m >> s;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cin >> b[i][j];
    }

    int x;
    while (1)
    {
        cin >> x;

        if (x == -1)
            break;
        else if(x>n||x<0)
            continue;
        else
        {
            if (x == 0)
            {
                if(q.size()!=0)
                {
                p++;
                c[p] = q.top();
                q.pop();
                }
            }
            else
            {
                if(a[x]!=m)
                {
                if (q.size() == s)
                {
                    p++;
                    c[p] = q.top();
                    q.pop();
                }

                    if (a[x] <=(m-1))
                    {
                        a[x]++;
                        q.push(b[x][a[x]]);
                    }
                }
                
            }

        }
    }
    for (int i = 1; i <= p; i++)
        cout << c[i];
}

12.

本题采用标记法即可,首先根据输入建立起人人间的关系,在判断两人是否有血缘关系时,对已出现的人进行标记,根据是否重复出现标记来判断。

#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
char a[N];
int n, k, f[N], x, y, z, c[N][3], ans;
void check(int x, int y)
{
    if (y <= 3)
    {
        for (int i = 1; i <= 2; i++)
        {
            if (c[x][i] != 0)
            {
                if(f[c[x][i]] == 0)
                {
                    f[c[x][i]] = 1;
                    check(c[x][i], y + 1);
        }
            else
                ans = 1;
            }
        }
    }
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> x >> a[x] >> y >> z;

        if (y != -1)
        {
            a[y] = 'M';
            c[x][1] = y;
        }
        if (z != -1)
        {
            a[z] = 'F';
            c[x][2] = z;
        }
    }
    cin >> k;
    while (k--)
    {
        ans=0;
        cin >> x >> y;
        if (a[x] == a[y])
            cout << "Never Mind" << '\n';
        else
        {
            memset(f, 0, sizeof(f));
            check(x, 0);
            check(y, 0);
            if (ans == 1)
                cout << "No" << '\n';
            else
                cout << "Yes" << '\n';
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值