pat甲级真题分类-逻辑处理

本文详细解析了PAT甲级考试中涉及逻辑处理的题目,包括FilePath、Panda and PP Milk、The Judger等,涵盖了外部排序、替换选择算法等概念。通过对每个题目思路的分析,帮助考生理解和掌握解题技巧。
摘要由CSDN通过智能技术生成

1

7-3 File Path (25 分)

#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
const int N=1010;
int last[N]= {0},pre[10010]= {0};   //pre[i[存放的是i的父亲,last存放的是同一层中最后一个值,比如1234与2234在同一层,则last[1]存放的是2234,注意last的下标是按照给出的字符串之前的空格决定的

//注意last的下标表示字符串之前的空格,范围用N来表示,但是pre的下标存放的是4位数字,所以范围最少1w

unordered_map<int,int> mp;


void dfs(int x)
{
	if(x==0)
    {
		printf("0000");
		return;
	}
	dfs(pre[x]);
	printf("->%04d",x);
}

int main()
{
	int n,k,id;

	cin>>n;             //读入n

	getchar();          //吸收一个空格

	string input;

	for(int i=0; i<n; i++)
    {
		getline(cin,input);
		int cnt=0;

		while(*input.begin()==' ')
		{
			input.erase(input.begin());
			cnt++;//计算前面有几个空格
		}

		int key=stoi(input);            //将输入的数字改为字符串
		mp[key]=1;          //这个是为了防止给出的数字不在输入的目录中出现



		pre[key]=last[cnt-1];//将上一层的最后结点设置为当前结点的父节点
		last[cnt]=key;//更新当前层的最后一个结点
	}



	cin>>k;
	int val;
	while(k--)
    {
		scanf("%d",&val);
		if(mp.count(val)==0)        //如果要查询的数字在mp中不存在,则输出not found
            printf("Error: %04d is not found.",val);
		else
            dfs(val);        //否则深搜
		cout << endl;
	}
	return 0;
}

2

7-1 Panda and PP Milk (20 分)


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

/*
    这道题的核心思路在于寻找低谷,比如样例中的100和138,然后数据向两边扩散
    1.从左往右开始遍历,初始值设置为200,如果右边的值比左边大则加100;如果右边的值等于左边的值则牛奶量不变;如果右边的值比左边的值小则修改为200,这样一定是能找到低谷
    你想,第一个数设置为200,后面如果体重比它大则往上升,如果相同则区域平静,如果小则还是200,所以一定能找到低谷
    2.同样从右向左按上述方法再次遍历,同样也能找到低谷
    3.这样取同一位置中,两个vector较大的值,因为处于低谷附近所能找到的就是最终的结果

*/

const int maxn=10003;

struct node
{
    int milk;
    int weight;
}Node[maxn];

vector<int> LR,RL;

int n;

int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
    {
        cin >> Node[i].weight;
    }
    Node[0].milk=200;
    LR.push_back(Node[0].milk);

    for(int i=1; i<n; i++)
    {
        if(Node[i].weight>Node[i-1].weight)
        {
            Node[i].milk=Node[i-1].milk+100;
        }
        else if(Node[i].weight==Node[i-1].weight)
        {
            Node[i].milk=Node[i-1].milk;
        }
        else
        {
            Node[i].milk=200;
        }
        LR.push_back(Node[i].milk);
    }

    Node[n-1].milk=200;
    RL.push_back(Node[n-1].milk);

    for(int i=n-2; i>=0; i--)
    {
        if(Node[i].weight>Node[i+1].weight)
        {
            Node[i].milk=Node[i+1].milk+100;
        }
        else if(Node[i].weight==Node[i+1].weight)
        {
            Node[i].milk==Node[i+1].milk;
        }
        else
        {
            Node[i].milk=200;
        }
        RL.push_back(Node[i].milk);
    }

    reverse(RL.begin(),RL.end());

    int sum=0,temp;
    for(int i=0; i<n; i++)
    {
        temp=max(LR[i],RL[i]);
        sum+=temp;
    }
    cout << sum;
}


3

7-2 The Judger (25 分)


#include<iostream>
#include<cmath>
#include<unordered_set>
using namespace std;

/*
在样本输入1中:9是和之前的重复了,37是找不到与之前的差,40是有41-1得来,对于第5轮,1号玩家是7,2号玩家也是7,但不淘汰2是因为1号玩家在第3轮的
时候已径被淘汰,所以他输入的7就没有用

整个题目就只有两个限制条件:
1.如果该数是之前存在的不行
2.如果该数不是之前任意的两个数只差,不行

*/


unordered_set<int> s,oid;   //设置unordered_set数组,不会自动排序,基于哈希表,数据数据插入和查找的时间复杂度很低
//oid是存放已经被淘汰的人的编号


int a[15][1005];

bool jugde(int x)   //在已存在的列表中,看是否能找到x为某两个数的差
{
    unordered_set<int>::iterator it=s.begin();   //在输入的s数组中,寻找x是否存在
    for(;it!=s.end();it++)
    {
        if(s.find((*it)+x)!=s.end())   //x表示当前要查找的数,it表示遍历s的数,然后在s中查找看是否存在,如果存在则返回true,否则遍历一圈没有直接返回false
            return true;   //找的到,则直接返回true
    }
    return false;
}

int main()
{
    int q,p,n,m;
    cin >> q >> p >> n >> m;    //分别表示第1个值,第2个值,n个玩家,m个循环
    s.insert(q);
    s.insert(p);

    for(int i=0;i<n;i++)    //4个人
    {
        for(int j=0;j<m;j++)   //5个轮回
        {
            scanf("%d",&a[i][j]);  //输入数据
        }
    }

    for(int j=0;j<m;j++)    //按照轮回作为第1层for循环
    {
        for(int i=0;i<n;i++)        //按照人数作为第2层for循环
        {
            if(oid.find(i)!=oid.end())  //如果在概论中,第i号玩家已经被淘汰则直接跳过下面的内容
                continue;
            int x=a[i][j];           //将当前第j轮的第i个玩家的值输出
            if(s.find(x)!=s.end() || !jugde(x))   //如果在s中能知道x,说明之前已径有x了,或者如果在s中没有知道合适的值,则需要将第i号玩家放入到oi中
            {
                oid.insert(i);        //oid表示淘汰的人
                printf("Round #%d: %d is out.\n",j+1,i+1);  //这表示被淘汰的人,则表示该玩家在该伦中被淘汰
            //对于题中说,在一轮中,如果有多个玩家被淘汰,则按照编号的升序进行排列,我们每次放入值的时候就是按照玩家的升序进行寻找
            }
            else
            {
                s.insert(x);  //将x插入到s中,如果满足要求就把x放入到总库中
            }

        }
    }

    //下来就该输出,胜利的人的编号,或者no winners
    bool flag=false;         //设置flag为false
    for(int i=0;i<n;i++)        //遍历循环n个人
    {
        if(oid.find(i)==oid.end())      //如果在剔除人的数组中,找不到i
        {
            if(!flag)          //flag为false执行,flag为true不执行,则执行下面的语句
            {
                flag=true;   //标记flag为true
                printf("Winner(s): %d",i+1);  //输出胜利者
            }
            else
                printf(" %d",i+1);
        }
    }
    if(!flag)   //如果flag经过上面的遍历,还是false,表示所有的人都已经剔除,则直接输出no winner
        printf("No winner.");

    return 0;
}


4

7-3 Safari Park (25 分)

#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;

const int maxn=511;

int n,r,k;

vector<int> G[maxn];

int main()
{
    cin >> n >> r >> k;
    for(int i=0; i<r; i++)
    {
        int a,b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int m;
    cin >> m;
    for(int i=0; i<m; i++)
    {
        int v[maxn];
        unordered_set<int> s;
        for(int j=1; j<=n; j++)
        {
            cin >> v[j];
            s.insert(v[j]);
        }
        if(s.size()>k)
            cout << "Error: Too many species." << endl;
        else if(s.size()<k)
            cout << "Error: Too few species." << endl;
        else
        {
            int flag=false;
            for(int j=1; j<=n; j++)
            {
                if(flag)
                    break;
                for(int t=0; t<G[j].size(); t++)
                    if(v[j]==v[G[j][t]])
                        flag=1;
            }
            if(!flag)
                cout << "Yes" << endl;
            else
                cout << "No" << endl;

        }
    }
    return 0;
}

5

7-1 Prime Day (20 分)


#include<iostream>
using namespace std;

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

int main()
{
    string s;
    cin >> s;
    bool flag=false;
    for(int i=0; i<s.size(); i++)
    {
        string temp;
        temp=s.substr(i);
        int x=stoi(temp);
        if(is_prime(x))
            cout << temp << " " << "Yes" << endl;
        else
        {
            flag=true;
            cout << temp << " " << "No" << endl;
        }

    }
    if(!flag)
        cout << "All Prime!" << endl;

    return 0;
}

6

PAT 2020年春季 7-4 Replacement Selection (30 分)
When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory.

Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run.

For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }.

Your job is to implement this replacement selection algorithm.

Input Specification:

13 3
81 94 11 96 12 99 17 35 28 58 41 75 15

Sample Output:

11 81 94 96 99
12 17 28 35 41 58 75
15

题目的思路:

在这里插入图片描述

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

/*
    这道题不是按照题目原来的思路进行操作,所以这种思路只能背

*/
int main()
{
	int n,m,index = 0,ind = 0,num[100010];
	cin>>n>>m;          //一共n个数,其中只有m个数进行排序
	multiset<int> ans[1000],m1,m2;//m1为first runs,m2为second runs    c++语言中,multiset是<set>库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存在重复的数。
	//multiset具有自动排序的功能,这个m1,m2就是控制所分配的块数
    //注意这个multiset的ans数组的范围,太大就直接超范围
	for(int i=0;i<n;i++)
    {
		cin>>num[i];    //在num中先输入n个数字
		if(i<m)   //先再m1中插入,并且在ans0中插入
		{
			m1.insert(num[i]);  //在m1中输入插入数字
			ans[0].insert(num[i]);//同时将num[i]插入ans[0]
		}
	}


	for(int i=m;i<n;i++)   //从m这个位置开始访问数字num[i]
	{
		if(num[i] > *m1.begin())  //*m1.begin是最小的数
		{
			ans[index].insert(num[i]);   //装入runs[index]
			m1.erase(m1.begin());   //m1消除头一个值
			m1.insert(num[i]);  //在插入num[i]
		}
		else  //其实,这个现在想来就是,当前这个数字num[i]是不同于前一个块中的,所以要把他放入到第2个块中
		{
			m2.insert(num[i]);  //在m2中插入num[i]
			m1.erase(m1.begin());  //前面那个if已经将m1中的值,进行比较过了,所以要消除m1中的头一个值
			ans[index+1].insert(num[i]); //将当前的num[i]插入到第index+1个块中,注意这里不是index++,只是表示下一个index
		}
        if(m1.size()==0)  //如果m1为空了,则index++,将m2中的所有值给m1,清空m2,说白了其实就是以m1,m2来撼动整个地球
        {		//m1中没有可替换的(m2满了),换
			index++;    //目前runs下标
			m1 = m2;
			m2.clear();
		}
	}

    while(ans[ind].size()!=0)    //寻找他分了几块
        ind++;
        


	for(int i=0;i<ind;i++)   //按照块数进行输出数字
	{	 //打印
		int st=0;
		for(int nn:ans[i])  //这个是访问multiset二维数组的值,i从0开始一直到ind,然后for循环,从ans[i]为第1个,
		{
			if(st!=0)  //这个只是为了控制输出
                cout<<" ";
			cout << nn;  //直接输出就好
			st++;
		}

		cout<<endl; //换行
	}
	return 0;
}



7

7-3 Summit (25 分)

A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.
Now given a set of tentative arrangements, your job is to tell the organizers whether or not each area is all set.

Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 200), the number of heads in the summit, and M, the number of friendship relations. Then M lines follow, each gives a pair of indices of the heads who are friends to each other. The heads are indexed from 1 to N.
Then there is another positive integer K (≤ 100), and K lines of tentative arrangement of rest areas follow, each first gives a positive number L (≤ N), then followed by a sequence of L distinct indices of the heads. All the numbers in a line are separated by a space.

Output Specification:
For each of the K areas, print in a line your advice in the following format:

  • if in this area everyone is a direct friend of everyone, and no friend is missing (that is, no one else is a direct friend of everyone in this area), print Area X is OK
  • if in this area everyone is a direct friend of everyone, yet there are some other heads who may also be invited without breaking the ideal arrangement, print Area X may invite more people, such as H. where H is the smallest index of the head who may be invited.
  • if in this area the arrangement is not an ideal one, then print Area X needs help. so the host can provide some special service to help the heads get to know each other.

Here X is the index of an area, starting from 1 to K.
Sample Input:

8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
2 4 6
3 3 2 1

Sample Output:

Area 1 is OK.
Area 2 is OK.
Area 3 is OK.
Area 4 is OK.
Area 5 may invite more people, such as 3.
Area 6 needs help.
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;
int n, m, w;
vector<int> v[205];
int  vis[205];      

int main()
{
    scanf("%d%d", &n, &m);      //n个人,m个询问
    
    for(int i = 0; i < m; i++)      //v存放两个点之间的桥梁
    {
        int a, b;
        scanf("%d%d", &a, &b);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    
    scanf("%d", &w);        //读入总共要查询的总数
    
    for(int i = 1; i <= w; i++)
    {
        int k;
        scanf("%d", &k);
        
        memset(vis, 0, sizeof(vis));            //vis存放的是每个点所能练的边数
        
        int flag = 2; //0 不行, 1 缺人, 2可以
        
        vector<int> temp(k);
        for(int j = 0; j < k; j++)
        {
            scanf("%d", &temp[j]);          //temp存放所有要查询的值
            int id = temp[j];       //先读出头一个
            for(int z = 0; z < v[id].size(); z++)
            {
                vis[v[id][z]] ++;           //将这个点所有向连的边进行标记
            }
        }
        
        for(int j = 0; j < k; j++)          //遍历下所有要查询的点,判断每个点连的边是否为k-1
        {
            int id = temp[j];
            if(vis[id] != k - 1)
            {
                flag = 0;           //这个表示不行
                break;
            }
        }
        
        int minx;
        if(flag == 2)
        {
            for(int j = 0; j < 201; j++)   
            {
                if(vis[j] == k) 
                {
                    flag = 1;
                    minx = j;
                    break;
                }
            }
        }
        printf("Area %d ", i);
        if(flag == 0) 
            printf("needs help.\n");
        else if(flag == 1) 
            printf("may invite more people, such as %d.\n", minx);
        else 
            printf("is OK.\n");
    }
}


8

7-3 Vertex Coloring (25 分)


#include<iostream>
#include<vector>
#include<set>
using namespace std;

int n,m;

int main()
{
    cin >> n >> m;

    vector<int> path[n];

    while(m--)
    {
        int a,b;
        cin >> a >> b;
        path[a].push_back(b);

    }




    int k;
    cin >> k;
    while(k--)
    {
        set<int> color;        
        vector<int> temp(n);
        for(int i=0; i<n; i++)
        {
            cin >> temp[i];
            color.insert(temp[i]);
        }

        bool flag=true;

        for(int i=0; i<n; i++)
        {
            if(!flag)
                break;
                for(int j=0; j<path[i].size(); j++)
                    if(temp[i] == temp[path[i][j]])
                    {
                        flag=false;
                        break;
                    }
        }



        if(flag)
        {
            printf("%d-coloring\n",color.size());
        }
        else
           printf("No\n");
    }
    return 0;
}

9

7-2 Dangerous Goods Packaging (25 分)


#include<iostream>
#include<set>
using namespace std;

const int maxn=10010;

int n,m;

int L[maxn],R[maxn];
set<int> s;

void fun()
{
    for(int i=0; i<n; i++)
    {
        if(s.count(L[i]) && s.count(R[i]))
        {
            cout << "No" << endl;
            return
        }
    }
    cout << "Yes" << endl;
}

int main()
{
    cin >> n >> m;
    for(int i=0; i<n; i++)
    {
        int a,b;
        cin >> L[i] >> R[i];
    }
    while(m--)
    {
        s.clear();
        int x;
        cin >> x;
        for(int i=0; i<x; i++)
        {
            int temp;
            cin >> temp;
            s.insert(temp);
        }
        fun();
    }
}

10

7-3 Travelling Salesman Problem (25 分)


/*
Not a TS cycle这个也分成2种情况,一种是联通,一种是不连通,不连通就要输出NA,联通就要输出距离


*/

#include <iostream>
#include <cstring>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;

int g[N][N];
int v[N * 2];
bool vis[N];
int n, m, k;

int main()
{
    memset(g, 0x3f, sizeof g);      //g表示地图
    
    scanf("%d%d", &n, &m);      //n个点,m条边
    
    for(int i = 0; i < m; i++)          //构建地图
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);            //读 入 a,b,c
        g[a][b] = g[b][a] = c;      
    }

    int min_id = -1, min_dis = INF;
    
    scanf("%d", &k);            //一共k个询问
    
    for(int l = 1; l <= k; l++)
    {
        memset(vis, 0, sizeof vis);
        int cnt;
        scanf("%d", &cnt);
        
        for(int i = 0; i < cnt; i++)
            scanf("%d", &v[i]);

        bool cycle = true;
        int sum = 0;
        
        for(int i = 0; i < cnt - 1; i++)            //去判断这条路径是否是联通的,如果一旦不连通
        {
            int a = v[i], b = v[i + 1];
            if(g[a][b] == INF)
            {
                cycle = false;
                sum = -1;
                break;
            }
            sum += g[a][b];
            vis[a] = true;
        }

        for(int i = 1; i <= n; i++)         //这个去判断是否每个点都访问到
            if(!vis[i])
            {
                cycle = false;
                break;
            }

        if(v[0] != v[cnt - 1])          //这个去判断首位是否相同
            cycle = false;

        if(cycle == false)      //如果cycle是false
        {
            if(sum == -1)
                printf("Path %d: NA (Not a TS cycle)\n", l);
            else
                printf("Path %d: %d (Not a TS cycle)\n", l, sum);
        }
        else            //如果cycle
        {
            if(n == cnt - 1)
                printf("Path %d: %d (TS simple cycle)\n", l, sum);
            else
                printf("Path %d: %d (TS cycle)\n", l, sum);

            if(sum < min_dis)
            {
                min_dis = sum;
                min_id = l;
            }
        }
    }
    
    printf("Shortest Dist(%d) = %d\n", min_id, min_dis);
    return 0;
}


11

7-1 Werewolf - Simple Version (20 分)


#include<iostream>
#include<cstring>

using namespace std;

const int N=110;

int n;
int q[N];

int judge(int k,int i,int j) // 如果是假话,返回1;如果是真话,返回0
{
    int t=q[k];
    if(t>0)
    {
        if(t==i||t==j) return 1;
        return 0;
    }

    t=-t;
    if(t==i||t==j) return 0;
    return 1;
}

int main()
{
    cin>>n;         //读入n
        
    for(int i=1;i<=n;i++) cin>>q[i];            

    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            int s=judge(i,i,j)+judge(j,i,j);
            if(s!=1) continue;

            s=0;
            for(int k=1;k<=n;k++)
                s+=judge(k,i,j);

            if(s!=2) continue;

            cout<<i<<' '<<j<<endl;
            return 0;
        }
        cout<<"No Solution"<<endl;

    return 0;
}


12

1141 PAT Ranking of Institutions (25 分)

#include<iostream>
#include<vector>
#include<unordered_map>
#include<string>
#include<algorithm>

using namespace std;

struct school           //设置一个结构体
{
    string name;
    double score;
    int num;
};

bool cmp(school a,school b)
{
    if(a.score!=b.score) return a.score>b.score;
    else if(a.num!=b.num) return a.num<b.num;
    else return a.name<b.name;
}

int main()
{
    int n;
    cin>>n;         //读入n,一共n个人

    unordered_map<string,school> sch;               //sch是unorder_map

     for(int i=0;i<n;i++)
     {
        string id,sname;
        double score;
        cin>>id>>score>>sname;             //读入信息

        if(id[0]=='B') score/=1.5;
         else if(id[0]=='T') score*=1.5;

         for(auto& c:sname) c=tolower(c);

         sch[sname].name=sname;             //使用unorder_map的原因是需要通过相同的学校进行相加
         sch[sname].score+=score;
         sch[sname].num++;
     }

    vector<school> v;
    for(auto it:sch)
    {
        it.second.score = (int)(it.second.score+1e-8);//double精度 23.9999999999
        v.push_back(it.second);
    }

    sort(v.begin(),v.end(),cmp);
    cout<<v.size()<<endl;
    int rank=1;
    for(int i=0;i<v.size();i++)             
    {
        auto t=v[i];
        if(i&&t.score!=v[i-1].score) rank=i+1;
        printf("%d %s %d %d\n",rank,t.name.c_str(),(int)t.score,t.num);
    }

    return 0;
}



13

C Maximal Clique (25 分)


#include<iostream>
#include<cstring>

using namespace std;

const int N=210;

int n,m;
bool g[N][N],st[N];  //存边和已经存在的点
int vers[N];//存点

bool check_clique(int cnt)
{
    for(int i=0;i<cnt;i++) //判断是不是数组的两两都有边
        for(int j=0;j<i;j++)
            if(!g[vers[i]][vers[j]])
                return false;
    return true;
}

bool check_maximum(int cnt)
{
    memset(st,0,sizeof st);
    for(int i=0;i<cnt;i++)
        st[vers[i]]=true;

    for(int i=1;i<=n;i++)
        if(!st[i])
        {
            bool success=true;
            for(int j=0;j<cnt;j++)
                if(!g[i][vers[j]])
                {
                    success=false;
                    break;
                }
            if(success) return false;
        }
    return true;
}


int main()
{
    cin>>n>>m;          //n个点,m条边

    while(m--)          //m自减
    {
        int a,b;
        cin>>a>>b;          //a,b  标记a,b之间为true
        g[a][b]=g[b][a]=true;
    }

    int k;
    cin>>k;
    while(k--)
    {
        int cnt;
        cin>>cnt;
        for(int i=0;i<cnt;i++) cin>>vers[i];            //读入需要访问的点

        if(check_clique(cnt))
        {
            if(check_maximum(cnt)) cout<<"Yes"<<endl;
            else cout<<"Not Maximal"<<endl;
        }
        else cout<<"Not a Clique"<<endl;
    }

    return 0;
}


14

B Final Grading (25 分)


#include<iostream>
#include<cmath>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=10010;

int p,m,n;

struct node
{
    string name;
    int p,m,f,sum;

    node()
    {
        m=f=-1;
        sum=0;
    }

    void cal()
    {
        if(m>f)
            sum=round(m*0.4+f*0.6);
        else
            sum=f;
    }
};

bool cmp(node a,node b)
{
    if(a.sum!=b.sum)
        return a.sum>b.sum;
    else
        return a.name<b.name;
}

unordered_map<string,node> mp;

int main()
{
    cin >> p >> m >> n;
    string name;
    int grade;
    while(p--)
    {
        cin >> name >> grade;
        mp[name].name=name;
        mp[name].p=grade;
    }
    while(m--)
    {
        cin >> name >> grade;
        mp[name].name=name;
        mp[name].m=grade;
    }
    while(n--)
    {
        cin >> name >> grade;
        mp[name].name=name;
        mp[name].f=grade;
    }

    vector<node> v;

    for(auto item:mp)
    {
        auto temp=item.second;
        temp.cal();
        if(temp.p>=200 && temp.p<=900 && temp.sum>=60)
            v.push_back(temp);
    }

    sort(v.begin(),v.end(),cmp);

    for(int i=0; i<v.size(); i++)
    {
        cout << v[i].name << " " << v[i].p << " " << v[i].m << " " << v[i].f << " " << v[i].sum << endl;
    }

    return 0;
}

15

D First Contact (30 分)


#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=311;

unordered_map<string,int> mp;

string num[maxn];

int id;

int n,m;

vector<int> boys,girls;

bool g[maxn][maxn];

int main()
{
    cin >> n >> m;

    char as[maxn],bs[maxn];


    for(int i=0; i<m; i++)
    {
        cin >> as >> bs;
        string a=as,b=bs;

        if(a[0]=='-')
            a=a.substr(1);
        if(b[0]=='-')
            b=b.substr(1);

        if(mp.count(a)==0)
            mp[a]=++id,num[id]=a;

        if(mp.count(b)==0)
            mp[b]=++id,num[id]=b;


        int pa=mp[a],pb=mp[b];

        g[pa][pb]=g[pb][pa]=true;

        if(as[0]=='-')
            girls.push_back(pa);
        else
            boys.push_back(pa);

        if(bs[0]=='-')
            girls.push_back(pb);
        else
            boys.push_back(pb);
    }


    sort(boys.begin(),boys.end());
    boys.erase(unique(boys.begin(),boys.end()),boys.end());
    sort(girls.begin(),girls.end());
    girls.erase(unique(girls.begin(),girls.end()),girls.end());


    int k;
    cin >> k;

    while(k--)
    {
        vector<pair<string,string>> res;
        cin >> as >> bs;
        string x=as,y=bs;

        if(x[0]=='-')
           x=x.substr(1);

        if(y[0]=='-')
            y=y.substr(1);

        vector<int> pa=boys,pb=boys;

        if(as[0]=='-')
            pa=girls;
        if(bs[0]=='-')
            pb=girls;

        int a=mp[x],b=mp[y];

        for(auto c:pa)
            for(auto d:pb)
                if(c!=a && c!=b && d!=a && d!=b && g[a][c] && g[c][d] && g[d][b])
                    res.push_back({num[c],num[d]});

        cout << res.size() << endl;
        sort(res.begin(),res.end());

        for(int i=0; i<res.size(); i++)
            cout << res[i].first << " " << res[i].second << endl;


    }

    return 0;

}

16

Cut Integer


#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        string s,s1,s2;
        cin >> s;
        s1=s.substr(0,s.size()/2);
        s2=s.substr(s.size()/2);

        if(stoi(s1)*stoi(s2) &&stoi(s)%(stoi(s1)*stoi(s2))==0)          //前面那个判断是除数不能为0
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

17

Chain the Ropes


#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=10010;


int a[maxn];

int n;

int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
        cin >> a[i];
    sort(a,a+n);

    int sum=a[0];

    for(int i=1; i<n; i++)
    {
        sum=(sum+a[i])/2;
    }

    cout << sum;

    return 0;
}

18

Damn Single


#include<iostream>
#include<unordered_map>
#include<set>
using namespace std;

const int maxn=50010;

int n;

int main()
{
    cin >> n;
    unordered_map<int,int> mp;
    while(n--)
    {
        int a,b;
        cin >> a >> b;
        mp[a]=b;
        mp[b]=a;
    }
    int k;
    cin >> k;
    set<int> s,s1;
    while(k--)
    {
        int x;
        cin >> x;
        s.insert(x);
    }
    for(set<int>::iterator item=s.begin(); item!=s.end(); item++)
    {
        if(s.find(mp[*item])==s.end())
            s1.insert(*item);
    }
    cout << s1.size() << endl;
    for(set<int>::iterator item=s1.begin(); item!=s1.end(); item++)
    {
        if(item!=s1.begin())
            cout << " ";
        printf("%05d",*item);
    }
        

    return 0;
}

19

Friend Numbers


#include<iostream>
#include<set>
using namespace std;

const int maxn=10010;

set<int> s;

int v[maxn];

int n;

int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
    {
        int x;
        cin >> x;
        int sum=0;
        while(x)
        {
            sum+=x%10;
            x/=10;
        }
        s.insert(sum);
    }

    cout << s.size() << endl;
    for(set<int>::iterator item=s.begin(); item!=s.end(); item++)
    {
        if(item!=s.begin())
            cout << " ";
        cout << *item;
    }
    return 0;
}

20

Come on! Let’s C



#include<iostream>
#include<unordered_map>
using namespace std;

const int maxn=10010;

int n;

unordered_map<int,int> mp;

bool st[maxn];

bool isprime(int x)
{
    if(x==0 || x==1)
        return false;

    for(int i=2; i*i<=x; i++)
        if(x%i==0)
            return false;

    return true;
}

int main()
{
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        int x;
        cin >> x;
        mp[x]=i;
    }

    int m;
    cin >> m;

    for(int i=0; i<m; i++)
    {
        int x;
        cin >> x;
        if(st[x]==false)
        {
            if(mp.count(x)==0)
            {
                printf("%04d: Are you kidding?\n",x);
            }
            else
            {
                if(mp[x]==1)
                     printf("%04d: Mystery Award\n",x);
                else if(isprime(mp[x]))
                      printf("%04d: Minion\n",x);
                else
                    printf("%04d: Chocolate\n",x);
                st[x]=true;
            }

        }
        else
        {
            printf("%04d: Checked\n",x);
        }
    }
    return 0;
}

21

Eddington Number


#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=100010;

int n;

int a[maxn];

int main()
{
    cin >> n;
    for(int i=0; i<n; i++)
        cin >> a[i];
    sort(a,a+n);

    for(int i=n; i>=0; i--)
        if(a[n-i]>i)
        {
            cout << i;
            return 0;
        }
    cout << 0;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值