min & max
visualxyk
posted @ 2011年8月17日 16:14
in 未分类
, 588 阅读
#include <iostream> using namespace std; void print_array(int* array, int size) { for (int i=0; i<size; i++) cout << array[i] << " "; cout << endl; } // 比逐个比较每两个省去一次比较时间 void get_min_max(int* array, int size, int& min, int& max) { min = max = array[0]; int minIdx = 0, maxIdx = 0; for (int i=0; i<size-1; i+=2) { if (array[i] < array[i+1]) // 一次比较 { minIdx = i; maxIdx = i+1; } else { minIdx = i+1; maxIdx = i; } if (array[minIdx] < min) // 二次比较 min = array[minIdx]; if (array[maxIdx] > max) // 三次比较 max = array[maxIdx]; } if (size % 2 != 0) { if (array[size-1] < min) min = array[size-1]; if (array[size-1] > max) max = array[size-1]; } } void main() { int* array = 0; int size; cout << "input the size:"; cin >> size; array = new int[size]; for (int i=0; i<size; i++) array[i] = rand()%1000; print_array(array, size); int min, max; get_min_max(array, size, min, max); cout << "min:" << min << " max:" << max << endl; }
2021年9月16日 03:32
What a thrilling post, you have pointed out some excellent points, I as well believe this is a superb website. I have planned to visit it again and again. soap 2 day
2021年11月04日 09:05
A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. 오피시티
2021年11月17日 21:05
I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... http://judislots.com/
2021年12月05日 20:32
I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. situs togel