At the opening ceremony of the Olympic Games,
as well as at its closing ceremony, the athletes of each country wore the same
costumes. Naturally, with so many athletes, coaches, and support staff, the
process of sewing the Olympic parade uniform for many countries was quite
responsible and important, and it was necessary to know the growth of each
member of the delegation to sew the uniform in an appropriate size. The
companies that will be engaged in tailoring this uniform are partly lucky, since
it is known that none of the members of the delegation are shorter than one and
a half meters and above two and a half meters.
You have a database with the growth of each
member of the delegation for the respective kind of sport. Your task is to
quickly answer the next type of query: how many members of the delegation have
heights in the range from a to b centimeters inclusively?
Input. The first line of each query contains one number n
(1 ≤ n ≤ 20000) – the number of members in the corresponding
delegation. The second line contains n integers – the heights of
athletes in centimeters. The data are not sorted because they were placed in a
database at the last moment, and therefore were not processed. The third line
contains a query: two numbers – the lower and upper limits of growth,
in which the manufacturer is interested.
Output. For each query, print the answer on a separate line.
Sample
input |
Sample output |
7 153 168 155
167 155 167 155 165 170 6 189 191 169
190 192 191 165 172 |
3 1 |
SOLUTION
array
Read the height
of members of delegations into a linear array. Then iterate through it, counting the number
of elements in the interval from a to b, inclusive.
Declare an array
to store the growth of delegation members.
#define MAX 20010
int m[MAX];
Read the input data. Process multiple test
cases.
while(scanf("%d",&n)
== 1)
{
for(i = 0; i < n; i++) scanf("%d",&m[i]);
scanf("%d %d",&a,&b);
In the variable cnt, count the number of m[i] in the interval
from a to b inclusive.
for(cnt = i = 0; i < n; i++)
if ((m[i] >= a) && (m[i]
<= b)) cnt++;
Print the answer.
printf("%d\n",cnt);
}
#include <stdio.h>
int i, n, a, b, cnt;
int *m;
int main(void)
{
while(scanf("%d",&n)
== 1)
{
m = new int[n];
for(i = 0; i < n; i++)
scanf("%d",&m[i]);
scanf("%d %d",&a,&b);
for(cnt = i = 0; i < n; i++)
if ((m[i] >= a) && (m[i]
<= b)) cnt++;
printf("%d\n",cnt);
delete [] m;
}
return 0;
}
#include <cstdio>
#include <vector>
using namespace std;
int i, n, a, b, cnt;
vector<int>
m;
int main(void)
{
while(scanf("%d",&n)
== 1)
{
m.resize(n);
for(i = 0; i < n; i++)
scanf("%d",&m[i]);
scanf("%d %d",&a,&b);
for(cnt = i = 0; i < n; i++)
if ((m[i] >= a) && (m[i]
<= b)) cnt++;
printf("%d\n",cnt);
}
return 0;
}
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner
con = new Scanner(System.in);
while(con.hasNextInt())
{
int cnt, i, n = con.nextInt();
int m[] = new int[n];
for(i = 0; i < n; i++)
m[i] = con.nextInt();
int a = con.nextInt();
int b = con.nextInt();
for(cnt = i = 0; i < n; i++)
if ((m[i] >= a) && (m[i] <= b)) cnt++;
System.out.println(cnt);
con.close();
}
}
}
Java realization – BufferedReader
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader read =
new BufferedReader(new
InputStreamReader(System.in));
//BufferedReader read =
// new
BufferedReader(new FileReader ("3607.in"));
while(read.ready())
{
int cnt = 0, i, n = Integer.parseInt(read.readLine());
int mas[] = new int[n];
String s[] = read.readLine().split(" ");
for(i = 0; i < n; i++)
mas[i] = Integer.parseInt(s[i]);
s = read.readLine().split(" ");
int a = Integer.parseInt(s[0]),
b = Integer.parseInt(s[1]);
for(i = 0; i < n; i++)
if ((mas[i] >= a) && (mas[i] <= b)) cnt++;
System.out.println(cnt);
}
}
}