不废话,直接上代码(拷贝就行)
主要是改solve
//#include "read.h" #include <Windows.h> #include <fstream> #include <cmath> #include <iostream> #include <string> using namespace std; fstream f1; string ans[10001]; int* ap = new int[100001]; int ln; void open_file_read(string fname) { f1.open(fname.c_str(), ios::in);//以读的方式打开 if (!f1) {//文件不存在 cout << "读取文件" + fname + "失败!" << endl; } } void read_test(fstream& fin) { int n; while (fin >> n) { ap[0] = n; for (int i = 0; i < n; i++) { fin >> ap[i+1]; } } } string plusall(int n, int _Radix) { double ret = 0; char tmp[16]; sprintf_s(tmp, "%d", n); int cnt = strlen(tmp); int j = cnt - 1; for (int i = 0; i < cnt && j >= 0; i++, j--) { ret += (tmp[j] - '0') * pow(_Radix, i); } return to_string((int)ret); } void solve() { //... } void write_ans(fstream& fout,int len) { for (int i = 0; i < len; i++) { fout << ans[i] << endl; } } void open_file_write(string fname) { f1.open(fname.c_str(), ios::in);//以读的方式打开 if (!f1) {//文件不存在 f1.close(); f1.open(fname.c_str(), ios::out);//则创建它 if (!f1)cout << "创建失败" << endl; } else {//存在 f1.close(); f1.open(fname.c_str(), ios::out | ios::trunc);//将文件清空再打开 } cout << "打开文件" + fname << endl; } int main() { open_file_read("file.in"); read_test(f1); //ln = ap[0]; cout << "计算结果并输出中..." << endl; for (int i = 1; i <= 1; i++) { clock_t time_stt = clock(); //solve cout << "完成数量: " << i + 1 << " 完成率: " << 100.0 / ap[0] * i << endl; cout << "完成时间: " << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl; } open_file_write("file.out"); write_ans(f1, 1); cout << "file.out输出完毕!"; return 0; }
生成
#include <fstream> #include <cstdio> #include <iostream> #include <ctime> #include <string> using namespace std; fstream f1; void write_test(fstream& fout,int fw) { int t = /*rand() % 1 + 2*/1; while (t--) { int n = rand() % 1000 + 1; fout << n << " "; for (int i = /*0*/0; i /* <=*/ <= 3; i++) fout << rand() % (int)(fw) << " "; fout << endl; } } void open_file_write(string fname) { f1.open(fname.c_str(), ios::in);//以读的方式打开 if (!f1) {//文件不存在 f1.close(); f1.open(fname.c_str(), ios::out);//则创建它 if (!f1)cout << "创建失败" << endl; } else {//存在 f1.close(); f1.open(fname.c_str(), ios::out | ios::trunc);//将文件清空再打开 } cout << "打开文件" + fname << endl; } int main() { char file_name[50]; int fw; cout << "请输入范围: "; cin >> fw; for (int i = 1; i <= 5; i++) { sprintf_s(file_name, "test%d.txt", i); open_file_write(file_name); write_test(f1,fw); f1.close(); } return 0; }
亲测有效,如有问题请留言!
中土大地,万物峥嵘。
C++的solve函数,
云淡风轻的男孩纸。
中土大地,万物峥嵘。