簡體   English   中英

如何在C ++中對數組進行排序和檢查?

[英]How can I sort and check an array for duplicates in C++?

我要解決的問題是在用戶向數組中輸入值時檢查數組。 具體說明是:使用單獨的方法進行檢查; 該方法必須首先對數組進行排序; 重復檢查必須實時執行,這意味着用戶不會先輸入所有值,然后數組返回並檢查它們。 我環顧四周,雖然我想出了如何排序,但我對輸入每個值時如何檢查重復項感到完全困惑。 誰能在正確的思想方向上推動我?

這是我想出的,在分配的范圍內工作:

void getLottoPicks(int numbers[])
{
    cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;

    for (int i = 0; i < SIZE; i++)
    {
        cout << "Selection #" << i + 1 << endl;
        cin >> numbers[i];
        while (numbers[i] < 1 || numbers[i] > 40)
        {
            cout << "Please choose a number between 1 and 40: " << endl;
            cin >> numbers[i];
        }
        for (int j = 0; j < i; j++)
        {
            while (numbers[i] == numbers[j])
            {
                cout << "You have already picked that number. Enter a different one: " << endl;
                cin >> numbers[i];
            }
        }
    }
}

它可以編譯並運行,沒有任何錯誤(無論如何我都還沒有發現)。 如果有人有任何反饋,我們將不勝感激。 我知道這種算法效率很低,但是我們的數組只有7個值,這正是所要的。 最初,我認為它必須進行排序,但這僅在我們選擇在其他函數中進行檢查時才需要。

您可以通過多種選擇來執行此操作。

  1. 首先,如果僅使用array,請使用std :: sort和std :: unique。

  2. 如果數組中的最大數目不是很大,則可以維護bool數組,可以將其設置為0。

      #include <cstring> bool a[100000]; memset(a,0,sizeof a) main(){ int x,i,arr[1000]; while(cin>>x){ if( a[x] == 0) arr[i++]=x,a[x]=1; } 

在數組中輸入值之前,請檢查該數字的a的索引值是否為0。
如果為0,則在數組中輸入值,標記布爾數組1的索引。稍后,您可以使用

      #include<algorithm>
      sort(arr,arr+(size of arr));

3.您可以維護輸入值的集合,而不是數組,它將是二叉樹,並且將丟棄任何重復值,並且插入后的值將按排序順序排列。

      #include <set>
       set <int> arr;
      main(){
        int x;
        while(cin>>x)
        a.insert(x);
      }

您可以使用類似的迭代器從集合中獲取值

      set<int> :: iterator it;
  1. 獲取第一個輸入。 將其放在數組中。
  2. 獲取另一個輸入。 檢查是否與數組中的其他任何內容都不匹配。 如果是這樣,請將其丟棄。 如果沒有,請按順序將其放入數組中(這樣數組就保持排序)。
  3. 重復步驟2。

或者,如果分配允許,則使用set


如果不允許使用集合,請使用vector 向量可讓您輕松地在特定索引處插入元素,以幫助保持排序。 當您向其中添加元素時,它將自動調整大小。

由於您的數字是1到40之間的整數,因此可以使用平凡而又快速的解決方案(O(1))。 您可以重用此技巧以在以后進行排序。 以下是主要內容:

  • 創建一個布爾數組,其中的40個元素初始化為false。 我稱它為vals
  • 每次讀取值時,請檢查vals[input_value]的值。 如果為true ,則為重復項,否則將其設置為true
  • 現在,為了對值進行排序,您只需在1到40之間進行一個for循環,如果vals[i]為true,則將其放入另一個數組或打印它。

這是我的解決方案:

void getLottoPicks(int numbers[])
{
    cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;
    int value;
    bool vals[41];
    for (int i = 0; i <= 40; i++) vals[i] = false;

    for (int i = 0; i < SIZE; i++)
    {
        cout << "Selection #" << i + 1 << endl;
        bool input_ok = false;
        while (!input_ok)
        {
            cin >> value;
            if (value < 1 || value > 40) {
                cout << "Please choose a number between 1 and 40: " << endl;
            } else if (vals[value]) {
                cout << "You have already picked that number. Enter a different one: " << endl;
            } else {
                input_ok = true;
            }
        }
        vals[value] = true;
    }

    int j = 0;
    for (int i = 1; i <= 40; i++) {
        if (vals[i]) {
            numbers[j++] = i;
        }
    }
}

該算法的靈感來自計數排序算法。 計數排序算法是一種非常簡單有效的算法,可以在元素為小整數時使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM