2017-11-02 2 views
1

ich ein C++ Programm geschrieben, das mir die Anzahl der Wiederholungen der Array-Elemente zeigt ... mein Quellcode ist:Zählen mehrere Array-Elemente

#include <iostream> 
#include <string> 
using namespace std; 
int main() { 
    int x[20] = {1,1,32,43,54,65,76,76,76,2,12,12,32,43,54,3,3,23,1,43}; 
    for (int i=0;i<20;i++) { 
    } 

    for (int i=0;i<20;i++) { 
     int count=1; 
     for (int j=i+1;j<19;j++) { 
      if (x[i]==x[j]) count++; 
     } 
     cout<<"The number "<<x[i]<<" is repeated "<<count<<" times"<<"\n"; 
    } 
} 

und aus diesem heraus genommen wird:

The number 1 is repeated 3 times 
The number 1 is repeated 2 times 
The number 32 is repeated 2 times 
The number 43 is repeated 2 times 
The number 54 is repeated 2 times 
The number 65 is repeated 1 times 
The number 76 is repeated 3 times 
The number 76 is repeated 2 times 
The number 76 is repeated 1 times 
The number 2 is repeated 1 times 
The number 12 is repeated 2 times 
The number 12 is repeated 1 times 
The number 32 is repeated 1 times 
The number 43 is repeated 1 times 
The number 54 is repeated 1 times 
The number 3 is repeated 2 times 
The number 3 is repeated 1 times 
The number 23 is repeated 1 times 
The number 1 is repeated 1 times 
The number 43 is repeated 1 times 

Das Problem ist Ausgabe zeigt Array-Element jedes Mal, aber ich möchte, dass mein Programm zeigt nur die wiederholte Array nur einmal. und ich will kein neues Array definieren. Jeder hat eine Ahnung was los ist ??

Hinweis: dass ohne Definition jede neue Anordnung und ohne Programmausgabesortierung sollte wie folgt sein:

The number 1 is repeated 3 times 
    The number 32 is repeated 2 times 
    The number 43 is repeated 3 times 
    The number 54 is repeated 2 times 
    The number 65 is repeated 1 times 
    The number 76 is repeated 3 times 
    The number 2 is repeated 1 times 
    The number 12 is repeated 2 times 
    The number 3 is repeated 2 times 
    The number 23 is repeated 1 times 
+0

warum die Ausgabe nicht enthalten sollte 'Die Zahl 3 wird 2 times' wiederholt? – user463035818

+0

* "ohne Definition irgendein neues Array und ohne Sortierung" * - Warum? Platzieren Sie keine willkürlichen Beschränkungen für die Lösungen Ihres Problems, dann hat Ihr Problem nichts mit der realen Programmierung zu tun. –

+0

Möchten Sie die Anzahl der Elemente drucken, die nur für mehrere Male vorhanden sind? Und nicht den, der einmal da ist, ausdrucken? –

Antwort

4

Sie eine Karte verwenden kann, um Elemente zu zählen, die Ihre Anforderung erfüllen, dass Sie schaffen keine neue Array.

std::map<int, int> counts; 
for(auto&& elem : x) 
{ 
    counts[elem]++; 
} 

for(auto&& item : counts) 
{ 
    std::cout << "The number " << item.first << " is repeated " << item.second << " times; 
} 
0

Ohne zusätzliche Strukturen, können Sie einfach tun:

#include <algorithm> 
#include <iostream> 
#include <string> 

int main() { 
    const int x[20] = {1,1,32,43,54,65,76,76,76,2,12,12,32,43,54,3,3,23,1,43}; 

    for (int i=0;i<20;i++) { 
     if (std::find(x, x + i, x[i]) != x + i) { 
      continue; 
     } 
     const auto count = std::count(x + i, x + 20, x[i]); 
     std::cout << "The number " << x[i] << " is repeated " << count << " times\n"; 
    } 
} 

Demo

+0

Ihr Code zeigt nicht Array 65 und Arrays mit 1 Vorkommen –

+0

@ SinaSameti: Meine erste Version wurde nur gedruckt, wenn "count> 1", es wurde behoben und Demo hinzugefügt. – Jarod42

+0

danke für deine Antwort es geht gut in code runner, aber es hat Fehler in Borland C++ Compiler ... Wie kann ich es auf Borland C++ ausführen? –

0

Dies ist ein Betrieb zu reduzieren. C++ hat in Algorithmen eine linksseitige Operation namens std::accumulate. Füttern Sie es mit einem std::map, um einen Count-Record zu erstellen.

auto count_map = std::accumulate(std::begin(x), std::end(x), 
           std::map<int, int>{}, 
           [] (auto& m, int val) -> decltype(m) { 
    ++m[val]; 
    return m; 
}); 

// Output result 
for (auto&& [val, count] : count_map) { 
    std::cout << "Value: " << val << " - Count: " << count << std::endl; 
} 
# Output: 
Value: 1 - Count: 3 
Value: 2 - Count: 1 
Value: 3 - Count: 2 
Value: 12 - Count: 2 
Value: 23 - Count: 1 
Value: 32 - Count: 2 
Value: 43 - Count: 3 
Value: 54 - Count: 2 
Value: 65 - Count: 1 
Value: 76 - Count: 3 
Verwandte Themen