【问题标题】:C++, sort through array of numbers to find uniquenessC ++,对数字数组进行排序以查找唯一性
【发布时间】:2016-09-26 18:43:02
【问题描述】:

假设我有一个包含 4 个不同数字的数组。

整数[4] = {50234, 50356, 50454, 50934};

如何在 C++ 中创建一个嵌套的 for 循环来从后到前对这些数字进行排序,以确定唯一性所需的位数?

从示例中您可以看出,您需要从后面开始 3 位数字,以确保没有数字包含相似的数字尾部。 50234、50934 = 3 位数字,它们是唯一的 = 502 和 509。

for 循环一个接一个、一个接一个地遍历每个数字,然后对相同的数字进行排序以达到 3 的输出,会是什么样子?

会是这样的:

4

6 - 丢弃这个数字,它不相同

4

4

然后:

3

5 - 丢弃这个数字

3

然后:

2

9 万岁!没有类似的数字了,打印出来 3 就是答案。

我一头雾水,想不通。

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 你真的不需要检查整数吗? 502123456789503123456789 后面有很多相同的数字,但它们是不同的数字。
  • 不合逻辑的逻辑
  • 我是 C++ 新手,只是一个初学者。我不需要检查整个数字。我只希望它从后到前遍历数字的每个位置。它可以丢弃没有数字相似的数字,并在到达数字中没有数字相似的位置时停止。那有意义吗? ://
  • 我尝试了一个嵌套的 for 循环,其中包含 3 个循环。第一个遍历数字的长度(因此为 5),下一个遍历数字的数量(即 4),最后一个循环将每个数字与下一个数字进行比较以查看数字是否相同。不适合我..
  • 查看该代码会有所帮助。

标签: c++ arrays sorting unique


【解决方案1】:

假设你开始

#include <unordered_set>
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    const std::vector<int> numbers{50234, 50356, 50454, 50934};

您可以将其转换为字符串向量:

    std::vector<std::string> string_numbers;
    std::for_each(std::begin(numbers), std::end(numbers), [&](int n){ string_numbers.push_back(std::to_string(n)); });

现在我们将检查所需的位数,从 1 开始:

    size_t digits = 1;
    while(true) {

在每次迭代中,我们将创建一个unordered_set

        std::unordered_set<std::string> partials;

对于每个数字,我们将尝试将其 digits 数字放入集合中:

        for(const auto &s: string_numbers) {
            if(s.size() <= digits) {
                std::cout << "not unique" << std::endl;
                return 0;
            } 
            partials.insert(s.substr(0, digits));
        }

如果集合的大小是向量的大小,我们就完成了:

        if(partials.size() == numbers.size()) {
                std::cout << digits << " required" << std::endl;
                return 0;
            }

否则,我们需要增加位数:

        ++digits;
    }
}

完整代码:

#include <unordered_set>
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    const std::vector<int> numbers{50234, 50356, 50454, 50934};

    std::vector<std::string> string_numbers;
    std::for_each(std::begin(numbers), std::end(numbers), [&](int n){ string_numbers.push_back(std::to_string(n)); });

    size_t digits = 1;
    while(true) {
        std::unordered_set<std::string> partials;
        for(const auto &s: string_numbers) {
            if(s.size() <= digits) {
                std::cout << "not unique" << std::endl;
                return 0;
            } 
            partials.insert(s.substr(0, digits));
        }
        if(partials.size() == numbers.size()) {
                std::cout << digits << " required" << std::endl;
                return 0;
            }
        ++digits;
    }
}

【讨论】:

  • 嗨,阿米,只是想在这里对您的解决方案表示感谢。我对 c++ 还是很陌生,这超出了我能理解的水平,但它让我得到了我需要的答案。谢谢。
【解决方案2】:

如果您想对数字进行排序,请使用其中一种排序算法,比如说冒泡排序。然后检查唯一性并将唯一值存储在一个新数组中,然后打印它们:

我们编写代码是为了理解和练习,但在实际程序中,我们使用的库过于强大和快速:

#include <iostream>
using std::cout;
using std::endl;



int main()
{

    int numbers[4] = {50234, 50356, 50454, 50934};
//  int numbers[4] = {50234, 50356, 50454, 50356};

    for(int i(0); i < 4; i++)
    {
        for(int j(i + 1); j < 4; j++)
        {
            if(numbers[i] > numbers[j])
            {
                numbers[i] ^= numbers[j];
                numbers[j] ^= numbers[i];
                numbers[i] ^= numbers[j];
            }
        }
    }

    for(int i = 0; i < 4; i++)
        cout << numbers[i] << ", ";

    int  nUniq = 0;
    bool isUniq = true;

    for(int i = 0; i < 4; i++)
    {
        isUniq = true;

        for(int j(i + 1); j < 4; j++)
        {
            if(numbers[i] == numbers[j])
            {
                isUniq = false;
                break;
            }
        }
        if(isUniq)
            nUniq++;
    }

    cout << nUniq << endl;

    int* ptrUniq = new int[nUniq];
    int k = 0;

    for(int i = 0; i < 4; i++)
    {
        isUniq = true;

        for(int j(i + 1); j < 4; j++)
        {
            if(numbers[i] == numbers[j])
            {
                isUniq = false;
                break;
            }
        }
        if(isUniq)
        {
            ptrUniq[k] = numbers[i];
            k++;
        }
    }

    cout << "\nhere are uniq values:\n\n";
    for(int i = 0; i < nUniq; i++)
        cout << ptrUniq[i] << ", ";

    delete[] ptrUniq;
    ptrUniq = NULL;


    cout << endl << endl;
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2012-08-31
    相关资源
    最近更新 更多