⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 多处最优服务次序.cpp

📁 using greedy trategy to solve the problem of multiOptimalServe.this code runs in vc 6.0.
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>

int* read_file(  )										// 读取文件内容到数组.......模块........
{
	FILE *fp; 
	fp = fopen("input.txt", "r");
	if(fp == NULL)															// 若打开文件失败   退出程序
	{
		puts("FILE OPEN ERROR !!! .  .   .   .   \n");
		puts("press any key to exit this instance.\n\n");
		getchar();
		exit( 0 );
	}
	puts( " FILE OPENED OK  .   .   ." );

	int length,cpu;															// 若成功  则将文件中的原始数据送入数组  ....
	fscanf( fp,"%d %d",&length,&cpu );
	printf( "             .   .   .   .   作业的个数: %3d\n",length );
	printf( "             .   .   .   .   服务点个数: %3d\n",cpu );

	int* array = new int[ length+2 ];
	array[0] = length+2;
	array[1] = cpu;

	for( int i=0;i<length;i++ ) // 读入数据,送入数组
	{
		int get;
		fscanf( fp,"%d",&get );
		array[i+2] = get;
	}

	int j=fclose(fp);                 // 关闭文件
	puts( " FILE CLOSED OK  .   .\n" );

	return array;
}

//

void write_file( int output )										// 写结果到文件.......模块........
{
	FILE *fp; 
	fp = fopen("output.txt", "w");

	if( fp == NULL )
	{
		puts( " CAN NOT OPEN FILE TO WRITE THE ANSWER !\n" );
		return;
	}
	printf( " OPEN FILE TO WRITE ANSWER OK  .   .   .   .   .\n" );

	fprintf( fp,"%d",output );

	int j=fclose(fp);                 // 关闭文件
	puts( " WRITE THE ANSWER INTO FILE SUCCESFULLY.   .   .   .\n " );	
}

void init_array( int* cur,int cpux )						// 初始化数组.......
{
	int i;
	for( i=0;i<cpux;i++ )
	{
		cur[ i ] = 0;
	}
}

void bubble_sort( int* array,int length )					// 冒泡排序数组........
{
	int i=0,j=0;
	for( i=length-1;i>0;i-- )
	{
		for( j=0;j<i;j++ )
		{
			if( array[j] > array[j+1] )
			{
				int tmp;
				tmp = array[j+1];
				array[j+1] = array[j];
				array[j] = tmp;
			}
		}
	}
}

void print_array( int* cur,int cpux )						// 打印数组.........
{
	int i;
	for( i=0;i<cpux;i++ )
	{
		printf( " %d ->",cur[i] );		
	}
	printf( "\n\n" );
}

int find_min_loc( int* cur,int cpux )						// 找出数组中最小值的位置.......
{
	int loc = 0;
	int min = cur[ 0 ];
	for( int i=0;i<cpux;i++ )
	{
		if( cur[ i ] < min )
		{
			loc = i;
			min = cur[i];
		}
	}
	return loc;
}

int get_total_wait( int* current,int cpux,int* task,int task_lengthx  )   // 得到总等待时间......
{
	int task_indicator = 0;
	int total_wait = 0;	

	int loc;
	while( task_indicator < task_lengthx )  // 利用贪心法,将需时间最少的服务优先放入当前被预定时间最短的等待队列
	{	
		loc = find_min_loc( current,cpux );
		current[ loc ] += task[ task_indicator++ ];	
		total_wait += current[ loc ];
	}
	return total_wait;
}

////////////////////////.  M.A.I.N.  .  E.N.T.R.E.N.C.E .  .  .  .  .  ///////////////////////////////

int main()													// 主函数,程序入口
{
	int* task;	
	task = read_file( );
	int cpu = task[1];										// 服务点的个数 ( CPU 个数 )
	int task_length = task[0] - 2;

	task += 2;

	bubble_sort( task,task_length );						// 将初始数组冒泡排序 -> 递增数组......
	printf( "\n 原始数组作冒泡排序后: \n" );
	print_array( task,task_length );

	int* current = new int[ cpu ];							// 某服务点已预定总时间
	init_array( current,cpu );

	int total = get_total_wait( current,cpu,task,task_length );		// 得到总等待时间......

	printf( " 总共等待时间: %d\n\n",total );
	printf( " 平均等待时间: %d\n\n",total/task_length );

	write_file( total/task_length );

	getchar();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -