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