6-3 jmu-Java-m06 学生选修课信息的统计 (TreeSet集合,以及两个集合之间的交并补)

6-3 jmu-Java-m06 学生选修课信息的统计
一个班级有学生Student[1,1000],选修Badminton的有300人,选修Basketball的有400人,请找出:

所有有参加选修的人

既选修了Badminton又选修了Basketball的人

两门课程都没有选修的人

输入格式:
输入seed1调用函数getBadmintonList获取选修Badminton的学生的集合

输入seed2调用函数getBasketballList获取选修Basketball的学生的集合

函数实现如下

public static Set getBadmintonList(int seed){
	Set set = new TreeSet();
	Random rd = new Random(seed);
	while(set.size()<300) {
		set.add(rd.nextInt(1000)+1);
	}
	return set;		
}

public static Set getBasketballList(int seed){
	Set set = new TreeSet();
	Random rd = new Random(seed);
	while(set.size()<400) {
		set.add(rd.nextInt(1000)+1);
	}
	return set;		
}

输出格式:
输出选修badminton的学生集合

输出选修basketballn的学生集合

输出有参加选修的学生集合

输出二者都选修的学生集合

输出二者都不选修的学生集合

输入样例:
3
4

输出样例:
[4, 7, 9, 15, 17, 18, 23, 25, 35, 36, 38, 41, 42, 44, 56, 57, 62, 69, 75, 76, 77, 79, 81, 83, 84, 85, 86, 90, 92, 93, 100, 101, 103, 118, 122, 128, 129, 134, 135, 136, 138, 139, 142, 143, 150, 151, 152, 153, 154, 156, 158, 170, 171, 172, 177, 181, 185, 187, 188, 193, 194, 199, 200, 203, 208, 211, 221, 230, 235, 239, 241, 247, 251, 260, 266, 267, 268, 270, 271, 273, 278, 279, 283, 291, 293, 296, 304, 309, 313, 314, 324, 327, 329, 336, 341, 345, 346, 347, 350, 353, 354, 355, 356, 358, 361, 368, 373, 376, 378, 380, 386, 387, 388, 396, 400, 401, 404, 408, 412, 415, 417, 418, 420, 421, 422, 425, 426, 427, 431, 435, 437, 439, 444, 445, 447, 455, 460, 463, 466, 474, 479, 489, 491, 492, 494, 495, 498, 501, 508, 509, 511, 515, 526, 527, 536, 537, 539, 540, 547, 549, 550, 561, 562, 565, 572, 577, 578, 580, 582, 583, 585, 586, 587, 590, 591, 594, 599, 603, 610, 611, 612, 614, 615, 616, 621, 622, 623, 628, 631, 632, 633, 636, 639, 641, 648, 650, 655, 659, 661, 665, 670, 672, 681, 683, 684, 688, 689, 691, 693, 702, 704, 709, 710, 712, 715, 723, 724, 725, 730, 732, 734, 735, 740, 748, 750, 756, 760, 763, 767, 769, 775, 778, 780, 781, 783, 787, 788, 791, 794, 796, 799, 800, 802, 807, 815, 828, 835, 836, 844, 845, 847, 851, 854, 856, 858, 860, 862, 865, 866, 867, 872, 873, 882, 883, 885, 887, 890, 895, 896, 897, 900, 901, 905, 912, 913, 914, 917, 919, 922, 923, 925, 929, 931, 938, 940, 947, 948, 953, 957, 958, 962, 966, 971, 980, 982, 984, 989, 994, 996, 999]
[2, 4, 6, 11, 12, 13, 14, 19, 23, 24, 26, 28, 29, 30, 34, 37, 38, 39, 43, 45, 46, 47, 50, 53, 55, 57, 59, 60, 65, 67, 71, 73, 76, 81, 84, 86, 87, 91, 95, 96, 98, 105, 106, 109, 110, 113, 116, 122, 127, 128, 130, 131, 133, 136, 137, 139, 142, 145, 148, 149, 154, 155, 162, 163, 164, 165, 167, 168, 173, 174, 175, 176, 178, 179, 182, 183, 184, 186, 190, 194, 197, 204, 205, 207, 209, 210, 214, 217, 219, 224, 225, 228, 231, 237, 239, 242, 243, 246, 249, 254, 262, 264, 265, 270, 271, 276, 277, 278, 279, 281, 284, 286, 287, 291, 292, 293, 294, 295, 296, 297, 298, 304, 307, 309, 310, 313, 321, 323, 324, 325, 327, 330, 332, 333, 338, 342, 346, 348, 350, 351, 352, 358, 360, 365, 368, 370, 373, 374, 375, 376, 379, 381, 382, 383, 384, 385, 389, 390, 393, 394, 401, 407, 410, 415, 417, 419, 421, 422, 423, 427, 428, 429, 431, 434, 436, 443, 444, 447, 452, 453, 454, 458, 460, 462, 463, 464, 465, 470, 474, 476, 477, 482, 484, 487, 490, 495, 502, 505, 509, 510, 511, 513, 517, 520, 522, 524, 526, 529, 538, 541, 542, 543, 544, 547, 549, 550, 553, 558, 559, 560, 561, 562, 563, 566, 568, 569, 570, 572, 574, 577, 578, 581, 582, 583, 584, 587, 590, 592, 593, 594, 596, 601, 608, 613, 614, 615, 616, 618, 619, 639, 640, 641, 644, 645, 650, 652, 656, 657, 659, 660, 661, 663, 664, 665, 668, 670, 673, 677, 683, 684, 685, 687, 688, 689, 693, 695, 700, 703, 704, 706, 708, 709, 710, 711, 712, 717, 718, 720, 721, 725, 727, 731, 732, 738, 740, 744, 751, 753, 756, 759, 760, 764, 765, 766, 767, 768, 769, 772, 773, 775, 777, 779, 781, 783, 784, 785, 786, 787, 788, 790, 792, 795, 796, 798, 799, 802, 807, 813, 815, 816, 817, 819, 820, 823, 824, 825, 828, 832, 833, 834, 835, 836, 842, 847, 848, 854, 857, 863, 866, 868, 871, 872, 876, 878, 881, 884, 885, 887, 889, 892, 894, 896, 899, 901, 902, 903, 907, 908, 912, 917, 919, 922, 926, 927, 928, 930, 934, 935, 938, 939, 942, 945, 946, 947, 949, 952, 953, 958, 959, 962, 963, 966, 970, 981, 982, 985, 987, 989, 990, 991]
578
[2, 4, 6, 7, 9, 11, 12, 13, 14, 15, 17, 18, 19, 23, 24, 25, 26, 28, 29, 30, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 50, 53, 55, 56, 57, 59, 60, 62, 65, 67, 69, 71, 73, 75, 76, 77, 79, 81, 83, 84, 85, 86, 87, 90, 91, 92, 93, 95, 96, 98, 100, 101, 103, 105, 106, 109, 110, 113, 116, 118, 122, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 142, 143, 145, 148, 149, 150, 151, 152, 153, 154, 155, 156, 158, 162, 163, 164, 165, 167, 168, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 181, 182, 183, 184, 185, 186, 187, 188, 190, 193, 194, 197, 199, 200, 203, 204, 205, 207, 208, 209, 210, 211, 214, 217, 219, 221, 224, 225, 228, 230, 231, 235, 237, 239, 241, 242, 243, 246, 247, 249, 251, 254, 260, 262, 264, 265, 266, 267, 268, 270, 271, 273, 276, 277, 278, 279, 281, 283, 284, 286, 287, 291, 292, 293, 294, 295, 296, 297, 298, 304, 307, 309, 310, 313, 314, 321, 323, 324, 325, 327, 329, 330, 332, 333, 336, 338, 341, 342, 345, 346, 347, 348, 350, 351, 352, 353, 354, 355, 356, 358, 360, 361, 365, 368, 370, 373, 374, 375, 376, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 393, 394, 396, 400, 401, 404, 407, 408, 410, 412, 415, 417, 418, 419, 420, 421, 422, 423, 425, 426, 427, 428, 429, 431, 434, 435, 436, 437, 439, 443, 444, 445, 447, 452, 453, 454, 455, 458, 460, 462, 463, 464, 465, 466, 470, 474, 476, 477, 479, 482, 484, 487, 489, 490, 491, 492, 494, 495, 498, 501, 502, 505, 508, 509, 510, 511, 513, 515, 517, 520, 522, 524, 526, 527, 529, 536, 537, 538, 539, 540, 541, 542, 543, 544, 547, 549, 550, 553, 558, 559, 560, 561, 562, 563, 565, 566, 568, 569, 570, 572, 574, 577, 578, 580, 581, 582, 583, 584, 585, 586, 587, 590, 591, 592, 593, 594, 596, 599, 601, 603, 608, 610, 611, 612, 613, 614, 615, 616, 618, 619, 621, 622, 623, 628, 631, 632, 633, 636, 639, 640, 641, 644, 645, 648, 650, 652, 655, 656, 657, 659, 660, 661, 663, 664, 665, 668, 670, 672, 673, 677, 681, 683, 684, 685, 687, 688, 689, 691, 693, 695, 700, 702, 703, 704, 706, 708, 709, 710, 711, 712, 715, 717, 718, 720, 721, 723, 724, 725, 727, 730, 731, 732, 734, 735, 738, 740, 744, 748, 750, 751, 753, 756, 759, 760, 763, 764, 765, 766, 767, 768, 769, 772, 773, 775, 777, 778, 779, 780, 781, 783, 784, 785, 786, 787, 788, 790, 791, 792, 794, 795, 796, 798, 799, 800, 802, 807, 813, 815, 816, 817, 819, 820, 823, 824, 825, 828, 832, 833, 834, 835, 836, 842, 844, 845, 847, 848, 851, 854, 856, 857, 858, 860, 862, 863, 865, 866, 867, 868, 871, 872, 873, 876, 878, 881, 882, 883, 884, 885, 887, 889, 890, 892, 894, 895, 896, 897, 899, 900, 901, 902, 903, 905, 907, 908, 912, 913, 914, 917, 919, 922, 923, 925, 926, 927, 928, 929, 930, 931, 934, 935, 938, 939, 940, 942, 945, 946, 947, 948, 949, 952, 953, 957, 958, 959, 962, 963, 966, 970, 971, 980, 981, 982, 984, 985, 987, 989, 990, 991, 994, 996, 999]
122
[4, 23, 38, 57, 76, 81, 84, 86, 122, 128, 136, 139, 142, 154, 194, 239, 270, 271, 278, 279, 291, 293, 296, 304, 309, 313, 324, 327, 346, 350, 358, 368, 373, 376, 401, 415, 417, 421, 422, 427, 431, 444, 447, 460, 463, 474, 495, 509, 511, 526, 547, 549, 550, 561, 562, 572, 577, 578, 582, 583, 587, 590, 594, 614, 615, 616, 639, 641, 650, 659, 661, 665, 670, 683, 684, 688, 689, 693, 704, 709, 710, 712, 725, 732, 740, 756, 760, 767, 769, 775, 781, 783, 787, 788, 796, 799, 802, 807, 815, 828, 835, 836, 847, 854, 866, 872, 885, 887, 896, 901, 912, 917, 919, 922, 938, 947, 953, 958, 962, 966, 982, 989]
422
[1, 3, 5, 8, 10, 16, 20, 21, 22, 27, 31, 32, 33, 40, 48, 49, 51, 52, 54, 58, 61, 63, 64, 66, 68, 70, 72, 74, 78, 80, 82, 88, 89, 94, 97, 99, 102, 104, 107, 108, 111, 112, 114, 115, 117, 119, 120, 121, 123, 124, 125, 126, 132, 140, 141, 144, 146, 147, 157, 159, 160, 161, 166, 169, 180, 189, 191, 192, 195, 196, 198, 201, 202, 206, 212, 213, 215, 216, 218, 220, 222, 223, 226, 227, 229, 232, 233, 234, 236, 238, 240, 244, 245, 248, 250, 252, 253, 255, 256, 257, 258, 259, 261, 263, 269, 272, 274, 275, 280, 282, 285, 288, 289, 290, 299, 300, 301, 302, 303, 305, 306, 308, 311, 312, 315, 316, 317, 318, 319, 320, 322, 326, 328, 331, 334, 335, 337, 339, 340, 343, 344, 349, 357, 359, 362, 363, 364, 366, 367, 369, 371, 372, 377, 391, 392, 395, 397, 398, 399, 402, 403, 405, 406, 409, 411, 413, 414, 416, 424, 430, 432, 433, 438, 440, 441, 442, 446, 448, 449, 450, 451, 456, 457, 459, 461, 467, 468, 469, 471, 472, 473, 475, 478, 480, 481, 483, 485, 486, 488, 493, 496, 497, 499, 500, 503, 504, 506, 507, 512, 514, 516, 518, 519, 521, 523, 525, 528, 530, 531, 532, 533, 534, 535, 545, 546, 548, 551, 552, 554, 555, 556, 557, 564, 567, 571, 573, 575, 576, 579, 588, 589, 595, 597, 598, 600, 602, 604, 605, 606, 607, 609, 617, 620, 624, 625, 626, 627, 629, 630, 634, 635, 637, 638, 642, 643, 646, 647, 649, 651, 653, 654, 658, 662, 666, 667, 669, 671, 674, 675, 676, 678, 679, 680, 682, 686, 690, 692, 694, 696, 697, 698, 699, 701, 705, 707, 713, 714, 716, 719, 722, 726, 728, 729, 733, 736, 737, 739, 741, 742, 743, 745, 746, 747, 749, 752, 754, 755, 757, 758, 761, 762, 770, 771, 774, 776, 782, 789, 793, 797, 801, 803, 804, 805, 806, 808, 809, 810, 811, 812, 814, 818, 821, 822, 826, 827, 829, 830, 831, 837, 838, 839, 840, 841, 843, 846, 849, 850, 852, 853, 855, 859, 861, 864, 869, 870, 874, 875, 877, 879, 880, 886, 888, 891, 893, 898, 904, 906, 909, 910, 911, 915, 916, 918, 920, 921, 924, 932, 933, 936, 937, 941, 943, 944, 950, 951, 954, 955, 956, 960, 961, 964, 965, 967, 968, 969, 972, 973, 974, 975, 976, 977, 978, 979, 983, 986, 988, 992, 993, 995, 997, 998, 1000]

作者: 郑如滨
单位: 集美大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main{
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		Set<Integer> student= new TreeSet<Integer>();
		for(int i=1;i<1001;i++)
		{
			student.add(i);
		}
		int seed1=sc.nextInt();
		int seed2=sc.nextInt();
		//输入seed2调用函数getBasketballList获取选修Basketball的学生的集合
		Set s1=getBadmintonList(seed1);
		//输入seed1调用函数getBadmintonList获取选修Badminton的学生的集合
		Set s2=getBasketballList(seed2);
		
		System.out.println(s1);
		System.out.println(s2);
		
		//输出有参加选修的学生集合
		Set sb =new TreeSet();//Set集合里面的元素是无序不可重复的,TreeSet是有序不可重复的
		sb.addAll(s1);
		sb.addAll(s2);
		System.out.println(sb.size());
		System.out.println(sb);
		
		//输出二者都选修的学生集合
		Set sj =new TreeSet();
		sj.addAll(s1);
		sj.retainAll(s2);//将存在于集合s中但不存在于集合s2中的元素移除
		System.out.println(sj.size());
		System.out.println(sj);
	
		//输出二者都不选修的学生集合
		sj.clear();
		sj.addAll(student);
		sj.removeAll(sb);
		System.out.println(sj.size());
		System.out.println(sj);
		
		
	}
	public static Set<Integer> getBadmintonList(int seed){
		Set<Integer> set = new TreeSet<Integer>();
		Random rd = new Random(seed);
		while(set.size()<300) {
			set.add(rd.nextInt(1000)+1);
		}
		return set;		
	}
	public static Set<Integer> getBasketballList(int seed){
		Set<Integer> set = new TreeSet<Integer>();
		Random rd = new Random(seed);
		while(set.size()<400) {
			set.add(rd.nextInt(1000)+1);
		}
		return set;		
	}
	
	
	
}

Set集合(HashSet/TreeSet)

	// 并集
	set.addAll(set1);
	set.addAll(set2);


	// 交集
	set.clear();
	set.addAll(set1);
	set.retainAll(set2);

	//  补集
	set.clear();
	set.addAll(set1);
	set.removeAll(set2);

    }

List集合(ArrayList/LinkedList)

	/ 并集
	list.addAll(list1);
	for (int i = 0; i < list2.size(); i++) {
		if (!list.contains(list2.get(i)))
			list.add(list2.get(i));
	}
	

	// 交集
	list.clear();
	for (int i = 0; i < list2.size(); i++) {
		if (list1.contains(list2.get(i)))
			list.add(list2.get(i));
	}


	// 补集
	list.clear();
	for (int i = 0; i < list1.size(); i++) {
		if (!list2.contains(list1.get(i)))
			list.add(list1.get(i));
	}

你可能感兴趣的:(6-3 jmu-Java-m06 学生选修课信息的统计 (TreeSet集合,以及两个集合之间的交并补))