众数问题,

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

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;
}
相关推荐
Swift社区15 小时前
LeetCode 394. 字符串解码(Decode String)
算法·leetcode·职场和发展
tt55555555555516 小时前
LeetCode进阶算法题解详解
算法·leetcode·职场和发展
让我们一起加油好吗16 小时前
【基础算法】DFS中的剪枝与优化
算法·深度优先·剪枝
Q741_14716 小时前
C++ 模拟题 力扣495. 提莫攻击 题解 每日一题
c++·算法·leetcode·模拟
Felven17 小时前
A. Be Positive
算法
小O的算法实验室17 小时前
2026年COR SCI2区,自适应K-means和强化学习RL算法+有效疫苗分配问题,深度解析+性能实测,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
青岛少儿编程-王老师18 小时前
CCF编程能力等级认证GESP—C++7级—20250927
数据结构·c++·算法
夏鹏今天学习了吗18 小时前
【LeetCode热题100(39/100)】对称二叉树
算法·leetcode·职场和发展
天选之女wow19 小时前
【代码随想录算法训练营——Day34】动态规划——416.分割等和子集
算法·leetcode·动态规划
Boop_wu20 小时前
[数据结构] 哈希表
算法·哈希算法·散列表