先阅读这点代码#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
double simulateAverageSingleThread(int numSimulations, int roleCount, int weaponCount) {
srand((unsigned int)time(0)); // Initialize the random seed
code Copy code
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int totalPulls = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = 0, weaponPullCount = 0;
// 先抽角色池
while (roleObtained < roleCount) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)rand() / RAND_MAX < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || ((double)rand() / RAND_MAX < guaranteedRate)) {
guaranteedRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)rand() / RAND_MAX < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || ((double)rand() / RAND_MAX < guaranteedRate)) {
guaranteedWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
totalPulls += pulls;
}
return (double)totalPulls / numSimulations;
}
double simulateWithItemsSingleThread(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls,
int majorPityRole, int majorPityWeapon) {
srand((unsigned int)time(0)); // Initialize the random seed
code Copy code
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int successfulSimulations = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int currentMajorPityRole = majorPityRole;
int currentMajorPityWeapon = majorPityWeapon;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = initialRolePulls, weaponPullCount = initialWeaponPulls;
// 先抽角色池
while (roleObtained < roleCount && pulls < items) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)rand() / RAND_MAX < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || currentMajorPityRole || ((double)rand() / RAND_MAX < guaranteedRate)) {
guaranteedRole = 0;
currentMajorPityRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount && pulls < items) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)rand() / RAND_MAX < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || currentMajorPityWeapon || ((double)rand() / RAND_MAX < guaranteedRate)) {
guaranteedWeapon = 0;
currentMajorPityWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
if (roleObtained >= roleCount && weaponObtained >= weaponCount) {
successfulSimulations++;
}
}
return (double)successfulSimulations / numSimulations * 100.0;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s ...\n", argv[0]);
return 1;
}
code Copy code
if (strcmp(argv[1], "-average") == 0) {
if (argc < 4) {
fprintf(stderr, "Usage: %s -average <roleCount> <weaponCount>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
double averagePulls = simulateAverageSingleThread(50000, roleCount, weaponCount);
double starJades = averagePulls * 160.0; // Calculate with a floating point for more precision
printf("%.1f\n约合星琼数 %.1f\n", averagePulls, starJades); // Print both with one decimal precision
} else if (strcmp(argv[1], "-simulate") == 0) {
if (argc < 9) {
fprintf(stderr, "Usage: %s -simulate <roleCount> <weaponCount> <items> <initialRolePulls> <initialWeaponPulls> <majorPityRole> <majorPityWeapon>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
int items = atoi(argv[4]);
int initialRolePulls = atoi(argv[5]);
int initialWeaponPulls = atoi(argv[6]);
int majorPityRole = atoi(argv[7]);
int majorPityWeapon = atoi(argv[8]);
double successRate = simulateWithItemsSingleThread(50000, roleCount, weaponCount, items, initialRolePulls, initialWeaponPulls, majorPityRole, majorPityWeapon);
printf("成功率 %.3f%%\n", successRate); // Print success rate with three decimal precision
} else {
fprintf(stderr, "Invalid function specified.\n");
return 1;
}
return 0;
}
Show full message · 197 lines Assistant2024-11-08 13:58 UTC Copy response
这段代码是一个用于模拟抽卡机制的程序,通常用于模拟电子游戏中的抽卡系统,以估计获得特定角色或武器所需的平均抽卡次数或成功概率。这个代码主要实现了两个功能:
simulateAverageSingleThread 函数 :该函数用于模拟多次抽卡(如 50,000 次)以计算获得指定数量角色和武器所需的平均抽卡次数。它考虑了抽卡的保底机制,即随着不出金(稀有角色或武器)的次数增加,获得稀有物品的概率会增加。
simulateWithItemsSingleThread 函数 :这个函数模拟在给定抽卡次数(items)时,达到所需的角色和武器数量的成功率。它还考虑了初始的抽卡计数(initialRolePulls 和 initialWeaponPulls)和是否已经触发的主要保底(majorPityRole 和 majorPityWeapon)。
程序的入口是 main 函数,它根据用户输入的参数选择运行哪一个模拟功能:
请注意,这是一种蒙特卡罗模拟方法,基于随机数生成器进行概率模拟,因此结果可能会有一定的波动。
我目前使用了-O2 -s编译,是否还有方法在完全不能改变功能的情况下压缩.exe的体积,现在是19KB
Assistant2024-11-08 13:59 UTC Copy response
在完全保持功能不变的情况下,压缩生成的可执行文件(.exe)的体积是一个常见的需求。下面是一些方法,可能帮助你减少文件大小:
使用更高级别的优化 :除了 -O2,可以尝试使用 -Os,这是 gcc 的一个专门用于优化尺寸的选项,它会在不过多影响性能的情况下尽量减小代码体积。
去除调试信息 :确保没有调试信息被编译进可执行文件。通常使用 -s 参数已经去除了符号表和调试信息,但如果没有,可以使用 strip 命令来移除此类信息:
bash Copy code
strip your_program.exe
使用 UPX 压缩 :UPX(Ultimate Packer for eXecutables)是一款流行的可执行文件压缩工具,它可以显著地减小可执行文件的体积。你可以这样使用:
bash Copy code
upx --best your_program.exe
请注意,虽然这种方式压缩后会影响加载速度(解压缩需要时间),但对程序功能没有影响。
使用静态库 :如果你的程序依赖于一些动态链接库(DLL),考虑将这些功能静态链接到你的程序中,避免程序需要查找和加载外部DLL,从而可能减小体积。
代码级别优化 :虽然你提到不能改变功能,但如果允许查看库的使用或一些不必要的代码分支,有时候能通过去除冗余代码、优化算法细节来减少体积。
这些方法应该有助于进一步减小你的可执行文件的大小。不过需要注意的是,某些压缩技术(如UPX)有可能会引起某些杀毒软件的误报,因此在分发时需要做好额外的准备。
Assistant2024-11-08 14:01 UTC Copy response
在使用了 -O3、-Os 和 -s 之后,编译出的可执行文件体积已经很小了,如果你希望进一步压缩 .exe 文件的体积,可以尝试以下几种方法,虽然每种方法可能只会带来微小的改善:
检查库和依赖 :确认没有包含不必要的库或功能模块。虽说这应该在代码层面上解决,但确认仅使用必要的标准库有助于减小体积。
使用 LTO(链接时优化) :尝试启用链接时优化(Link Time Optimization)。可以通过在编译和链接时使用 -flto 选项开启。在某些情况下,这可以进一步优化代码和减小文件体积:
bash Copy code
gcc -O3 -flto -s your_program.c -o your_program.exe
更改目标架构 :确保编译器的目标架构设置为最小化代码体积,而不仅仅是兼容性。例如,如果代码将在特定的现代处理器上运行,设置优化选项如 -mtune=native。
使用更小的 C 标准库实现 :在某些嵌入式或极小化的环境中,替代标准 C 库(如 musl 或 uclibc 而非 glibc)有时会生成更小的可执行文件。
采用 UPX :正如之前提到的,使用 UPX 对可执行文件进行压缩。这会对文件加载时间有些许影响,但通常能够极大地减小文件体积。
bash Copy code
upx --best your_program.exe
手动优化代码 :虽然你提到不能改变功能,但查看一些函数是否能被更有效的实现还是有潜力的。例如,检查是否有大规模的初始化数据或静态数据可以被更紧凑地存储。
当尝试进一步压缩 .exe 文件时,达到了某个优化极限后,稀少的改变可能只会带来很小的体积优化增量。每一个方法带来的改进都会愈加困难,尤其是在如此接近底线的时候。
Assistant2024-11-08 14:01 UTC Copy response
在替换标准库以减小可执行文件体积的过程中,您需要非常小心,因为某些替代库可能会丢失部分功能,或会有兼容性问题。不过,为了帮助您达到目标,这里是一些替代常见标准库的方法:
替换标准 C 库(glibc) :
考虑使用 musl 或 uclibc 作为替代。它们设计为更小和更简洁,并且广泛用于嵌入式系统中。要使用它们,您可能需要在编译器上进行配置。
如果您使用 musl,可通过为 gcc 设置 -static -nostdlib 和指定合适版本的 musl 进行静态链接。
精简 IO 和时间功能 :
stdio.h 的功能可以手动实现,尤其是简单的输入输出可以直接使用系统调用实现。
对于 time.h,如果精度要求不高,可以直接使用系统调用获取时间。
在获取随机数方面替换 stdlib.h :
可以使用整数范围内的简单线性同余生成器(LCG)来替代 rand(),虽然它不如 rand() 复杂,但在某些应用中表现良好。
字符串操作替换 :
很多 string.h 的功能可以由手写的小函数实现,例如 strcpy、strlen 等。
对于 strcmp,如果只需基本功能,也可以通过简单的循环对比字符。
数学库替换 :
如果只用到基本的数学计算,考虑手动实现必须用到的数学函数,虽然这在某些情况下可能增加负担,但对于不复杂的函数(如简单的三角函数或基本运算),这一步可以节省不少空间。
请记住,这些替代可能都需要手动调整和代码兼容性工作。如果决定采用这些替代,请逐步进行测试,确保功能和行为与原版保持一致。这种深层次的替换工作量较大,需要认真确立更换每一个库后的功能一致性和性能测试。
Assistant2024-11-08 14:02 UTC Copy response
如果你选择替换当前使用的标准库,例如使用 musl 或 uclibc 来替代 glibc 以减少可执行文件的体积,那么在大多数情况下,你的代码不需要进行重大的更改。这是因为这些替代库通常会模仿标准库的 API,以便提供兼容性。
然而,有几点需要注意:
编译和链接设置 :
你需要确保编译和链接步骤针对新的库进行了正确的配置。例如,你可能需要指定库的路径或者使用特定编译器选项,例如 -nostdlib 来避免使用默认的 C 标准库。
特殊功能检查 :
尽管 API 相同,但某些实现细节和边缘情况处理可能不会完全一致。最好在替换库之后全面测试代码,确保所有功能都正常运作。
移植性和兼容性 :
某些替代库在特定平台上的支持可能不如标准库广泛。如果你的程序需要跨平台移植,请确认所选替代库在目标平台上有充分的支持。
手动实现的函数 :
如果你决定不使用标准库的某些部分而改用手动实现(例如,简单的输入输出或时间函数),那么你需要替换相关调用并实现这些基础功能。
示例:如果你决定不使用 stdio.h 的所有或部分功能,那么类似 printf 这样的函数调用可能要使用更直接的系统调用或者自己实现一些简单的输出格式化。
总之,如果只是切换到不同的标准库实现,理论上不大需要改动代码。配置编译器和测试将会是主要任务。但若需要用更小的手动实现替代特定功能,则需要逐步修改和测试相关代码片段。无论进行哪个方向的优化,更详尽的测试以确保功能完整性是非常重要的。
Assistant2024-11-08 14:05 UTC Copy response
线性同余生成器(LCG)是一种简单且常用的伪随机数生成器。要在你的代码中使用这个方法来生成随机数,可以按照以下步骤进行实现。下面是一个基本的 LCG 实现例子:
c Copy code
#include <stdio.h>
// 定义 LCG 的参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296 // 通常使用 2^32
static unsigned int lcg_seed = 1; // 初始种子
// 使用 LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// 设置 LCG 的初始种子
void lcg_srand(unsigned int seed) {
lcg_seed = seed;
}
int main() {
// 设置随机数种子,使用当前时间(秒)作为种子
lcg_srand((unsigned int)time(NULL));
// 生成10个随机数
for (int i = 0; i < 10; i++) {
printf("%u\n", lcg_rand());
}
return 0;
}
说明:
参数选择 :
LCG_A, LCG_C, 和 LCG_M 是 LCG 的核心参数。通常,M 取的是 2^32。参数 A 和 C 的选择决定了生成随机数的质量。
初始种子 :
用 lcg_srand(unsigned int seed) 来设置初始种子,这确保你每次运行程序时都能生成不同的随机数序列(假如种子足够随机,如系统时间)。
生成随机数 :
调用 lcg_rand() 来获取随机数。这个函数会更新内部状态并返回一个新的随机值。
使用方法 :
在程序中调用 lcg_srand() 来设置种子,然后使用 lcg_rand() 来生成伪随机数。
请记住,LCG 方法生成的随机数是伪随机数,其周期性和统计性质较实际随机数差,但在某些场合下是足够使用的。如果你需要高质量的随机数(例如加密系统),建议使用更先进的随机数生成算法或库。
Assistant2024-11-08 14:05 UTC Copy response
好的,让我们将线性同余生成器(LCG)集成到你之前给出的代码中,以替代标准库的随机数生成功能。以下是完整的代码示例:
Show full message · 226 lines Assistant2024-11-08 14:07 UTC Copy response
如果你想避免使用标准库函数 time() 来获取当前时间,您可以通过操作系统提供的不同接口来获取时间。在大多数现代操作系统上,例如 Windows 和 Linux,我们常常使用更低级的 API 来获取系统时间。以下是如何在 Windows 和 Linux 上使用低级方法来获取系统时间:
Windows
在 Windows 系统中,可以使用 Windows API 获取当前的系统时间:
c Copy code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// LCG 伪随机数生成器参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296
static unsigned int lcg_seed = 1;
// LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// 使用 Windows API 获取当前时间并设置种子
void lcg_srand_from_system_time() {
SYSTEMTIME time;
GetSystemTime(&time);
lcg_seed = (unsigned int)(time.wSecond + time.wMilliseconds);
}
int main(int argc, char* argv[]) {
lcg_srand_from_system_time(); // 初始化随机种子
// 继续原有程序逻辑...
return 0;
}
Linux
对于 Linux 和其他类 UNIX 系统,可以使用 gettimeofday 来获取时间:
c Copy code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
// LCG 伪随机数生成器参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296
static unsigned int lcg_seed = 1;
// LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// 使用 POSIX gettimeofday 获取当前时间并设置种子
void lcg_srand_from_system_time() {
struct timeval tv;
gettimeofday(&tv, NULL);
lcg_seed = (unsigned int)(tv.tv_sec + tv.tv_usec);
}
int main(int argc, char* argv[]) {
lcg_srand_from_system_time(); // 初始化随机种子
// 继续原有程序逻辑...
return 0;
}
说明
这两种方法都避开了使用标准库 time() 函数,直接从操作系统获取时间信息,并为 LCG 生成器设定种子。这种方式可以在标准库受限的环境内工作,同时保持伪随机数序列的多样性。请结合你目标部署的操作系统选择合适的方法。
Assistant2024-11-08 14:07 UTC Copy response
以下是更新后的代码,使用 Windows 的 GetSystemTime 函数来获取系统时间,并用它来为线性同余生成器(LCG)设置种子。此方法绕过了标准 C 库的 time() 函数。
Show full message · 227 lines #include <stdio.h>
#include <stdlib.h> // 仍然用于 atoi
#include <string.h>
#include <windows.h> // Windows API
// LCG 伪随机数生成器参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296
static unsigned int lcg_seed = 1;
// LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// 使用 Windows API 获取当前时间并设置种子
void lcg_srand_from_system_time() {
SYSTEMTIME time;
GetSystemTime(&time);
lcg_seed = (unsigned int)(time.wSecond + time.wMilliseconds);
}
double simulateAverageSingleThread(int numSimulations, int roleCount, int weaponCount) {
lcg_srand_from_system_time(); // 初始化随机种子
code Copy code
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int totalPulls = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = 0, weaponPullCount = 0;
// 先抽角色池
while (roleObtained < roleCount) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
totalPulls += pulls;
}
return (double)totalPulls / numSimulations;
Show full message · 215 lines Assistant2024-11-08 14:08 UTC Copy response
为了进一步减小生成的可执行文件的大小,除了更改代码逻辑或者减少库的使用之外,还可以尝试以下一些常用的方法和工具:
优化编译选项 :
使用 -Os 优化尺寸选项。
确保使用 -s 以去除符号表和调试信息。
使用 strip 命令 :
已经在使用这种方式,不过再次确认通过 strip 命令能更有效地去掉未使用的符号和信息:
bash Copy code
strip your_program.exe
进一步压缩 :
使用 UPX 进行进一步压缩,这是一个流行的可执行文件压缩器。它通常可以将文件体积显著减小:
bash Copy code
upx --best your_program.exe
手动优化代码 :
确保没有多余的函数或静态数据。
去掉未使用的代码路径,尤其是在条件编译中。
静态链接 :
如果使用的库很小,可能对某些库进行静态链接而不是动态链接,可以减少某些情况下的大小。不过这通常相反让文件变大,所以需谨慎选择。
检查库的使用 :
确认没有引入不必要的大型库。如果只需要库的少量功能,考虑自己实现相关代码。
如果已经应用了上述方法但效果不明显,则可能已经接近该代码体积优化的极限。尤其是对于像 C 这样的常见语言,编译器通常已经做了大量优化工作,因此更大的压缩和体积减少可能需要显著改变代码结构和逻辑。注意,过度压缩可能引发杀毒软件的误报,以及影响程序执行效率,权衡这两者之间的性能和体积需求是非常重要的。
你使用atoi为什么不能直接引用包括atoi的那部分
Assistant2024-11-08 14:09 UTC Copy response
在 C 语言中,atoi 是一个用于将字符串转换为整数的标准函数,它隶属于标准库头文件 <stdlib.h>。如果你希望减少对外部库的依赖,可以考虑自己实现一个简单的版本来代替 atoi。下面是如何手动实现一个基本的字符串到整数转换函数:
Show full message · 256 lines 我使用upx优化了,现在9KB,还有别的办法吗?比如我使用汇编语言
Assistant2024-11-08 14:14 UTC Copy response
使用汇编语言可以进一步减小可执行文件的体积,因为汇编允许你对代码进行更细粒度的控制,从而能去掉所有不必要的代码和数据。然而,这需要对汇编语言有较深的理解,并且重写整个程序的话工作量相当大。以下是一些关于如何继续压缩和优化可执行文件的方法:
内联汇编优化 :在关键的性能敏感代码段,可以使用内联汇编来优化这些部分。不过这通常是为了性能而非尺寸优化,且大部分编译器已经能够妥善处理。
用汇编重写关键部分 :
如果有某些代码段是执行频繁且简单的逻辑,可以尝试使用汇编自己实现。例如随机数生成器的核心逻辑。
压缩库使用 :
尽可能减少对标准库的依赖。如果实现某功能只需少数几个库函数,考虑自己用汇编实现。
检查是否仍然有不必要的库文件链接到目标文件当中。
减少数据部分 :
确保没有未使用的静态数据。编译器有时会保留比实际使用更多的静态初始化数据。
进阶编译器技术 :
搜索并使用高级的编译技术和选项,这些可能会随时间和环境有不同的效果。例如 --gc-sections 等链接器选项,只保留用到的代码段。
极限操作(仅专家建议) :
对打包器进行二次包装,利用加密/压缩手段以换取一部分执行开销。但这通常不再是靠语言和编译技术本身,而是打包技术。
编写汇编语言代码需要相当的技巧和时间,特别是如果需要通过汇编重写大量 C 代码功能。你需要一个合适的工具链,比如 NASM 或 MASM,并对于程序的执行环境有清晰明了的理解。这种方法往往用来极限优化性能或者资源受限系统的开发。对于普通的应用程序,过于追求体积的极限可能没有实际意义,相对现代化的开发方法或许能事半功倍。
如果简化核心生成器的代码(如 LCG 实现部分)至汇编那样,你的生成语言会允许你连 C/C++的诸多初始化部分及退出代码都能很大化缩小。
Assistant2024-11-08 14:14 UTC Copy response
--gc-sections 是一个链接器选项,用于删除程序中未被使用的代码和数据段(garbage collection of sections)。这是由 GNU 链接器(如 ld)提供的一种优化选项,帮助减少最终生成的可执行文件的大小。
如何使用 --gc-sections
编译时启用 :在编译和链接时,需要确保在编译过程中启用了 -ffunction-sections 和 -fdata-sections 选项。这两个选项将每个函数和数据段分别放置在单独的段中,而不是默认的组合在一起。这一细分是 --gc-sections 能够单独识别哪些段是未使用的前提。
链接时启用 :在链接步骤中使用 --gc-sections,将使链接器分析哪些段没有被其他代码引用,并将这些段从最终的可执行文件去掉。
bash Copy code
gcc -ffunction-sections -fdata-sections -Wl,--gc-sections -o your_program your_source.c
上述命令在编译和链接中进行了优化控制,确保未使用的函数和数据段能够被合适地去除。
注意事项
精细的依赖管理 :慎用 --gc-sections,确保你的代码没有使用奇特的间接调用或实例化方式来访问未明确引用的函数或数据段。否则,链接器可能会将这些虽然用到但表面上没引用的代码段不小心删掉。
调试复杂性 :因为某些未显式使用的代码可能被删除,调试时的可访问性会受到影响。确保在调试版本中不使用 --gc-sections,以便于问题排查。
启动代码和库 :某些启动和库代码可能要求特定的函数存在,即便表面上这些函数在主逻辑中没有直接被调用。如果程序有奇怪的链接问题,检查是否需要在某些编译时加入 -u 选项强制保留某些段。
--gc-sections 是优化大小方面的强大功能,通过清除未使用代码段来减少可执行文件规模,非常适合在资源紧张的环境中进一步精细化地优化程序。
Assistant2024-11-08 14:15 UTC Copy response
将完整的 C 程序转换为汇编语言是一项复杂且耗时的任务,尤其是在 C 代码中涉及较多的控制结构和标准库调用的情况下。为了显示代码如何用汇编表示,我会展示一个简单的随机数生成器(LCG)的实现和时间获取功能。
以下是如何将部分功能从 C 代码转换为 x86 汇编代码。在 Windows 环境中,用汇编实现基本的随机数生成器和使用 Windows API 的系统时间获取功能:
LCG 随机数生成器(汇编版本)
asm Copy code
section .data
LCG_A dd 1664525
LCG_C dd 1013904223
lcg_seed dd 1
section .text
global lcg_rand
lcg_rand:
; 使用 LCG 算法计算新的种子值
mov eax, [lcg_seed]
mov ecx, [LCG_A]
mul ecx
add eax, [LCG_C]
mov [lcg_seed], eax
ret
Windows 系统时间获取
asm Copy code
extern _GetSystemTime@4
section .data
timeinfo SYSTEMTIME
section .text
global lcg_srand_from_system_time
lcg_srand_from_system_time:
; 调用 GetSystemTime
sub esp, 16
lea eax, [timeinfo]
push eax
call _GetSystemTime@4
add esp, 16
; 使用秒和毫秒初始化随机种子
mov eax, [timeinfo.wSecond]
add eax, [timeinfo.wMilliseconds]
mov [lcg_seed], eax
ret
使用说明
这些代码片段演示了如何用汇编语言实现 C 程序中的两个功能,但整个程序将更加复杂且取决于使用的汇编工具和目标平台(例如 32 位或 64 位 Windows)。
工具和步骤:
汇编器 :使用 NASM 或 MASM 等汇编器将这些代码片段编译为目标文件。
链接器 :与其他必要的代码段和启动例程链接在一起形成可执行文件。
测试和调试 :由于汇编没有类型检查和高层次的语法保护,确保密切测试结果,以确保行为如期望。
完整地将所有应用程序从 C 代码转换为汇编需包括所有逻辑的实现(包括控制结构如循环和条件判断),以及库功能的替代实现。许多库功能可以是复杂的,建议仅在必要情况下进行这样的转换,并确保拥有足够的汇编背景来进行调试和维护工作。
Assistant2024-11-08 14:16 UTC Copy response
将整个 C 程序转换为汇编语言是一项复杂和耗时的任务,特别是当程序涉及到复杂的逻辑和多种库函数时。由于每个平台和汇编器的语法和指令集可能会有所不同,我可以为你展示一个基本的框架,介绍如何在汇编中实现类似的功能。但是,请注意,完整的转换需要对整个程序流程进行仔细的重新设计和汇编编写。
下面是基于 x86 平台的汇编代码框架,其中包括部分实现,比如简单的随机数生成和输入参数解析。实际实现会根据程序逻辑复杂度和环境而有差异。
我们将继续使用简化的随机数生成器和如下功能:
代码框架
asm Copy code
section .data
msg db "Usage: <program> <function> <parameters>...", 0
LCG_A dd 1664525
LCG_C dd 1013904223
lcg_seed dd 1
input_buf db 256 ; 用于存放输入参数
section .bss
num_args resb 4 ; 存储命令行参数个数
args resd 64 ; 存储指向参数的指针
section .text
global _start
; 随机数生成器
lcg_rand:
mov eax, [lcg_seed]
mov ecx, [LCG_A]
mul ecx
add eax, [LCG_C]
mov [lcg_seed], eax
ret
_start:
; 初始化命令行参数
call parse_args
; 检查参数数量是否有效
cmp dword [num_args], 3
jl show_usage
; 例子:调用随机数生成器
call lcg_rand
; 退出程序
mov eax, 1 ; sys_exit
xor ebx, ebx ; return code 0
int 0x80
show_usage:
mov eax, 4 ; sys_write
mov ebx, 1 ; file descriptor: stdout
mov ecx, msg
mov edx, 35
int 0x80
jmp _start
parse_args:
; 假设我们有系统调用/库函数来获取命令行参数
; 此处省略具体实现,如操作系统调用,读取命令行参数
ret
实现说明
随机数生成器 :使用了简单的线性同余生成器(LCG)算法。
参数处理 :在上述示例中,参数处理部分是占位符,“parse_args”部分通常需要操作系统提供命令行参数的方式处理。
系统调用 :使用 Linux 下的系统调用示例(在其他系统,如 Windows 中,需要使用不同的调用规范和 API)。
跑通所有的真实功能需要各自实现、组合每个 C 函数或逻辑,这将大幅增加代码量。因此通常建议先用汇编实现 C/C++ 中的小模块或性能密集部分以进阶学习和性能优化,而非从头到尾自下而上地通过汇编重写整个应用程序。
如果确实需要重写,建议:
逐步实现 :从关键模块开始,验证功能,然后集成。
大量注释和文档 :特别是在复杂操作和调用接口时。
频繁测试 :与原始 C 代码对比输出以确保功能一致性。
Assistant2024-11-08 14:16 UTC Copy response
将整个程序从 C 转换为汇编是一项相当复杂的任务,尤其是在功能涉及到浮点运算、复杂控制结构和库函数调用时。不过,我可以为你提供一个基于 NASM 汇编的骨架,与原始程序的功能相似,但由于解析完整功能需要非常详细且细致的工作,因此只能为你展示基本部分。以下是如何在 NASM 中实现一个骨架,用于处理随机数生成和简单的输入参数。
简化版汇编程序
由于汇编代码对平台和具体实现要求非常严格,下述代码主要用来示范如何在汇编中组织结构,而不是具体细节实现。
asm Copy code
section .data
LCG_A dd 1664525
LCG_C dd 1013904223
lcg_seed dd 1
usage_msg db "Usage: <program> <function> <parameters>...", 10, 0
section .bss
input_buffer resb 256
section .text
global _start
_start:
; 调用函数初始化种子(例如通过时间),此处略过实现
call lcg_srand
; 调用函数生成随机数演示
call lcg_rand
; 模拟退出代码
mov eax, 1 ; syscall number for exit
xor ebx, ebx ; exit code 0
int 0x80
lcg_srand:
; 在这里添加种子初始化方法,利用时间或输入,用于简单示例
; mov eax, [some_time_function]
ret
lcg_rand:
mov eax, [lcg_seed]
mov ecx, [LCG_A]
mul ecx
add eax, [LCG_C]
mov [lcg_seed], eax
ret
print_usage:
; 输出用法消息到标准输出
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor 1 is stdout
mov ecx, usage_msg
mov edx, 43
int 0x80
ret
说明
LCG 伪随机数 :简单实现了一个随机数生成器,在示例中未对外部输入进行交互或种子初始化,只是展示骨架。
I/O 操作 :示范了如何输出文本信息。对于完全的程序,还需解析命令行输入(在真实环境中需要具体实现)。
退出例程 :使用简单的 sys_exit 系统调用退出程序。
注意 :完整的程序实现包括系统接口调用、内存管理及更多复杂逻辑,需要高度计划和逐步验证正确性。在不具有丰富汇编经验的情况下,重写大型 C 项目建议获得更多汇编实践或者通过解释器、编译器前端工具逐步转换小部分代码,为达到学习目的添加适当符号解释与工程文档,促进从语言翻译到可运行程序的步调有迹可循。
完整的汇编程序可能通过反汇编工具获取更多编译器生成示例,代码中大多数内容需要根据每一部分的具体工作原理进行调整。建议了解平台架构规范和调试信息,为逐步完成的汇编项目有效奠基。
Not yet included in a published release.
#include <stdio.h>
#include <stdlib.h> // 仅用于 atoi,不能完全去掉
#include <time.h>
#include <math.h>
#include <string.h>
// LCG 伪随机数生成器参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296
static unsigned int lcg_seed = 1;
// LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// LCG 设置种子
void lcg_srand(unsigned int seed) {
lcg_seed = seed;
}
double simulateAverageSingleThread(int numSimulations, int roleCount, int weaponCount) {
lcg_srand((unsigned int)time(0)); // 初始化随机种子
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int totalPulls = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = 0, weaponPullCount = 0;
// 先抽角色池
while (roleObtained < roleCount) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
while (weaponObtained < weaponCount) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
totalPulls += pulls;
}
return (double)totalPulls / numSimulations;
}
double simulateWithItemsSingleThread(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls,
int majorPityRole, int majorPityWeapon) {
lcg_srand((unsigned int)time(0)); // 初始化随机种子
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int successfulSimulations = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int currentMajorPityRole = majorPityRole;
int currentMajorPityWeapon = majorPityWeapon;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = initialRolePulls, weaponPullCount = initialWeaponPulls;
while (roleObtained < roleCount && pulls < items) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || currentMajorPityRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
currentMajorPityRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
while (weaponObtained < weaponCount && pulls < items) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || currentMajorPityWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
currentMajorPityWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
if (roleObtained >= roleCount && weaponObtained >= weaponCount) {
successfulSimulations++;
}
}
return (double)successfulSimulations / numSimulations * 100.0;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <function> <parameters>...\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "-average") == 0) {
if (argc < 4) {
fprintf(stderr, "Usage: %s -average <roleCount> <weaponCount>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
double averagePulls = simulateAverageSingleThread(50000, roleCount, weaponCount);
double starJades = averagePulls * 160.0;
printf("%.1f\n约合星琼数 %.1f\n", averagePulls, starJades);
} else if (strcmp(argv[1], "-simulate") == 0) {
if (argc < 9) {
fprintf(stderr, "Usage: %s -simulate <roleCount> <weaponCount> <items> <initialRolePulls> <initialWeaponPulls> <majorPityRole> <majorPityWeapon>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
int items = atoi(argv[4]);
int initialRolePulls = atoi(argv[5]);
int initialWeaponPulls = atoi(argv[6]);
int majorPityRole = atoi(argv[7]);
int majorPityWeapon = atoi(argv[8]);
double successRate = simulateWithItemsSingleThread(50000, roleCount, weaponCount, items, initialRolePulls, initialWeaponPulls, majorPityRole, majorPityWeapon);
printf("成功率 %.3f%%\n", successRate);
} else {
fprintf(stderr, "Invalid function specified.\n");
return 1;
}
return 0;
}
代码说明:
引入了简单的 LCG 作为随机数生成器,以替代标准库中的 rand() 函数。
使用 lcg_srand((unsigned int)time(0)) 设置种子,以确保每次程序运行时随机序列不同。
用 lcg_rand() 生成随机数,并通过 (double)lcg_rand() / LCG_M 将其转换为 [0, 1) 范围内的浮点数。
使用此代码的随机数生成方式,你可以得到类似于原始代码的随机性,而无需 rand() 函数的支持。确保在多次测试中验证结果一致性与正确性。
#include <stdio.h>
#include <stdlib.h> // 仍然用于 atoi
#include <string.h>
#include <windows.h> // Windows API
// LCG 伪随机数生成器参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296
static unsigned int lcg_seed = 1;
// LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// 使用 Windows API 获取当前时间并设置种子
void lcg_srand_from_system_time() {
SYSTEMTIME time;
GetSystemTime(&time);
lcg_seed = (unsigned int)(time.wSecond + time.wMilliseconds);
}
double simulateAverageSingleThread(int numSimulations, int roleCount, int weaponCount) {
lcg_srand_from_system_time(); // 初始化随机种子
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int totalPulls = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = 0, weaponPullCount = 0;
// 先抽角色池
while (roleObtained < roleCount) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
totalPulls += pulls;
}
return (double)totalPulls / numSimulations;
}
double simulateWithItemsSingleThread(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls,
int majorPityRole, int majorPityWeapon) {
lcg_srand_from_system_time(); // 初始化随机种子
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int successfulSimulations = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int currentMajorPityRole = majorPityRole;
int currentMajorPityWeapon = majorPityWeapon;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = initialRolePulls, weaponPullCount = initialWeaponPulls;
// 先抽角色池
while (roleObtained < roleCount && pulls < items) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || currentMajorPityRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
currentMajorPityRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount && pulls < items) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || currentMajorPityWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
currentMajorPityWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
if (roleObtained >= roleCount && weaponObtained >= weaponCount) {
successfulSimulations++;
}
}
return (double)successfulSimulations / numSimulations * 100.0;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <function> <parameters>...\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "-average") == 0) {
if (argc < 4) {
fprintf(stderr, "Usage: %s -average <roleCount> <weaponCount>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
double averagePulls = simulateAverageSingleThread(50000, roleCount, weaponCount);
double starJades = averagePulls * 160.0;
printf("%.1f\n约合星琼数 %.1f\n", averagePulls, starJades);
} else if (strcmp(argv[1], "-simulate") == 0) {
if (argc < 9) {
fprintf(stderr, "Usage: %s -simulate <roleCount> <weaponCount> <items> <initialRolePulls> <initialWeaponPulls> <majorPityRole> <majorPityWeapon>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
int items = atoi(argv[4]);
int initialRolePulls = atoi(argv[5]);
int initialWeaponPulls = atoi(argv[6]);
int majorPityRole = atoi(argv[7]);
int majorPityWeapon = atoi(argv[8]);
double successRate = simulateWithItemsSingleThread(50000, roleCount, weaponCount, items, initialRolePulls, initialWeaponPulls, majorPityRole, majorPityWeapon);
printf("成功率 %.3f%%\n", successRate);
} else {
fprintf(stderr, "Invalid function specified.\n");
return 1;
}
return 0;
}
说明 这个代码版本应该可以在 Windows 环境中使用,不再依赖标准 C 库中的时间函数。请确保在您的系统上测试该实现以验证功能和随机性。
double simulateWithItemsSingleThread(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls,
int majorPityRole, int majorPityWeapon) {
lcg_srand_from_system_time(); // 初始化随机种子
code Copy code
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int successfulSimulations = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int currentMajorPityRole = majorPityRole;
int currentMajorPityWeapon = majorPityWeapon;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = initialRolePulls, weaponPullCount = initialWeaponPulls;
// 先抽角色池
while (roleObtained < roleCount && pulls < items) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || currentMajorPityRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
currentMajorPityRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount && pulls < items) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || currentMajorPityWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
currentMajorPityWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
if (roleObtained >= roleCount && weaponObtained >= weaponCount) {
successfulSimulations++;
}
}
return (double)successfulSimulations / numSimulations * 100.0;
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s ...\n", argv[0]);
return 1;
}
code Copy code
if (strcmp(argv[1], "-average") == 0) {
if (argc < 4) {
fprintf(stderr, "Usage: %s -average <roleCount> <weaponCount>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
double averagePulls = simulateAverageSingleThread(50000, roleCount, weaponCount);
double starJades = averagePulls * 160.0;
printf("%.1f\n约合星琼数 %.1f\n", averagePulls, starJades);
} else if (strcmp(argv[1], "-simulate") == 0) {
if (argc < 9) {
fprintf(stderr, "Usage: %s -simulate <roleCount> <weaponCount> <items> <initialRolePulls> <initialWeaponPulls> <majorPityRole> <majorPityWeapon>\n", argv[0]);
return 1;
}
int roleCount = atoi(argv[2]);
int weaponCount = atoi(argv[3]);
int items = atoi(argv[4]);
int initialRolePulls = atoi(argv[5]);
int initialWeaponPulls = atoi(argv[6]);
int majorPityRole = atoi(argv[7]);
int majorPityWeapon = atoi(argv[8]);
double successRate = simulateWithItemsSingleThread(50000, roleCount, weaponCount, items, initialRolePulls, initialWeaponPulls, majorPityRole, majorPityWeapon);
printf("成功率 %.3f%%\n", successRate);
} else {
fprintf(stderr, "Invalid function specified.\n");
return 1;
}
return 0;
#include <stdio.h>
#include <string.h>
#include <windows.h>
// 简单的 atoi 实现
int my_atoi(const char *str) {
int res = 0;
int sign = 1;
int i = 0;
// 处理字符串开头的空白符
while (str[i] == ' ') {
i++;
}
// 处理正负号
if (str[i] == '-') {
sign = -1;
i++;
} else if (str[i] == '+') {
i++;
}
// 逐位转换字符为整数
for (; str[i] != '\0'; ++i) {
if (str[i] < '0' || str[i] > '9') {
break; // 遇到非数字字符时停止
}
res = res * 10 + (str[i] - '0');
}
// 返回结果值
return sign * res;
}
// LCG 伪随机数生成器参数
#define LCG_A 1664525
#define LCG_C 1013904223
#define LCG_M 4294967296
static unsigned int lcg_seed = 1;
// LCG 生成随机数
unsigned int lcg_rand() {
lcg_seed = (LCG_A * lcg_seed + LCG_C) % LCG_M;
return lcg_seed;
}
// 使用 Windows API 获取当前时间并设置种子
void lcg_srand_from_system_time() {
SYSTEMTIME time;
GetSystemTime(&time);
lcg_seed = (unsigned int)(time.wSecond + time.wMilliseconds);
}
double simulateAverageSingleThread(int numSimulations, int roleCount, int weaponCount) {
lcg_srand_from_system_time(); // 初始化随机种子
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int totalPulls = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = 0, weaponPullCount = 0;
// 先抽角色池
while (roleObtained < roleCount) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
totalPulls += pulls;
}
return (double)totalPulls / numSimulations;
}
double simulateWithItemsSingleThread(int numSimulations, int roleCount, int weaponCount, int items, int initialRolePulls, int initialWeaponPulls,
int majorPityRole, int majorPityWeapon) {
lcg_srand_from_system_time(); // 初始化随机种子
double baseRate = 0.006;
const int pityStartRole = 73;
const int hardPityRole = 90;
const int pityStartWeapon = 63;
const int hardPityWeapon = 80;
double rateIncrease = 0.062;
int successfulSimulations = 0;
for (int i = 0; i < numSimulations; ++i) {
int pulls = 0;
int guaranteedRole = 0, guaranteedWeapon = 0;
int currentMajorPityRole = majorPityRole;
int currentMajorPityWeapon = majorPityWeapon;
int roleObtained = 0, weaponObtained = 0;
int rolePullCount = initialRolePulls, weaponPullCount = initialWeaponPulls;
// 先抽角色池
while (roleObtained < roleCount && pulls < items) {
double pullRate = baseRate;
if (rolePullCount >= pityStartRole) {
pullRate += (rolePullCount - pityStartRole) * rateIncrease;
}
int gotGold = (rolePullCount >= hardPityRole - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
rolePullCount = 0;
double guaranteedRate = 0.5;
if (guaranteedRole || currentMajorPityRole || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedRole = 0;
currentMajorPityRole = 0;
roleObtained++;
} else {
guaranteedRole = 1;
}
} else {
rolePullCount++;
}
pulls++;
}
// 再抽武器池
while (weaponObtained < weaponCount && pulls < items) {
double pullRate = baseRate;
if (weaponPullCount >= pityStartWeapon) {
pullRate += (weaponPullCount - pityStartWeapon) * rateIncrease;
}
int gotGold = (weaponPullCount >= hardPityWeapon - 1) || ((double)lcg_rand() / LCG_M < pullRate);
if (gotGold) {
weaponPullCount = 0;
double guaranteedRate = 0.75;
if (guaranteedWeapon || currentMajorPityWeapon || ((double)lcg_rand() / LCG_M < guaranteedRate)) {
guaranteedWeapon = 0;
currentMajorPityWeapon = 0;
weaponObtained++;
} else {
guaranteedWeapon = 1;
}
} else {
weaponPullCount++;
}
pulls++;
}
if (roleObtained >= roleCount && weaponObtained >= weaponCount) {
successfulSimulations++;
}
}
return (double)successfulSimulations / numSimulations * 100.0;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <function> <parameters>...\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "-average") == 0) {
if (argc < 4) {
fprintf(stderr, "Usage: %s -average <roleCount> <weaponCount>\n", argv[0]);
return 1;
}
int roleCount = my_atoi(argv[2]);
int weaponCount = my_atoi(argv[3]);
double averagePulls = simulateAverageSingleThread(50000, roleCount, weaponCount);
double starJades = averagePulls * 160.0;
printf("%.1f\n约合星琼数 %.1f\n", averagePulls, starJades);
} else if (strcmp(argv[1], "-simulate") == 0) {
if (argc < 9) {
fprintf(stderr, "Usage: %s -simulate <roleCount> <weaponCount> <items> <initialRolePulls> <initialWeaponPulls> <majorPityRole> <majorPityWeapon>\n", argv[0]);
return 1;
}
int roleCount = my_atoi(argv[2]);
int weaponCount = my_atoi(argv[3]);
int items = my_atoi(argv[4]);
int initialRolePulls = my_atoi(argv[5]);
int initialWeaponPulls = my_atoi(argv[6]);
int majorPityRole = my_atoi(argv[7]);
int majorPityWeapon = my_atoi(argv[8]);
double successRate = simulateWithItemsSingleThread(50000, roleCount, weaponCount, items, initialRolePulls, initialWeaponPulls, majorPityRole, majorPityWeapon);
printf("成功率 %.3f%%\n", successRate);
} else {
fprintf(stderr, "Invalid function specified.\n");
return 1;
}
return 0;
}
说明
my_atoi 函数 :这个函数对输入字符串进行遍历,将字符转换成整数,如果前面有空格会跳过,并能够处理正负号。
使用 my_atoi 代替之前的 atoi,从而可以避免包含 stdlib.h,进而减少程序对外部库的依赖。
这种手动实现的方式将极简化功能,可以根据需求进一步扩展,例如对越界值的处理或者错误的提示。在不使用标准库的情况下,可以有效地减少一些不必要的体积。