众数问题,

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

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;
}
相关推荐
啵一杯24 分钟前
leetcode621. 任务调度器
服务器·前端·数据结构·算法·c#
2401_857297911 小时前
2025校招内推-招联金融
java·前端·算法·金融·求职招聘
小冉在学习2 小时前
leetcode刷题day32|动态规划Part01(509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯)
算法·leetcode·动态规划
雷达学弱狗2 小时前
孙论文——定标
算法
Mr_Xuhhh2 小时前
vector
c语言·开发语言·数据结构·算法·链表·visualstudio
枫の准大一2 小时前
空间复杂度&动态顺序表
数据结构·算法
saberyydsicloud3 小时前
C++(9.25)
开发语言·c++·算法
sewinger3 小时前
关于贪心算法
算法·贪心算法
暴怒香菜统治世界3 小时前
c语言--结构体
c语言·开发语言·数据结构·笔记·算法