UVA - 11292 - Dragon of Loowater

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2267


题意:

    输入Dragon数组和Knight数组,Knight比Dragon大时付款,计算出最小的总付款值。

解题:

    普通的模拟水题,回来没事动动脑子。再不动脑就要生锈啦!

    先使用qsort/sort快速排序(升序),然后按顺序从Dragon找出合适(最小)的Knight来。


#include 
#include 
#include 
#include 
using namespace std;

// #define LOCAL_TEST

const int MAX_SIZE = 20000+1;


int fun_compare(const void* a, const void* b)
{
	return ( *(int*)a - *(int*)b );
}


int main()
{
#ifdef LOCAL_TEST
	freopen("f:\\in.txt", "r", stdin);
	freopen("f:\\out.txt", "w+", stdout);
#endif
	int szDragons[MAX_SIZE];
	int szKnights[MAX_SIZE];

	int nDragon;
	int mKnight;

	while ( cin >>nDragon >> mKnight )
	{
		if ( nDragon == 0 && mKnight == 0 )
			break;

		// get Dragons & Knights data and sort
		memset(szDragons, 0, sizeof(szDragons));
		memset(szKnights, 0, sizeof(szKnights));
		for ( int i=0; i>szDragons[i];
		for ( int i=0; i>szKnights[i];
		qsort(szDragons, nDragon, sizeof(szDragons[0]), fun_compare);
		qsort(szKnights, mKnight, sizeof(szKnights[0]), fun_compare);
		
		// special situation
		if ( nDragon > mKnight )
		{
			cout <<"Loowater is doomed!" <<"\n";
			continue;
		} // end if

		// common situation, start to find the Knight for mininal
		int sum = 0;
		bool bFlagWin = true;
		int iDragon=0, iKnight=0;
		while ( 1 )
		{
			if ( iDragon >= nDragon )
			{
				bFlagWin = true;
				break;
			} // end if

			if ( iKnight >= mKnight )
			{
				bFlagWin = false;
				break;
			} // end if

			if ( szDragons[iDragon] <= szKnights[iKnight] )
			{
				sum += szKnights[iKnight];
				iDragon++;
				iKnight++;
			} // end if
			else
			{
				iKnight++;
			} // end else
		} // end while

		// print out the result
		if ( bFlagWin )
			cout <


注意:

    ① 使用memset的时候要加入string.h。(必须带.h)

    ② sort默认是升序,而且只能对应基础类型数据;而qsort要自定义compare函数,compare成立时交换顺序。记得头文件。要多练习。

    ③ 在可视化的基础上,能简约则简约。现在不够简约!


你可能感兴趣的:(UVA)