没有重复的最少位数

least number of digits without duplicates

我是 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;
}

检查下面的代码。

#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