高级算法分析与设计的第一题

the results:

// Functions
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int get_max_times(int a[]);                     // get the max of times in compare differently
int get_min_times(int a[]);                     // get the min of times in compare differently
int integer_random(int min,int max);            // get a random integer in range from min to max
int main()
{
	//get_max_times();get_min_times()  
	srand((int)time(0));
	int i, count_max_all = 0, count_min_all = 0;
	double count_max_average,count_min_average; //±äÁ¿1£¨2£©Í³¼Æ²éÕÒ×î´ó£¨Ð¡£©ÖµµÄ´ÎÊý£» 
	double iterate_number = 1000.0;                //Ñ­»·µÄ×Ü´ÎÊý 
	int min = 1, max = 20;
	int a[10];
	for (i=0; i<10; ++i) {
		a[i] = integer_random(min,max);
	}
//	for (i=0; i<4; ++i){
//		cout<< a[i];
//	}
	for (i=1; i<= iterate_number; ++i){
		count_max_all += get_max_times(a); 
		count_min_all += get_min_times(a);
	}
	count_max_average = count_max_all/iterate_number;
	count_min_average = count_min_all/iterate_number;
	cout << "The average times to find the max in array a = " <<count_max_average <<endl;
	cout << "The average times to find the min in array a = " <<count_min_average <<endl;
	return 0;
}

int integer_random(int min,int max)
{
	int result = rand() % max + min;
	return result;	
}
int get_max_times(int a[])
{
	int i; // the definition of i is to control the time of iteration
	int count = 0; //log the time with comparing the difference 
	int max = a[0];   // store the biggest interger in array
	for (i=1; i<10; ++i){
		if(a[i] > max){
			max = a[i];
			++count;
		}
	} 
	return count;
}
int get_min_times(int a[])
{
	int i; // the definition of i is to control the time of iteration
	int count = 0; //log the time with comparing the difference 
	int min = a[0];   // store the biggest interger in array
	for (i=1; i<10; ++i){
		if(a[i] < min){
			min = a[i];
			++count;
		}
	} 
	return count;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值