5个数求最值

5个数求最值

时间限制:1000 ms  |  内存限制:65535 KB
难度:1
描述
设计一个从5个整数中取最小数和最大数的程序
输入
输入只有一组测试数据,为五个不大于1万的正整数
输出
输出两个数,第一个为这五个数中的最小值,第二个为这五个数中的最大值,两个数字以空格格开。
样例输入
1 2 3 4 5
样例输出
1 5
题目转自南阳理工学院:http://acm.nyist.edu.cn/JudgeOnline/problemset.php

个人代码(以下原创)

#include <stdio.h>

int num[5];

void quicksort(int left, int right)
{
	if (left >= right)
	{
		return;
	}

	int temp = num[left];
	int a = left;
	int b = right;

	while (a != b)
	{

		while (num[b] >= temp && b > a)
		{
			b--;
		}
		while (num[a] <= temp && b > a)
		{
			a++;
		}
		if (b > a)
		{
			int t = num[a];
			num[a] = num[b];
			num[b] = t;
		}
	}
	
	num[left] = num[a];
	num[a] = temp;

	quicksort(left, a-1);
	quicksort(a+1, right);
}
int main()
{
	for (int i = 0; i < 5; i++)
	{
		scanf("%d", &num[i]);
	}
	quicksort(0, 4);
	printf("%d %d\n", num[0], num[4]);
	

	return 0;
}



posted @ 2018-01-27 21:59  focus5679  阅读(210)  评论(0编辑  收藏  举报