众数问题,

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

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;
}
相关推荐
biter down25 分钟前
C++ 解决海量数据 TopK 问题:小根堆高效解法
c++·算法
用户66006766853928 分钟前
斐波那契数列:从递归到缓存优化的极致拆解
前端·javascript·算法
初夏睡觉34 分钟前
P1055 [NOIP 2008 普及组] ISBN 号码
算法·p1055
程芯带你刷C语言简单算法题38 分钟前
Day28~实现strlen、strcpy、strncpy、strcat、strncat
c语言·c++·算法·c
踏浪无痕1 小时前
周末拆解:QLExpress 如何做到不编译就能执行?
后端·算法·架构
一个不知名程序员www1 小时前
算法学习入门--- 树(C++)
c++·算法
如竟没有火炬1 小时前
四数相加贰——哈希表
数据结构·python·算法·leetcode·散列表
背心2块钱包邮1 小时前
第9节——部分分式积分(Partial Fraction Decomposition)
人工智能·python·算法·机器学习·matplotlib
仰泳的熊猫1 小时前
1148 Werewolf - Simple Version
数据结构·c++·算法·pat考试
chao1898441 小时前
MATLAB中的多重网格算法与计算流体动力学
开发语言·算法·matlab