Compare commits

...

3 Commits

Author SHA1 Message Date
dc8c323910 feat(P8901): 优化农夫游戏逻辑并改进性能
- 移除不必要的数组a,改用变量存储最小值
- 添加对偶数的特殊处理,直接计算t[i]值
- 修改游戏结果判断逻辑,基于模4运算决定获胜者
- 清理调试代码和注释
2025-09-21 17:01:24 +08:00
82d3e19015 refactor(test.cpp): 移除蒙特卡洛模拟代码,保留空主函数
清理不再使用的代码,简化测试文件结构
2025-09-21 16:42:27 +08:00
367e1927a5 feat: 添加两个新的算法实现文件
添加了vowel-spellchecker.cpp和P8901.cpp两个算法实现文件。P8901.cpp实现了基于筛法的素数处理功能,包含输入处理和主逻辑
2025-09-21 16:41:00 +08:00
3 changed files with 79 additions and 67 deletions

View File

@ -0,0 +1,3 @@
int main(){
}

73
src/9/21/P8901.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <print>
#include <vector>
using ll = int64_t;
const ll maxa=5e6+5;
std::vector<ll> t;
std::bitset<maxa> isp;
static inline void solve(){
ll n;
std::cin>>n;
// a.clear();
// a.resize(n+1);
ll minn = 1e9+7;
ll a=-1;
for(ll i=1;i<=n;i++){
ll tmp;
std::cin>>tmp;
if(t[tmp]<minn){
minn=t[tmp];
a=tmp;
}
}
if(!(a%4)){
std::cout<<"Farmer Nhoj\n";
}else{
std::cout<<"Farmer John\n";
}
}
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
isp.set();
for(ll i=2;i<isp.size();i++){
if(!isp[i])continue;
for(ll j=2;i*j<isp.size();j++){
isp[i*j]=false;
}
}
// for(ll i=1;i<=100;i++)if(isp[i])std::cout<<i<<"\n";
t.resize(maxa);
for(ll i=1;i<t.size();i++){
if((i&1)==0){
t[i]=i/4;
continue;
}
ll nt=0,ni=i;
while(ni>=4){
if(isp[ni]){
t[i]=nt;
break;
}
nt++;
ni-=4;
}
// printf("t[%lld]=%lld\n",i,t[i]);
}
// for(ll i=1;i<=100;i++)printf("t[%lld]=%lld\n",i,t[i]);
ll T;
std::cin>>T;
while(T--){
solve();
}
}

View File

@ -1,67 +1,3 @@
#include <iostream> int main(){
#include <vector>
#include <random>
#include <algorithm>
#include <iomanip>
// 使用 C++11 的随机数生成工具,比 rand() 质量更高
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
/**
* @brief [min, max]
* @param min
* @param max
* @return
*/
double random_double(double min, double max) {
std::uniform_real_distribution<double> dist(min, max);
return dist(rng);
}
int main() {
// 设置 IO 加速,在 OI/ACM 竞赛中常用
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
// --- 问题参数 ---
const double L = 3.0; // 木棍总长度
const int NUM_TRIALS = 10000000; // 模拟试验次数,次数越多结果越接近理论值
int success_count = 0; // 能够构成三角形的成功次数
// --- 蒙特卡洛模拟 ---
for (int i = 0; i < NUM_TRIALS; ++i) {
// 1. 在木棍上随机选择两个断点
double break1 = random_double(0.0, L);
double break2 = random_double(0.0, L);
// 2. 确定两个断点的位置,方便计算三段长度
double p1 = std::min(break1, break2);
double p2 = std::max(break1, break2);
// 3. 计算三段的长度
double seg1 = p1;
double seg2 = p2 - p1;
double seg3 = L - p2;
// 4. 检查是否能构成三角形
// 充要条件:任意一边的长度都小于总长度的一半
double half_L = L / 2.0;
if (seg1 < half_L && seg2 < half_L && seg3 < half_L) {
success_count++;
}
}
// --- 输出结果 ---
double probability = static_cast<double>(success_count) / NUM_TRIALS;
std::cout << "--- 蒙特卡洛模拟解决木棍折断问题 ---" << std::endl;
std::cout << "木棍长度 (L): " << L << std::endl;
std::cout << "模拟次数 (N): " << NUM_TRIALS << std::endl;
std::cout << "成功次数: " << success_count << std::endl;
std::cout << std::fixed << std::setprecision(6);
std::cout << "模拟得到的概率 P ≈ " << probability << std::endl;
std::cout << "理论概率 P = 0.25" << std::endl;
return 0;
} }