众数问题,

在本实验中,需要编写一个程序来处理一组输入数据,找出其中的众数,即出现次数最多的数字。程序会读取文件中的数据进行处理,并将结果与标准答案进行比对,判断程序输出是否正确。

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
using namespace std ;
int a[10000010] ;
int fre ;
int num ;
int n ;
void read(const string& file) {
    ifstream ifs;
    ifs.open(file.c_str(), ios::in); // 使用 c_str() 将 string 转换为 const char*
    if (ifs >> n) {
            for(int i= 1 ;i <=n ;i++) ifs >> a[i] ;
        ifs.close();
    }
	 else {
        // 如果无法读取文件或者无法提取值,则处理错误
        cerr << "Error reading from file: " << file << endl;
        return ; // 返回错误代码或者抛出异常
    }
}

void write(const string& file) {
    ofstream ofs;
    ofs.open(file.c_str(), ios::out);
    ofs << num << endl ;
    ofs << fre ;
    ofs.close();
}

void comparejugle(const string& a, const string& b) {
    // 使用 .c_str() 将 string 类型的文件路径转换为 const char* 类型
    ifstream fileA(a.c_str());
    ifstream fileB(b.c_str());

    // 检查文件是否正确打开
    if (!fileA.is_open() || !fileB.is_open()) {
        cerr << "Error opening files." << endl;
        return;
    }

    // 读取文件A和文件B的内容到字符串中
    string contentA((istreambuf_iterator<char>(fileA)), istreambuf_iterator<char>());
    string contentB((istreambuf_iterator<char>(fileB)), istreambuf_iterator<char>());

    // 比较两个字符串,如果不同则输出 "NO",相同则输出 "Yes"
    if (contentA != contentB) {
        cout << "NO" << endl;
    } else {
        cout << "Yes" << endl;
    }

    // 关闭文件
    fileA.close();
    fileB.close();
}

void mode(int a[] ,int l ,int r){
     if(l>=r) return ;
     int x = a[l+r>>1] ;
     int i =l+r >> 1 ;
     int j =l+r >> 1 ;
     while(a[i]==x&&i>=l) i-- ;
     while(a[j]==x&&j<=r) j++ ;
     int s = j-i+1-2 ;
    // cout << s<<" " << i << " " << j <<endl ;
     if(s>fre) {
        fre = s ;
        num = x ;
     }
     mode(a,l,i) ;
     mode(a,j,r) ;
 }
signed main() {
    string testFolder = "G:\\算法实验作业\\众数问题\\test\\mode"; // 测试文件夹路径
    string answerFolder = "G:\\算法实验作业\\众数问题\\myanswer\\"; // 答案文件夹路径
    string correctAnswerFolder = "G:\\算法实验作业\\众数问题\\answer\\mode"; // 正确答案文件夹路径

    for (int i = 1; i <= 10; i++) {
        stringstream ss;
        ss << i;
        string s = ss.str();
        string testFilePath = testFolder + s + ".in";
    //    cout << s << " " << testFilePath << endl;
        read(testFilePath);
        sort(a+1,a+n+1) ;
        num = 0 ;
        fre = 0 ;
	    mode(a,1,n) ;
        string answerFilePath = answerFolder + s + ".txt";
        write( answerFilePath);

        string correctAnswerFilePath = correctAnswerFolder + s + ".out";
        comparejugle(answerFilePath, correctAnswerFilePath);
    }
	return 0;
}
相关推荐
Wendy14417 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归
拾光拾趣录7 小时前
括号生成算法
前端·算法
渣呵8 小时前
求不重叠区间总和最大值
算法
拾光拾趣录8 小时前
链表合并:双指针与递归
前端·javascript·算法
好易学·数据结构8 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
香蕉可乐荷包蛋9 小时前
AI算法之图像识别与分类
人工智能·学习·算法
chuxinweihui9 小时前
stack,queue,priority_queue的模拟实现及常用接口
算法
tomato0910 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
墨染点香10 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展
甄卷11 小时前
李沐动手学深度学习Pytorch-v2笔记【08线性回归+基础优化算法】2
pytorch·深度学习·算法