[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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值