把这段代码的随机数生成改成srand time 0:#include <iostream> #include <random> #include <chrono>…
- User
把这段代码的随机数生成改成srand time 0:#include #include #include #include #include #include #include
// 初始化全局随机数引擎 std::random_device rd;
// 单线程模拟角色池和武器池抽卡的平均次数 void simulateAverageTask(int numSimulations, int roleCount, int weaponCount, std::vector& results, int threadIndex) { std::mt19937 gen(rd()); double baseRate = 0.006; const int pityStartRole = 73; const int hardPityRole = 90; const int pityStartWeapon = 63; const int hardPityWeapon = 80; double rateIncrease = 0.062; std::uniform_real_distribution<> dis(0.0, 1.0);
for (int i = 0; i < numSimulations; ++i) { int pulls = 0; bool guaranteedRole = false, guaranteedWeapon = false; int roleObtained = 0, weaponObtained = 0; int rolePullCount = 0, weaponPullCount = 0; bool falseMajorPity = false; // 先抽角色池 while (roleObtained < roleCount) { double pullRate = baseRate; if (rolePullCount >= pityStartRole) { pullRate += (rolePullCount - pityStartRole) * rateIncrease; } bool gotGold = (rolePullCount >= hardPityRole - 1) || (dis(gen) < pullRate); if (gotGold) { rolePullCount = 0; double guaranteedRate = 0.5; if (guaranteedRole || (dis(gen) < guaranteedRate)) { guaranteedRole = false; roleObtained++; } else { guaranteedRole = true; } } else { rolePullCount++; } pulls++; } // 再抽武器池 while (weaponObtained < weaponCount) { double pullRate = baseRate; if (weaponPullCount >= pityStartWeapon) { pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease; } bool gotGold = (weaponPullCount >= hardPityWeapon - 1) || (dis(gen) < pullRate); if (gotGold) { weaponPullCount = 0; double guaranteedRate = 0.75; if (guaranteedWeapon || (dis(gen) < guaranteedRate)) { guaranteedWeapon = false; weaponObtained++; } else { guaranteedWeapon = true; } } else { weaponPullCount++; } pulls++; } results[threadIndex] += pulls; }}
double calculateAveragePullsMultiThreaded(int roleCount, int weaponCount, int totalSimulations) { int numThreads = std::thread::hardware_concurrency(); if (numThreads == 0) numThreads = 1; int simulationsPerThread = totalSimulations / numThreads;
std::vector<int> results(numThreads, 0); std::vector<std::thread> threads; for (int i = 0; i < numThreads; ++i) { threads.push_back(std::thread(simulateAverageTask, simulationsPerThread, roleCount, weaponCount, std::ref(results), i)); } for (auto& th : threads) { th.join(); } int totalPulls = std::accumulate(results.begin(), results.end(), 0); return static_cast<double>(totalPulls) / totalSimulations;}
void simulateWithItemsTask(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls, bool majorPityRole, bool majorPityWeapon, std::vector& results, int threadIndex) { std::mt19937 gen(rd()); double baseRate = 0.006; const int pityStartRole = 73; const int hardPityRole = 90; const int pityStartWeapon = 63; const int hardPityWeapon = 80; double rateIncrease = 0.062; std::uniform_real_distribution<> dis(0.0, 1.0);
- Assistant
- User
209 42 C:\Users\surface\Desktop\srtool\expansions\ratecalc.cpp [Error] 'ceil' is not a member of 'std'
- Assistant
- User
为什么说#include <bits/stdc++.h>是c++万能头文件
- Assistant
#include <bits/stdc++.h>是一种在 C++ 编程竞赛和一些快速原型开发中常见的用法。下面是其被称为“C++万能头文件”的几个原因:-
涵盖全面:
bits/stdc++.h头文件几乎包含了 C++ 标准库中的所有常用头文件。这意味着你不需要逐个去包含iostream、vector、algorithm、cmath等头文件,而只需包含这一行即可获取标准库功能。 -
简化代码:在竞赛或快速开发中,速度是关键。使用这个头文件可以减少编写和维护包含语句的时间,让程序员专注于算法和逻辑本身。
-
方便使用:对于很多程序,尤其是在竞赛中所写的程序,其核心部分通常只有几十到数百行代码。在这种情况下,大而全的头文件对编译时间的增加可以忽略不计。
然而,值得注意的是:
-
可移植性:
#include <bits/stdc++.h>是 GCC 特有的实现,依赖于具体的编译器特性,这意味着它不是标准 C++ 的一部分。依赖于它的代码在其他编译器(如 MSVC 或 Clang)上可能会无法编译。 -
性能开销:包含不需要的大量头文件可能会导致编译时间的增加,特别是在大型项目中。这在竞赛之外的工程项目中可能并不理想,因为其中的许多头文件可能从未被用到。
-
可读性:使用
#include <bits/stdc++.h>会让他人(或你自己)在后期维护代码时难以立即理解具体使用了哪些功能。
总而言之,虽然在竞赛中它可以极大方便开发,但在生产和长期维护的代码中,建议显式包含真正需要的头文件,这样可以提高代码的可读性和可移植性。
-
- User
给我完整的bits/stdc++.h
- Assistant
bits/stdc++.h是一个非标准的头文件,是 GCC 特有的一种做法,在其实现中引入了几乎所有的 C++ 标准库头文件。它通常出现在编程竞赛或练习中,目的是减少开发时显式包含头文件的工作。这个头文件的具体实现可能会有所不同,甚至在不同的 GCC 版本中也会有所变化。这是一个常见的
bits/stdc++.h实现,用于 GCC 编译器:// bits/stdc++.h #include <bits/c++config.h> #include <bits/allocator.h> #include <bits/atomicfwd.h> #include <bits/locale_classes.h> #include <bits/locale_facets.h> #include <bits/ostream_insert.h> #include <bits/streambuf_iterator.h> #include <ext/atomicity.h> #include <ext/numeric_traits.h> #include <bits/indirect_array.h> #include <bits/stl_algobase.h> #include <bits/stl_alloc.h> #include <bits/stl_bvector.h> #include <bits/stl_construct.h> #include <bits/stl_deque.h> #include <bits/stl_function.h> #include <bits/stl_heap.h> #include <bits/stl_iterator.h> #include <bits/stl_list.h> #include <bits/stl_map.h> #include <bits/stl_multimap.h> #include <bits/stl_multiset.h> #include <bits/stl_numeric.h> #include <bits/stl_pair.h> #include <bits/stl_queue.h> #include <bits/stl_raw_storage_iter.h> #include <bits/stl_set.h> #include <bits/stl_stack.h> #include <bits/stl_tempbuf.h> #include <bits/stl_tree.h> #include <bits/stl_uninitialized.h> #include <bits/stl_vector.h> #include <bits/stl_vector.h> #include <bits/std_function.h> #include <bits/std_mutex.h> #include <bits/ptr_traits.h> #include <bits/unique_ptr.h> #include <bits/shared_ptr.h> #include <bits/weak_ptr.h> #include <bits/unordered_map.h> #include <bits/unordered_set.h> // Continue with many more standard library headers... - User
为什么我无法保存bits/stdc++.h,如果我想要保存它该怎么办
- Assistant
如果你在尝试创建或保存
bits/stdc++.h文件时遇到困难,这通常是由于某些原因导致的,例如权限问题或者保存路径不正确。以下是一些可能的解决方法和建议:-
确保权限:
- 确认你有权限在目标目录中创建和写入文件。如果是在系统文件夹下(比如某些操作系统的特定路径),你可能需要以管理员身份运行文本编辑器。
-
创建合适的目录结构:
bits/stdc++.h通常位于 GCC 的内部目录中,这些目录在安装 GCC 时自动生成。你可以在 GNU GCC 的头文件目录中找到这个文件,比如/usr/include/c++/<version>/bits/,其中<version>是 GCC 的版本号。- 如果目录不存在,需要先创建相应的目录结构。
-
正确的保存路径:
- 确保保存路径正确。如果你创建了一个新的文件,存放路径应与编译时的头文件查找路径匹配,可以通过设置编译器参数
-I<directory>来指定头文件目录。
- 确保保存路径正确。如果你创建了一个新的文件,存放路径应与编译时的头文件查找路径匹配,可以通过设置编译器参数
-
使用管理员权限:
- 在需要管理员权限的目录下保存文件时,可以用管理员权限启动文件编辑器。
-
检查文本编辑器:
- 确保使用的文本编辑器支持所需字符集,并且能够正确保存文件。
-
路径问题:
- 可能需要将文件存储在项目目录或其他编译器能够正确寻址的路径中,编译器通常会从几个默认目录查找头文件。
如果上面的方法仍然无法解决问题,建议在项目中手动包含所需的标准库头文件,而不是使用非标准的
bits/stdc++.h,特别是在不同编译器或不同平台上开发时,这样可以确保代码的可移植性和兼容性。 -