世界杯分组
太久不写代码,都不知掉怎么写了,我感觉我要废掉了。昨天写的迭代版本,应该很难看,花了好些时间改写成递归版本,现在应该会好看很多。
另外,只写了32强的分组,没有写32进16等等。
树画出来大致是这样的
阿根廷
阿根廷 英格兰
阿根廷 德国 日本 英格兰
程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> #define T League typedef struct T *T; #define TreeHeigh 5 char *Other[] = {"英格兰","意大利","葡萄牙","克罗地亚","土耳其","瑞典","捷克","塞尔维亚","加纳","科特迪瓦","突尼斯","尼日利亚","喀麦隆","日本","韩国","澳大利亚","伊朗","美国","墨西哥","哥斯达黎 加","洪都拉斯","巴拉圭","智利","厄瓜多尔"}; char *Send[] = { "乌拉圭","西班牙","德国","阿根廷","荷兰","法国","巴西","俄罗斯"}; int F_Send[ 8 ]; int F_Other[ 24 ]; struct T{ int Score; int Heigh; T Left; T Right; char *Name; }; void INIT( T *Root, int Heigh ); void Print( T Root ); int main( void ) { T Root; int heigh; heigh = 0; Root = NULL; INIT( &Root, heigh ); Print( Root ); return 0; } void INIT( T *Root, int Heigh ) { int f; static int g = -1; srand( ( unsigned )time( NULL ) ); if( NULL == *Root ) { T new = ( T )malloc( sizeof( struct T ) ); if( NULL == new ) exit( EXIT_FAILURE ); new->Left = NULL; new->Right = NULL; new->Name = NULL; new->Score = -1; new->Heigh = Heigh; *Root = new; } if( TreeHeigh == ( *Root )->Heigh ) { ++g; if( 0 == g % 4 ) { while( 1 ) { f = rand( ) % 8; if( 0 == F_Send[ f ] ) { ( *Root )->Name = Send[ f ]; F_Send[ f ] = 1; break; } } } else { while( 1 ) { f = rand() % 24; if( 0 == F_Other[ f ] ) { ( *Root )->Name = Other[ f ]; F_Other[ f ] = 1; break; } } } } if( TreeHeigh == Heigh ) return; INIT( &(*Root)->Left, Heigh + 1 ); INIT( &(*Root)->Right, Heigh + 1 ); } void Print( T Root ) { static int g = 0; static char ch = 'A'; if( NULL == Root ) return; if( NULL != Root->Name ) { if( 0 == g % 4 ) printf( "%c\n", ch++); printf( "%s ", Root->Name ); ++g; if( 0 == g % 2 ) printf( "\n" ); } Print( Root->Left ); Print( Root->Right ); }
[此贴子已经被作者于2017-7-10 22:17编辑过]