最少的数字没有重复

时间:2015-09-24 07:24:04

标签: c++ vector duplicates unique digits

我是C ++菜鸟。我有一个数字列表,我放入Vector。所有数字都是9位整数,并且是唯一的。我想知道什么是最少的数字(从右边开始),可用于单独识别集合中的每个数字。现在只有6个数字,但这个数字可能会增长到数千个。到目前为止,我已经发布了我的代码(不工作。)

EDIT输出如下......

digit is 1
digit is 1
digit is 1

RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

这主要是一项学习练习。请慷慨明确地提出您的意见和解决方案。

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main() {

    //declare stream variable and load vector with values 
    ifstream myfile("mydata.txt");
    vector<int> myVector;
    int num;
    while (myfile >> num) {
        myVector.push_back(num);
    }



    //sort and squack if there is a duplicate.  
    std::sort(myVector.begin(), myVector.end());
    for (int i = 0; i < (myVector.size() - 1); i++) {
        if (myVector.at(i) == myVector.at(i + 1)) {
            printf("There are duplicate student numbers in the file");
            exit(EXIT_FAILURE);
        }

    }
    //if it get here, then there are no duplicates of student numbers








    vector<int> newv;
    int k = 1;
    bool numberFound = false;
    bool myflag = false;

    while (numberFound == false) {
        //loop  through original numbers list and add a digit to newv.  
        for (int j = 0; j < myVector.size(); ++j) {
            newv.push_back(myVector.at(j) % (10^k));
        }
        sort(newv.begin(), newv.end());
        for (int i = 0; i < (newv.size() - 1); i++) {
            if (newv.at(i) == newv.at(i + 1)) {
                //there is a duplicate for this digit. Set flag.  
                myflag = true;
            }
            if (myflag == false) {
                numberFound = true;
                cout << "digit is " << k << endl;
            } else {
                k++;
            }

        }

    }






    //    for (int i = 0; i < myVector.size(); i++) {
    //        cout << "||" << myVector.at(i) << "||" << endl;
    //    }
    //
    //    for (int i = 0; i < newv.size(); i++) {
    //        cout << "---" << newv.at(i) << "---" << endl;
    //    }





    return 0;
}

1 个答案:

答案 0 :(得分:2)

检查以下代码。

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm> 
#include <math.h>
using namespace std;

int main() {

//declare stream variable and load vector with values
ifstream myfile("mydata.txt");
vector<int> myVector;
int num;
while (myfile >> num) {
    myVector.push_back(num);
}
//sort and squack if there is a duplicate.
std::sort(myVector.begin(), myVector.end());
for (int i = 0; i < (myVector.size() - 1); i++) {
    if (myVector.at(i) == myVector.at(i + 1)) {
        printf("There are duplicate student numbers in the file");
        exit(EXIT_FAILURE);
    }
}
//if it get here, then there are no duplicates of student numbers
vector<int> newv;
int k = 1;
bool numberFound = false;
bool myflag = false;
int p = 1;
while (numberFound == false) {
    //loop  through original numbers list and add a digit to newv.
    newv.clear();
    p = p * 10;
    for (int j = 0; j < myVector.size(); ++j) {
        newv.push_back(myVector[j] % p);
    }
    sort(newv.begin(), newv.end());
     myflag = false;
    for (int i = 0; i < (newv.size() - 1); i++) {

        if ( newv[i] == newv[i+1]) {
            //there is a duplicate for this digit. Set flag.
            myflag = true;
            break;
        }
    }
    if (myflag == true){
        k ++;
    }else{
        numberFound = true;
            cout << "digit is " << k << endl;
            break;
    }
}
return 0;
}

示例输入:

123451789
123456687
125456789
123456780

输出:

digit is 4