Week 5 of 2005 -- program design and algorithm test

28: the number of the same number as the specified number

Total time limit: 1000ms memory limit: 65536kB

describe
Outputs the number of the same number as the specified number in an integer sequence.

input
The input contains three lines:
The first line N represents the length of the integer sequence (N < = 100);
The second line is N integers separated by a space;
The third line contains an integer, which is the specified integer m.

output
The output is the number of the same number as m in N numbers.

sample input

3
2 3 2
2

sample output

2

code

```#include<stdio.h>
int main()
{
int N,arr[100],m;
int i,sum=0;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d",&arr[i]);
}
scanf("%d",&m);
for(i=0;i<N;i++){
if(arr[i]==m)
sum++;
}
printf("%d",sum);
return 0;
}
```

029: Tao Tao picking apples

Total time limit: 1000ms memory limit: 65536kB

describe
There is an apple tree in Taotao's yard, which will bear 10 apples every autumn. When the apple is ripe, Tao Tao will run to pick the apple. Tao Tao has a 30 cm high bench. When she can't pick apples directly by hand, she will step on the bench and try again.

Now we know the height of 10 apples to the ground and the maximum height that Tao Tao can reach when she straightens her hand. Please help Tao Tao calculate the number of apples she can pick. Suppose she touches an apple, it will fall.

input
Includes two rows of data. The first line contains 10 integers (in centimeters) between 100 and 200 (including 100 and 200), representing the height of 10 apples to the ground, and two adjacent integers are separated by a space. The second line only includes an integer (in centimeters) between 100 and 120 (including 100 and 120), indicating the maximum height that can be reached when the pottery hand is extended.

output
It includes a line, which contains only an integer, indicating the number of apples Tao Tao can pick.

sample input

100 200 150 140 129 134 167 198 200 111
110

sample output

5

code

```#include<stdio.h>
int main(){
int arr[10];
int i,height,sum=0;
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
scanf("%d",&height);
for(i=0;i<10;i++){
if((height+30)>=arr[i])
sum++;
}
printf("%d",sum);
return 0;
}
```

030: age and disease

Total time limit: 1000ms memory limit: 65536kB

describe
If a hospital wants to make statistics on whether the acquisition of a disease is related to age, it needs to sort out the previous diagnostic records, and the proportion of the number of patients in the total number of patients according to the four age groups of 0-18, 19-35, 36-60 and above 61 (including 61).

input
There are 2 lines in total. The first line is the number of past patients n (0 < n < = 100), and the second line is the age of each patient when he was ill.

output
According to the four age groups of 0-18, 19-35, 36-60 and above 61 (including 61), the proportion of the number of patients in this section in the total number of patients is output in the form of percentage, accurate to two decimal places. Each age group occupies one line, a total of four lines.

sample input

10
1 11 21 31 41 51 61 71 81 91

sample output

20.00%
20.00%
20.00%
40.00%

code

```#include<stdio.h>
int main(){
int sum,arr[100],i;
scanf("%d",&sum);
int a=0,b=0,c=0,d=0;
for(i=0;i<sum;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<sum;i++){
if(arr[i]>0&&arr[i]<=18){
a++;
}
else if(arr[i]>=19&&arr[i]<=35){
b++;
}
else if(arr[i]>=36&&arr[i]<=60){
c++;
}
else
d++;
}
printf("%0.2f%%\n%.2f%%\n%.2f%%\n%.2f%%\n",100.0*a/sum,100.0*b/sum,100.0*c/sum,100.0*d/sum);
return 0;
}
```

031: the tree outside the school gate

Total time limit: 1000ms memory limit: 65536kB

describe
There is a row of trees on the road with a length of L outside the gate of a school, and the interval between each two adjacent trees is 1 meter. We can regard the road as a number axis. One end of the road is at the position of number axis 0 and the other end is at the position of L; Every integer point on the number axis, i.e. 0, 1, 2,..., l, has a tree.

Because there are some areas on the road to build subways. These areas are represented by their starting and ending points on the number axis. It is known that the coordinates of the starting point and ending point of any region are integers, and there may be overlapping parts between regions. Now remove the trees in these areas (including the two trees at the end of the area). Your task is to calculate how many trees there are on the road after all these trees are removed.

input
The first line has two integers L (1 < = l < = 10000) and m (1 < = m < = 100). L represents the length of the road, M represents the number of areas, and l and m are separated by a space. The next M lines contain two different integers, separated by a space, representing the coordinates of the starting and ending points of an area.

For 20% of the data, there is no overlap between regions;
For other data, there is overlap between regions.

output
Includes a row that contains only an integer indicating the number of trees remaining on the road.

sample input

500 3
150 300
100 200
470 471

sample output

298

code

```#include<stdio.h>
int main(){
int L,M;
int a[10001],b[201],i,j;
scanf("%d%d",&L,&M);
for(i=0;i<2*M;i+=2){
scanf("%d%d",&b[i],&b[i+1]);
}
for(j=0;j<=L;j++){
a[j]=j;//0, 1, 2,..., L, all have a tree.
}
int r,s;
for(i=0;i<2*M;i+=2){
r=b[i];//Regional starting point
s=b[i+1];//Area end
for(j=r;j<=s;j++){
a[j]=-1;
}
}
int sum=0;
for(j=0;j<=L;j++){
if(a[j]!=-1){
sum++;
}
}
printf("%d",sum);
return 0;
}
```

Total time limit: 1000ms memory limit: 65536kB

describe
Given a 5 * 5 matrix, each row has only one maximum value and each column has only one minimum value, find the saddle point of this matrix.
Saddle point refers to an element in the matrix, which is the maximum value of the row and the minimum value of the column.
For example, in the following example (the element in row 4 and column 1 is the saddle point, with a value of 8).
11 3 5 6 9
12 4 7 8 10
10 5 6 9 11
8 6 4 7 2
15 10 11 20 25

input
Enter a matrix with 5 rows and 5 columns

output

sample input

11 3 5 6 9
12 4 7 8 10
10 5 6 9 11
8 6 4 7 2
15 10 11 20 25

sample output

4 1 8

code

```#include<stdio.h>
int main(){
int i,j,a[5][5];
int max,k,flag,point;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<5;i++){
max=a[i][0];//Initialize the maximum value of one line per cycle
for(j=0;j<5;j++){
if(a[i][j]>=max){
max=a[i][j];
point=j;//Record column
}
}
flag=1;
for(k=0;k<5;k++){
if(a[k][point]<max)
flag=0;
}
if(flag!=0){
printf("%d %d %d",i+1,point+1,max);
return 0;
}

}
return 0;
}
```

033: image blur processing

Total time limit: 1000ms memory limit: 65536kB

describe
Given the gray value of each pixel of the image in n rows and m columns, it is required to blur it with the following methods:

1. The gray value of the outermost pixels around remains unchanged;
2. The new gray value of each pixel in the middle is the average of the original gray value of the pixel and its upper, lower, left and right adjacent pixels (rounded to the nearest integer).

input
The first row contains two integers n and m, indicating the number of rows and columns of pixels in the image. 1 <= n <= 100，1 <= m <= 100.
Next, there are n lines, m integers in each line, representing the gray level of each pixel of the image. Two adjacent integers are separated by a single space, and each element is between 0 and 255.

output
n lines, m integers per line, are the blurred image. Two adjacent integers are separated by a single space.

sample input

4 5
100 0 100 0 50
50 100 200 0 0
50 50 100 100 200
100 100 50 50 100

sample output

100 0 100 0 50
50 80 100 60 0
50 80 100 90 200
100 100 50 50 100

code

```#include<stdio.h>
int main(){
int n,m,i,j,arr[100][100],b[100][100];
float avg;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&arr[i][j]);
b[i][j]=arr[i][j];
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(i!=0&&i!=n-1&&j!=0&&j!=m-1){
avg=(arr[i][j]+arr[i-1][j]+arr[i+1][j]+arr[i][j-1]+arr[i][j+1])/5.0;
b[i][j]=(int)(avg+0.5)>(int)avg?(int)avg+1:(int)avg;//This cannot be modified directly in arr, because it will change the calculation of subsequent values
}
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
return 0;
}
```

034: matrix transpose

Total time limit: 1000ms memory limit: 65536kB

describe
Input a matrix A with n rows and m columns and output its transpose AT.

input
The first row contains two integers n and m, representing the number of rows and columns of matrix A. 1 <= n <= 100，1 <= m <= 100.
Next, n rows, m integers per row, represent the elements of matrix A. Two adjacent integers are separated by a single space, and each element is between 1 and 1000.

output
m rows, n integers per row, which is the transpose of matrix A. Two adjacent integers are separated by a single space.

sample input

3 3
1 2 3
4 5 6
7 8 9

sample output
1 4 7
2 5 8
3 6 9

code

```#include<stdio.h>
int main(){
int n,m,i,j,arr[100][100],b[100][100];
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
b[j][i]=arr[i][j];
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
//n and m should also convert the cyclic output, otherwise the output will make an error when the matrix is not a square matrix
printf("%d ",b[i][j]);
}
printf("\n");
}
return 0;
}
```

Keywords: C Algorithm

Added by kjelle392 on Sat, 19 Feb 2022 12:52:39 +0200