Array —- one-dimensional array of arrays easier to deduplicate (c++)

Find the number of times each number appears in n numbers

Question description

Read n integers (n≤100) from the keyboard. These n numbers are all numbers between 1 and 10. Please output each occurring number from small to large, and count each

How many times does the number appear? For example: Suppose you read 5 numbers, respectively 1 2 3 3 5. The statistical results are as follows: 1 appears once, 2 appears once, 3 appears 2 times,

5 appears 1 time. The output format is as follows:

input

Line 1 enters an integer n .

Line 2 enters n integers, separated by spaces.

output

Output several lines, with 2 numbers in each line, separated by spaces. In order from small to large, output each number with

and the number of occurrences of each number.

#include <iostream>
using namespace std;
int main()
{
int cnt[200],n;
cin>>n;
for(int i = 0;i<201;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
for(int i = 1;i<201;i + + )
{
if(cnt[i]!=0)
{
cout<<i<<" "<<cnt[i]<<endl;
}
}
return 0;
}

Voice recognition

Question description

Teacher Wang of the school has developed a new voice recognition system. After entering each student’s student number and voice in advance, the system will know which student number the student belongs to as soon as he speaks.

The students are talking (different students have completely different voices). Teacher Wang installed the system in the classroom. The system collected n fragments of the students’ conversations in an English class, and

Convert to the student ID of the students, please program to help Teacher Wang calculate how many students have spoken in the English class, and output the student ID of each student according to the student ID in ascending order.

number and the number of times students with that student number have spoken. For example, suppose that after system analysis, the following student ID numbers of 88 students are obtained: 8 1 6 1 8 6 7 2, which indicates the number of students.

The order of speeches is: No. 8, No. 1, No. 6, No. 1, No. 8, No. 6, No. 7, No. 2. Then a total of 5 students have spoken. According to the student number from small to large, the number of speeches is output as follows:

Down:

On behalf of student No. 1, they spoke twice, student No. 2 spoke once, student No. 6 spoke twice, and student No. 7 spoke once.

Student No. 8 spoke twice.

input

The input has 2 lines, and the first line has an integer n (3≤n≤10000);

The second line has n numbers (these integers are all integers between 1 and 100, inclusive), with spaces between the numbers.

Separated, representing the student numbers of the students.

output

Line 11 outputs 11 integers, representing the total number of students who have posted;

Next, output a number of lines, each line contains 2 integers, separated by spaces, indicating that the students who spoke are output in ascending order of student numbers.

student number and the number of times each student has spoken.

#include <iostream>
using namespace std;
int main()
{
int cnt[200],n,cnt2;
cin>>n;
for(int i = 0;i<201;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
for(int i = 1;i<201;i + + )
{
if(cnt[i]!=0)
{
cnt2 + + ;
}
}
\t
\t
cout<<cnt2<<endl;
\t
for(int i = 1;i<201;i + + )
{
if(cnt[i]!=0)
{
cout<<i<<" "<<cnt[i]<<endl;
}
}
return 0;
}

Number of occurrences of number

Problem description

There are 50 numbers (0
~
19), what is the maximum number of times the same number appears among these 50 numbers?

Input

50 numbers.

Output

1 number (that is, the maximum number of times the same number occurs).

#include <iostream>
using namespace std;
int main()
{
int cnt[200],n,max,maxi,cnt2;
n = 50;
max = 0;
for(int i = 0;i<201;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
for(int i = 1;i<201;i + + )
{
if(cnt[i]>max)
{
max = i;
maxi = cnt[i];
}
}
\t
\t
\t
cout<<maxi;
return 0;
}

Missing numbers

Problem description

There are n numbers, and the value is 1 ~ n. Now it is found that 2 numbers are missing. Please program and calculate based on the remaining n – 2 numbers. Which is missing?

What about two numbers?

Input

Line 1 contains the integer N (N≤100000).

Line 2 has N
?
2 integers (both separated by spaces) represent the remaining N
?
2 digit value.

Output

In one line, output the two missing numbers in order from small to large. Separate the two numbers with a space.

#include <iostream>
using namespace std;
int cnt[100010];
int main()
{
int n,cnt2;
cin>>n;
for(int i = 0;i<n;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n-2;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t\t
for(int i = 1;i<=n;i + + )
{
if(cnt[i]==0)
{
cout<<i<<" ";
}
}
return 0;
}

teen programming

V

teen programming

V 13201376357

Elect class committee

Question description

The first thing that little T and his friends did after reporting to the innovative experimental class of CZ Middle School was to choose the class committee member, class teacher R

The teacher walked up to the podium and announced the election method. First, all 40 students in the class were asked to come to the podium one by one to introduce themselves, and then they followed the instructions.

The positions are elected one by one, first the squad leader, then the study committee member… The election method is that each person casts one vote, who

Whoever has the highest number of votes will be elected to this position. In the end, Xiao W, a Go master with a general demeanor, was elected as the squad leader. Xiao W, a scholar,

Z was elected as the study committee member, which was expected by everyone. Little S was born with a good voice. Not only could he sing well, but he could also sing

He imitated the sounds of various animals so vividly that he was elected as a cultural and sports committee member. Little T is responsible for planning this election.

He feels that manual vote counting is too slow and error-prone, so he would like to ask you to write a program to implement the machine vote counting function.

This program must be able to achieve the following functions: There are n students in the class, numbered from 1 to n, and there are m people in total.

(including head teachers, classroom teachers, etc.) participated in the voting. Each ballot has the number of a student who gets the most votes.

students were elected.

input

The first line of input data contains two positive integers n and m separated by spaces, where n≤200 and m≤2000.

The second line contains m positive integers not exceeding n separated by spaces, representing the numbers written on these m ballots.

output

Output the number of the student with the most votes. If there are more than two students with the most votes at the same time, the one with the smallest number will be output.

That classmate’s number.

#include <iostream>
using namespace std;
int main()
{
int cnt[500];
int n,m,cnt2,max;
cin>>m>>n;
max = 0;
for(int i = 0;i<500;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
for(int i = 1;i<=m;i + + )
{
if(cnt[i]>max)
{
max = i;
}
}
\t
\t\t
cout<<max;
return 0;
}

Summer Camp Flag Bearer

Problem description

The summer camp was held in an experimental primary school. The organizing committee decided to select a flag bearer among the students of the experimental primary school. The selection method

As follows: There are n students in the experimental primary school (1≤n≤1000). Each student has a student ID number, which is 1,2,…n.

At the same time, each student has a vote to elect a student to be the flag bearer. In the end, the one with the most votes is elected.

If the candidates with the most votes have the same number of votes, the candidate with the smaller student ID will be elected.

For example, the votes are 2 3 4 4 3 4 1 6. Student No. 4 got the most votes (3 votes) and was elected as the little chess player.

input

Line 1 reads two integers n and x1, n is the number of students, x1 is the student number on the first ballot, and the subsequent ballots x

i(i≥2) is deduced from the following recursion

The relation gives:

x i =(x i
?
1×37 + 33031) mod n + 1

Where mod is the remainder operation, for example, 13 mod 8=5, 21 mod 21=0. According to this formula, we can get from

x1 launched

x2,x3,…xn.

#include <iostream>
using namespace std;
int main()
{
int cnt[1000];
int n,cnt2,max,x[1000];
cin>>n>>x[0];
max = 0;
for(int i = 0;i<500;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 1;i<n;i + + )
{
int a;
a = (x[i-1]*37 + 33031)%n + 1;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
for(int i = 1;i<=n;i + + )
{
if(cnt[i]>max)
{
max = i;
}
}
\t
\t\t
cout<<max;
return 0;
}

Looking for chopsticks

Problem description

After a period of intense preparations, the computer team’s “RP Restaurant” finally opened. On this day, the manager LXC received a large order, which made everyone very happy!

The employees worked together to prepare the set meals as required and were about to deliver them when they suddenly encountered a difficult problem: chopsticks! CX The children found all the chopsticks in the restaurant

But unfortunately, these chopsticks are of different lengths, and we all know that chopsticks need to be of the same length to form a pair. What’s even more troublesome is that these chopsticks found by CX

The number is an odd number, but coincidentally, only one chopstick among these chopsticks is alone, and the rest are in pairs. If you are kind, you can help CX find this alone chopstick.

The length of the child?

Input

The first line reads a number N, which represents the number of chopsticks found by C X. (10≤N≤10000) The second line is N separated by spaces.

The number represents the length of the chopsticks. (The length of chopsticks is 1
~
an integer between 1000)

Output

One line, the length of a single chopstick

#include <iostream>
using namespace std;
int main()
{
int cnt[1000];
int n;
cin>>n;
for(int i = 0;i<500;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
for(int i = 0;i<n;i + + )
{
if(cnt[i]%2==1)
{
cout<<i;
break;
}
}
\t
\t\t
return 0;
}

teen programming

teen programming

Remove duplicate numbers

Problem description

Give you N numbers (N≤100), each number is in (0
~
1000),

There are many repeated numbers, please keep only one repeated number.

And sort the remaining numbers from small to large and output.

Input

Input has 2 lines;

The first line is a positive integer, indicating the number of numbers: N;

Line 2 has N space-separated integers.

Output

The first line contains a positive integer M, which represents the number of different numbers.

The next M lines, each line contains an integer, indicating that they are sorted from small to large.

of different numbers

#include <iostream>
using namespace std;
int main()
{
int cnt[1000];
int n,cnt2;
cin>>n;
cnt2 = 0;
for(int i = 0;i<1000;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
for(int i = 0;i<1000;i + + )
{
if(cnt[i]!=0)
{
cnt2 + + ;
}
}
cout<<cnt2<<endl;
\t
\t
\t
\t
for(int i = 0;i<1000;i + + )
{
if(cnt[i]!=0)
{
cout<<i<<endl;
}
}
\t
\t\t
return 0;
}

Find the number that appears most often among n numbers

Question description

Read n integers from the keyboard (n≤100), these n numbers are all 1
~
Among the numbers between 10, which number appears the most?

For example: Suppose you read 5 numbers, namely 1 2 3 3 5, and the number that appears the most is 3. (The data for this question ensures that it appears the most

There is only one number, and there is no situation where multiple numbers appear the most times)

input

Line 1 enters an integer n.

Line 2 enters n integers, separated by spaces.

output

Output the number with the most occurrences.

#include <iostream>
using namespace std;
int main()
{
int cnt[1000];
int n,max;
cin>>n;
max = 0;
for(int i = 0;i<1000;i + + )
{
cnt[i] = 0;
}
\t
\t
\t
for(int i = 0;i<n;i + + )
{
int a;
cin>>a;
cnt[a] + + ;
}
\t
\t
\t
\t
\t
\t
\t
\t
\t
for(int i = 0;i<1000;i + + )
{
if(cnt[i]>max)
{
max = i;
}
}
\t
cout<<max;
return 0;
}

teen programming

V 13201376357