当前位置: 首页 > article >正文

众数问题,

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

#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;
}


http://www.kler.cn/news/323617.html

相关文章:

  • JAVA并发编程系列(11)线程池底层原理架构剖析
  • 【春秋云境】CVE-2024-23897-Jenkins 2.441之前版本存在任意文件读取漏洞
  • RuoYi若依框架学习:多环境配置
  • 电子秤PCBA方案应用解决方案设计
  • Java面试:ArrayList 和 LinkedList 的区别是什么?谈谈你对ArrayList和LinkedList的理解
  • 500. 键盘行 哈希表 力扣 Python 难度指数:3
  • JavaScript 定时器与动画基础
  • DAY80服务攻防-中间件安全HW2023-WPS 分析WeblogicJettyJenkinsCVE
  • 旧衣回收小程序搭建,开发功能优势
  • CORDIC算法笔记整理
  • 全局中断总开关位与各个中断源对应的寄存器使能位开启顺序
  • Vscode把全部‘def‘都收起来的快捷键
  • Django 对数据库的增删改查
  • [译] K8s和云原生
  • `torch.utils.data`模块
  • PostgreSQL 向量扩展插件pgvector安装和使用
  • 高等数学 第11讲 多元函数偏导数的计算与应用_复合函数求偏导_隐函数求偏导_条件极值
  • 什么是原生IP?
  • QT+ESP8266+STM32项目构建三部曲二--阿里云云端处理之云产品流转
  • 学习threejs,绘制二维线
  • 洛谷P1197.星球大战
  • 一道简单的css动态宽度问题?
  • List 循环遍历删除元素
  • 精通推荐算法31:行为序列建模之ETA — 基于SimHash实现检索索引在线化
  • rtsp 协议推流接收(tcp udp)
  • 【深度学习】(9)--调整学习率
  • 后端返回内容有换行标识,前端如何识别换行
  • Linux:LCD驱动开发
  • MySQL:进阶巩固-存储过程
  • 经典Python应用库一览