[Loops]D. Liang 4.9 Finding the highest two score.c

Description
Write a program that reads in the number of students, each student’s score, and displays the highest score and the second-highest score.
Input
The first line contains a positive integer n, n is the number of students.
The second line contains n positive integers seperated by one blank which are the students’ scores. Each student’s score is unique.
Output
The higithest score and the second-highest score, seperated by one blank, followed by a new line.
Sample Input
3
90 80 95
Sample Output
95 90

Knowledge supplement:
Is the full name of malloc memory allocation, called Dynamic memory allocation in Chinese, is used to specify the size of the application for a continuous block of memory area to void
Returns the area of memory addresses, when you cannot know the specific memory location, and you want to bind the real memory space, you will need to use dynamic allocation of memory, and the size of the allocated size is the program’s requirements.
*

//  Date:2020/3/13
//  Author:xiezhg5 
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n = 0, i = 0;
    int max=0,rmax=0;  //初始化 
    scanf("%d", &n);   //提示输入n个数 
    // malloc动态内存分配 
    //sizeof判断数据类型或者表达式长度 
    int *p1 = (int *)malloc(sizeof(int)*n);    //强制类型转换 
    do
    {
        scanf("%d", p1+i);     //接收用户输入的整数 
        i++;        
    } while (getchar() != '\n');  //回车跳出循环 
    int *p2 = p1;
    for (i = 0; i < n; i++)    //执行n次循环 
    {
    	//神奇的算法 
        if (*p2 > max)
        {
            *p2 = *p2 + max;
            max = *p2 - max;
            *p2 = *p2 - max;
        }
        if (*p2 > rmax)
        {
            *p2 = *p2 + rmax;
            rmax = *p2 - rmax;
        }
        p2++;   //p2自增求得max和rmax的值 
    }
    printf("%d %d",max,rmax);
    free(p1);     //释放内存 
    return 0;
}
发布了52 篇原创文章 · 获赞 25 · 访问量 1291

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/104846228