CCF-CSP Questions (one)

This is the first question of CCF-CSP in March 2019.

Posted by Dusign on 2019-04-14
Words 370 and Reading Time 2 Minutes
Viewed Times

Algorithms change the world. The importance of algorithms is self-evident to a programmer, so the practice of some algorithms is indispensable. Next, I will share some algorithmic problems.

Question

question

Analysis

This problem involves the maximum, minimum and median. Because the knowledge is relatively simple, I will not explain it here.

Solution

Here is the solution to this problem.The following are two solutions, the result is 100 points.

Program

Method One

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// main.cpp
// sml
//
// Created by gang du on 2019/4/14.
// Copyright © 2019 gang du. All rights reserved.
//

#include<cstdio>

const int MAXN = 100000;
int n, data[MAXN];

int main() {
int i, max, min;
double middle;
scanf("%d", &n);
for (i=0; i<n; i++){
scanf("%d", &data[i]);
}

if (data[0] <= data[n-1]){
max = data[n-1];
min = data[0];
}else{
min = data[n-1];
max = data[0];
}

if (n%2==0){
middle = (data[n/2]+data[n/2-1])*1.0/2;
if (middle - (int)middle == 0){
printf("%d %d %d\n", max, (int)middle, min);
}else{
printf("%d %.1f %d\n", max, middle, min);
}
}else{
middle = data[(n-1)/2];
printf("%d %d %d\n", max, (int)middle, min);
}

return 0;
}

Method Two

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// main.cpp
// sml
//
// Created by gang du on 2019/4/14.
// Copyright © 2019 gang du. All rights reserved.
//

#include<cstdio>

int main() {
int n, data, max=0, min=0;
double middle=0;

scanf("%d", &n);
int parity = n & 1;

for (int i=0; i<n; i++){
scanf("%d", &data);
if(i==0) min=data;
if(i==n-1) max=data;
if(parity && i==n/2){
middle=data;
}
if(!parity && (i==n/2 || i==n/2-1))
middle += data*1.0/2;
}

if(max<=min){
int temp = max;
max = min;
min = temp;
}

if(middle - (int)middle == 0){
printf("%d %d %d\n", max, (int)middle, min);
}else{
printf("%d %.1f %d\n", max, middle, min);
}

return 0;
}

Remarks

This program is written in C++ and uses standard input and output functions.


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !

...

...

00:00
00:00