以下是一个可能的解决方案:\n\nc++\n#include <iostream>\n#include <set>\n#include <random>\n\nint main() {\n std::set<int> numbers; \n std::random_device rd; \n std::mt19937 gen(rd()); \n std::uniform_int_distribution<int> dis(1, 100); \n\n // 生成 100 个随机整数并插入到 set 中 \n while (numbers.size() < 100) { \n int num = dis(gen); \n numbers.insert(num); \n } \n\n // 输出排序后的数字 \n for (int num : numbers) { \n std::cout << num << " "; \n } \n std::cout << std::endl; \n\n return 0; \n}\n\n\n这个程序首先创建了一个空的std::set<int>来保存随机整数。然后,使用std::random_device来生成一个随机数种子,并使用std::mt19937来生成随机数引擎。接下来,使用std::uniform_int_distribution来生成1到100之间的随机整数。然后,使用一个循环来生成100个随机整数,并将它们插入到set中。由于set的性质,它会自动去重并进行排序。最后,使用一个循环来输出排序后的数字。


原文地址: https://www.cveoy.top/t/topic/pLnA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录