- Hello, World!
灌水
- @ 2026-5-10 15:33:08
灌水1
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <thread>
using namespace std;
// 生成一个随机整数 (范围可调)
int getRandom(int minVal = 1000, int maxVal = 9999) {
static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> dist(minVal, maxVal);
return dist(rng);
}
// 模板代码,其中的 {RANDOM} 将被替换为实际随机数
const string workerTemplate = R"(
#include <bits/stdc++.h>
#include <thread>
using namespace std;
void C(){
int qdtl=0;
while(qdtl<=1e9){
cout<<"qdtl";
qdtl++;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// 将输出重定向到独立文件
string filename = "output_{RANDOM}.txt";
freopen(filename.c_str(), "w", stdout);
thread a(C), b(C), c(C), d(C), e(C), f(C), g(C), h(C);
a.join(); b.join(); c.join(); d.join();
e.join(); f.join(); g.join(); h.join();
return 0;
}
)";
int main() {
int aaa;
cout<<"你想要几个打工程序?\n";
cin>>aaa;
const int WORKER_COUNT = aaa; // 想要启动的打工进程数量,可根据需要调整
cout << "正在生成 " << WORKER_COUNT << " 个打工程序...\n";
for (int i = 0; i < WORKER_COUNT; ++i) {
int r = getRandom();
string srcFile = "worker_" + to_string(r) + ".cpp";
string exeFile = "worker_" + to_string(r) + ".exe";
// 1. 生成源代码文件(替换随机数)
string code = workerTemplate;
size_t pos = code.find("{RANDOM}");
if (pos != string::npos) {
code.replace(pos, 7, to_string(r));
}
ofstream out(srcFile);
out << code;
out.close();
// 2. 编译
string compileCmd = "g++ " + srcFile + " -o " + exeFile + " -std=c++11 -pthread";
int ret = system(compileCmd.c_str());
if (ret != 0) {
cerr << "编译失败: " << srcFile << endl;
continue;
}
// 3. 后台运行 (Windows 下使用 start /b 不弹出窗口,也可用 start /min 最小化)
string runCmd = "start /min " + exeFile;
system(runCmd.c_str());
cout << "已启动: " << exeFile << " (输出文件: output_" << r << ".txt)\n";
// 略微延时,避免同时编译/启动造成瞬间负载过高
this_thread::sleep_for(chrono::milliseconds(200));
}
cout << "所有打工程序已运行。\n";
cout << "每个进程会生成对应的 output_xxxx.txt 文件,其中不断写入 qdtl。\n";
return 0;
}
灌水2
#include <bits/stdc++.h>
using namespace std;
void C(){
int qdtl=0;
while(qdtl<=1e9){
cout<<"qdtl";
qdtl++;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
thread a(C),b(C),c(C),d(C),e(C),f(C),g(C),h(C);
a.join();
b.join();
c.join();
d.join();
e.join();
f.join();
g.join();
h.join();
return 0;
}
这里面的thread xxx的xxx可以增加
2 条评论
-
@ 2026-6-7 14:59:58#include<bits/stdc++.h> #include<thread> #include<windows.h> using namespace std; typedef long long ll; typedef string str; void sen_lin_bao_zha(){ for(int i=1;i<=1e8;i++){ cout<<"boom!"; } sen_lin_bao_zha(); } void C(){ sen_lin_bao_zha(); } void sout(str s,ll sleepms){ for(int i=0;i<s.size();i++){ std::cout<<s[i]; Sleep(sleepms); } } int main(){ str s="我███的",s1="直接抬走宝箱,放",s2="9e18^个TNT,",s3="然后森林就会......"; sout((s+s1+s2+s3),100); thread a(C),b(C),c(C),d(C),e(C),f(C),g(C),h(C); thread a2(C),b2(C),c2(C),d2(C),e2(C),f2(C),g2(C),h2(C); a.join(); b.join(); c.join(); d.join(); e.join(); f.join(); g.join(); h.join(); a2.join(); b2.join(); c2.join(); d2.join(); e2.join(); f2.join(); g2.join(); h2.join(); return 0; } /* #include <bits/stdc++.h> using namespace std; void C(){ int qdtl=0; while(qdtl<=1e9){ cout<<"qdtl"; qdtl++; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); thread a(C),b(C),c(C),d(C),e(C),f(C),g(C),h(C); a.join(); b.join(); c.join(); d.join(); e.join(); f.join(); g.join(); h.join(); return 0; } */
- 1
信息
- ID
- 1
- 时间
- ms
- 内存
- MiB
- 难度
- 5
- 标签
- (无)
- 递交数
- 29
- 已通过
- 15
- 上传者