0001 #include <stdio.h> 0002 #include <string.h> 0003 #include <stdlib.h> 0004 #include <time.h> 0005 #include "_share/util.h" 0006 #include "UniPrint/print.h" 0007 #include "vector/vector.h" 0008 #include "random/randomSortedVector.h" 0009 #include "trivialMedian.h" 0010 #include "median.h" 0011 0012 /****************************************************************************************** 0013 * 中位数算法测试 0014 ******************************************************************************************/ 0015 int main ( int argc, char* argv[] ) { 0016 // 解析命令行 0017 if ( 7 > argc ) { printf ( "Usage:\n\t%s <#tests> <n> <min1> <max1> <min2> <max2>\a\a\n", argv[0] ); return 1; } 0018 int nTest = atoi ( argv[1] ); //测试轮数 0019 int n = atoi ( argv[2] ); //向量长度 0020 int min1 = atoi ( argv[3] ), max1 = atoi ( argv[4] ); //数值范围 0021 int min2 = atoi ( argv[5] ), max2 = atoi ( argv[6] ); //数值范围 0022 if ( ( nTest < 1 ) || ( n < 1 ) || ( max1 + 1 < min1 + n ) || ( max2 + 1 < min2 + n ) ) 0023 { printf ( "arguments error\a\a\n" ); return 1; } 0024 // 随机种子 0025 srand ( ( unsigned int ) time ( NULL ) ); 0026 // 每次测试100组 0027 for ( int i = 0; i < nTest; i++ ) { 0028 // 随机生成向量 0029 Vector<int> A1 = randomSortedVector ( n, min1, max1 ); print ( A1 ); 0030 Vector<int> A2 = randomSortedVector ( n, min2, max2 ); print ( A2 ); 0031 // 计算中位数,并与蛮力算法(更不易出错)的结果对比 0032 int m1 = median ( A1, 0, A2, 0, n ); 0033 int m2 = trivialMedian ( A1, 0, n, A2, 0, n ); 0034 if ( m1 == m2 ) { printf ( "\nmedian: %d = %d\a\n\n\n", m1, m2 ); } 0035 else { printf ( "\nmedian: %d <Error> %d\a\a\n\n\n", m1, m2 ); getchar(); } 0036 } 0037 // 返回 0038 return 0; 0039 }