2020.10.3天梯赛补题

7-9

此题在于,两个人的关系,以及他们是否存在一个人和他们的关系一样。样例100,

可以用二维数组,存两人的关系,而且前后行列的顺序需要转换。注意判断二人关系,注意共有的敌人朋友。

#include<bits/stdc++.h>
using namespace std;
int g[110][110];   //关系储存
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 0;i < m; i++ ){
        int x, y, state;
        cin >> x >> y >> state;
        g[x][y] = state;      //无论二人谁在前都要考虑
        g[y][x] = state;
    }
    for(int i = 0;i < k ; i++ ){
        int x, y, flag = 0;
        cin >> x >> y;

        if(g[x][y] == -1){                 //二人敌对,需要判断共有的关系

            for(int j = 1;j <= n; j++ ){
                if(g[x][j] * g[y][j] == 1){     // 共有的敌人或者朋友
                    flag = 1;
                    cout << "OK but...\n";
                    break;
                }
            }
            if(!flag)         //没有共有关系
            cout << "No way\n";
        }
        if(g[x][y] == 1){           //朋友关系
            cout << "No problem\n";
        }
        if(g[x][y] == 0) {          //无关系
             cout << "OK\n";
        }
    }
}

7-11

本题是考察线性表中的链表,并且将链表按照指定顺序输出。首先输入链表的首地址然后输入链表的节点数,按照n->1->n-1->2.....大体的思路

就是从最后选一个在选一个最前面的并且更换地址。我的思路是,构建一个结构体存一下地址和内容和下一个地址,然后按顺序将地址存入一个数组,

根据数组来输出内容。注意输出时候按照数组来输出地址按照地址索引输出序号最后将最后一个的地址输出-1。

#include<bits/stdc++.h>
using namespace std;
struct LNode     //构建结构体
{
    int add;
    int data;
    int next;
}List[100000];
int main()
{
    int fa, n;
    vector<int>ls;
    cin >> fa >> n;
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;            //将地址变为结构体的索引
        cin >> List[temp].data;
        cin >> List[temp].next;   //存放下一位的地址
        List[temp].add = temp;
    }
    while (fa != -1)
    {
        ls.push_back(fa);    //将地址存放进数组
        fa = List[fa].next;  //下一位地址
    }
    for (int i = 0; i < ls.size()/2; i++)
    {
        if (!i)             //控制输出格式化
            printf("%05d %d ", ls[ls.size() - 1 - i], List[ls[ls.size() - 1 - i]].data);
        else
            printf("%05d\n%05d %d ", ls[ls.size() - 1 - i], ls[ls.size() - 1 - i], List[ls[ls.size() - 1 - i]].data);
        printf("%05d\n%05d %d ", ls[i], ls[i], List[ls[i]].data);
    }
    if (ls.size() % 2 != 0)      //考虑如果总数是奇数需要输出中间的那个数
    {
        printf("%05d\n%05d %d ", ls[ls.size() / 2], ls[ls.size() / 2], List[ls[ls.size() / 2]].data);
    }
    cout << -1;     //结尾输出-1
    return 0;
}

7-12

此题是关于道路问题,只要最终任意两个城之间不存在道路,那么这次提案是合理的。所以我们只需要建立二位数组

将两个相邻的数组存储,在后面将被攻陷的城市记录下来,然后遍历记录,如果相邻的城市还存在道路那么作战失败。因为无法提交自己的代码所以借鉴一下网上的。

#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,k,t,d,f,city[10005],arr[10005][2];
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 0;i < m;i++){
        scanf("%d%d",&a,&b);    //每行记录两个相邻的城市
        arr[i][0] = a;
        arr[i][1] = b;
    }
    scanf("%d",&k);
    while(k--){
        f = 1;
        for(int i = 1;i <= n;i++){   //初始化每个城市都没被攻破
            city[i] = 1;
        }
        scanf("%d",&t);
        for(int j = 0;j < t;j++){    //记录被攻陷的城市
            scanf("%d",&d);
            city[d] = 0;
        }
        for(int j = 0;j < m;j++){
            if(city[arr[j][0]] == 1 && city[arr[j][1]] == 1){      //如果相邻的城市没有被全攻陷
                f = 0;
                printf("NO\n");
                break;
            }
        }
        if(f == 1){
            printf("YES\n");
        }
    }
}

 

posted @ 2020-10-11 21:45  xiaoxu778  阅读(131)  评论(0)    收藏  举报