#include<iostream>
#include<malloc.h>
using namespace std;
struct data
{
double a;
struct data *next;
};
int main()
{
int n, k, t;
int i = 0;
struct data *head, *p, *q;
head = (struct data *)malloc(sizeof(struct data));
head->next = NULL;
p = head;
cout << "输入n" << endl;
cin >> n;
cout << "输入" << n << "个数" << endl;
while (n > 0)//会多创一块内存
{
cin >> p->a;
q = (struct data *)malloc(sizeof(struct data));
q->next = NULL;
p->next = q;
p = q;
q = q->next;
n--;
}
cout << "输入k" << endl;
cin >> k;
t = k;
double x;
for (p = head; p->next != NULL; p = p->next)
{
for (q = p->next; q->next != NULL; q = q->next)
{
if (p->a < q->a)
{
x = p->a;
p->a = q->a;
q->a = x;
}
}
k--;
if (k == 0)
break;
}
cout << "第" << t << "大的数: ";
cout << p->a << endl;
p = head;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
head = NULL;
return 0;
}
输入n个数,输出第k大个数
最新推荐文章于 2022-01-02 20:06:31 发布