- 3
Write a program that reads in a set of positive integers and outputs how many times a particular number appears in the list.
Algorithm, explanation of the code
2 Antworten
+ 1
#include <bits/stdc++.h>
using namespace std;
int main(){
map<int,int> appears ;
cout << "Enter the number of numbers : ";
int x,n ; cin >> n;
while(n--){
cin >> x ;
appears[x]++;
}
cout << " What is the number ? " ;
int find ; cin >> find ;
cout << appears[find];
return 0;
}
0
Algorithm :
using map in C++ ( stl )
when we enter the set
cin >> x ;
map[x]++;
so when we need to know how many time a particular number appears in the list
easly :
we search for n
cout << map[n];
I don't know a lot of java as a newbie to it