|
楼主 |
发表于 2012-5-28 20:49:53
|
显示全部楼层
几个已经完美测试的代码
本帖最后由 Wudy 于 2012-5-28 21:12 编辑
1.八皇后- int C[8];
- int tot = 0;
- void search( int cur )
- {
- int i, j;
-
- if( cur == 8 )
- tot += 1;
- else
- {
- for(i = 0; i < 8; i += 1 )
- {
- int ok = 1;
- C[cur] = i;
- for( j = 0; j < cur; j += 1 )
- if( C[cur] == C[j] || cur - C[cur] == j - C[j] || cur + C[cur]
- == j + C[j] )
- {
- ok = 0;
- break;
- }
- if( ok )
- search( cur + 1 );
- }
- }
- }
- void main()
- {
- search( 0 );
- printf( "total : %d\n", tot );
- }
复制代码 2.快排- int partition( int* a, int p, int r )
- {
- int x, i, j;
- x = a[r];
- i = p - 1;
- for( j = p; j < r; j += 1 )
- {
- if( a[j] <= x )
- {
- i += 1;
- swap( &a, &a[j] );
- }
- }
- swap( &a[i+1], &a[r] );
- return i + 1;
- }
- void qsort( int *a, int p, int r)
- {
- int q;
- if( p < r )
- {
- q = partition( a, p, r );
- qsort( a, p, q-1 );
- qsort( a, q+1, r );
- }
- }
- void main()
- {
- int a[5];
- int i;
- for( i = 0; i < 5; i += 1 )
- scanf( "%d", &a );
- qsort( a, 0, 4 );
- printf( "-----------\n" );
- for( i = 0; i < 5; i += 1 )
- printf( "%d\n", a );
- }
- void swap( int *a, int *b )
- {
- int t;
- t = *a; *a = *b; *b = t;
- }
复制代码 3.hello world- void main()
- {
- printf( "%s\n", "Hello World!" );
- }
复制代码 |
|