不重复的最少位数

least number of digits without duplicates

本文关键字:      更新时间:2023-10-16

我是一个c++新手。我有一串数字,我把它们放入一个向量中。所有数字都是9位整数,并且是唯一的。我想知道可以用来唯一标识集合中每个数字的最小位数(从右开始)是多少。目前只有6个数字,但这个列表可能会增长到数千个。我已经张贴了我的代码到目前为止(不工作)

编辑输出如下…

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
相关文章:
  • 没有找到相关文章