• 检查随机序列重复[C++]


    /*
     * File:   Main.cpp
     * Author: 88250 <DL88250@gmail.com>, http://blog.csdn.net/DL88250
     *
     * Created on May 13, 2008, 6:25 PM
     */

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <vector>
    #include <time.h>

    using namespace std;

    /**
     * Check the same record in a file.
     *
     * Every record in data file is a random serial, like the followings:
     * // data file
     * 1902323484354370234844
     * 1928473090393719374
     * ....
     */

    vector<string> records; // store the data records
    vector<vector<string> > statistics; //statistics

    /**
     * Read the records from the data file which named "data.txt" into memory,
     * using a list store them.
     */
    void readRecords() {
        cout << "Get starting read records...." << endl;
        ifstream fin("data.txt");

        if (!fin) {
            cout << "Cannot open input file!" << endl;
            return;
        }

        string aLine;
        while (getline(fin, aLine)) {
            records.push_back(aLine);
        }
        fin.close();
        cout << "The amount of records: " << records.size() << endl;
    }

    /**
     * Display the data records in console.
     * @param amount display amount, start from {@link #records}'s beginning
     */
    void displayRecords(int amount) {
        if (amount < 0 || amount > records.size()) {
            cout << "The specified amount exceeds the Data records" <<
                    "size!" << endl;
        }
        cout << "Display: " << endl;
        for (int i = 0; i < amount; i++) {
            cout << records.at(i) << endl;
        }
        cout << endl;
    }

    /**
     * Display the statistic results in console.
     */
    void displayStats() {
        cout << "Statistics: " << endl;
        cout << "A amount of the same data records: " << statistics.size() << endl;
        for (int i = 0; i < statistics.size(); i++) {
            vector<string> aEqualities = statistics.at(i);
            cout << aEqualities.at(0) << " occurs " << aEqualities.size() << endl;
        }
    }
    /*
    bool greater(string s1, string s2){
        return s1.compare(s2);
    }
     */

    /**
     * Check the same data records.
     */
    void checkTheSame() {

        sort(records.begin(), records.end()); // sort them

        // displayRecords(10);
        for (int i = 0; i < records.size() - 1; i++) {
            string record1 = records.at(i);
            string record2 = records.at(i + 1);
            if (record1 == record2) {
                vector<string> equalities;
                equalities.push_back(record1);
                equalities.push_back(record2);
                statistics.push_back(equalities);
            }
        }
        displayStats();
    }

    int main(int argc, char** argv) {
        readRecords();
      
        displayRecords(10);
        checkTheSame();
        cout << "Elapsed time: " << clock() / CLOCKS_PER_SEC << endl;
        return (EXIT_SUCCESS);
    }


  • 相关阅读:
    20150216 IMX257实现GPIO-查询按键驱动程序
    20150216简单的Linux字符设备驱动程序
    Linux内核驱动编程
    解决安装完centos6.6之后/etc/sysconfig/目录下没有iptables 的问题
    centos6.6安装配置jboss7.1.1
    mysql5.6中 order by 多个字段排序问题
    centos6.6编译安装lnmp系列之PHP
    centos6.6编译安装lnmp系列之nginx
    centos6.6编译安装lnmp系列之mysql
    mysq 安装时候进行生成数据库系统时候执行语句 ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql 时候报错
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470278.html
Copyright © 2020-2023  润新知