PAT练习笔记——6.2 set的常见用法详解

2019年9月PAT - 练习笔记——6.2

以下页码标注的是阅读器中实际页码,而不是书本身自印的页码。

第6章 C++标准模板库(STL)介绍

6.2 set的常见用法详解

注意

  1. std::set和std::multiset为有序序列,而hash_set以及hash_multiset为无序序列。

  2. 并交差:https://blog.csdn.net/u013095333/article/details/89322501

    1. set_union(A.begin(),A.end(),B.begin(),B.end(),inserter( C1 , C1.begin() ) );
    2. set_intersection(A.begin(),A.end(),B.begin(),B.end(),inserter( C2 , C2.begin() ));
    3. set_difference( A.begin(), A.end(),B.begin(), B.end(),inserter( C3, C3.begin() ) );

    注意!以上函数要求两个集合必须是有序的

目录

  1. A1063 Set Similarity

  1. A1063 Set Similarity

    Given two sets of integers, the similarity of the sets is defined to be N**c/N**t×100%, where N**c is the number of distinct common numbers shared by the two sets, and N**t is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

    Input Specification:

    Each input file contains one test case. Each case first gives a positive integer N (≤50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤104) and followed by M integers in the range [0,109]. After the input of sets, a positive integer K (≤2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

    Output Specification:

    For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

    Sample Input:
    3
    3 99 87 101
    4 87 101 5 87
    7 99 101 18 5 135 18 99
    2
    1 2
    1 3
    
    Sample Output:
    50.0%
    33.3%
    
    1. 我的

      #include <iostream>
      #include <vector>
      #include <set>
      
      using namespace std;
      
      const int INF = 0x7fffffff;
      
      int main(void)
      {
      	int n = 0;
      	scanf("%d", &n);
      	
      	vector<set<int>> sets(n + 1);
      	for (int i = 1;i <= n;++i) {
      		int m = 0;
      		scanf("%d", &m);
      		for (int j = 0;j < m;++j) {
      			int num = 0;
      			scanf("%d", &num);
      			sets[i].insert(num);
      		}
      		sets[i].insert(INF);
      	}
      	
      	int k = 0;
      	scanf("%d", &k);
      	for (int i = 0;i < k;++i) {
      		int set1 = 0, set2 = 0;
      		scanf("%d %d", &set1, &set2);
      		
      		int count1 = -1, count2 = -1;
      		set<int>::iterator iter1 = sets[set1].begin(), iter2 = sets[set2].begin();
      		for (;iter1 != sets[set1].end() && iter2 != sets[set2].end();) {
      			int num1 = *iter1, num2 = *iter2;
      			if (num1 == num2) {
      				++count1;
      				++iter1;
      				++iter2;
      			}
      			else if (num1 < num2) ++iter1;
      			else ++iter2;
      			
      			++count2;
      		}
      		
      		float percent = (float)count1 / count2;
      		printf("%.1f%%\n", percent * 100);
      	} 
      	
      	return 0;
      } 
      

      用set_union和set_intersection的话,测试点4会超时

    2. 《算法笔记》P246

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MOOC PAT练习是指在在线课程平台上进行的编程能力考试。它提供了一系列编程题目,旨在帮助学生提高解决问题的能力和编程技能。下面是对此练习的回答: MOOC PAT练习是一种非常有益的学习资源。它可以帮助学生在虚拟的环境中解决实际问题,并通过编程来提高解决问题的能力。通过这种方式,学生可以更好地理解和掌握编程语言和算法知识。 MOOC PAT练习不仅可以提升学生的编程能力,还可以培养学生的创新思维和团队合作精神。在解决编程题目时,学生需要思考合适的算法和数据结构,同时还需要灵活运用编程语言来实现自己的想法。这样的练习可以锻炼学生的分析问题和解决问题的能力,培养其创新和合作意识。 通过MOOC PAT练习,学生可以接触到各种各样的编程题目,涵盖了不同的难度级别和主题领域。这对于学生来说是一个很好的学习机会,可以不断地挑战自己、提高自己。此外,该练习还提供了评测功能,学生可以实时了解自己的答题情况,并获得相应的反馈和建议,这有助于他们更好地调整学习进度和方向。 总结来说,MOOC PAT练习是一个非常有价值的学习资源,它可以提升学生的编程能力、培养创新和合作意识,并且通过不断挑战和实践来提高自己。对于想要学习编程的人来说,参加MOOC PAT练习是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值