第1章:绪论
1、三数交换
void Descend ( int & a, int & b, int & c)
{ int temp; if ( a < b) { temp = a; a = b; b = temp; } if ( a < c) { temp = a; a = c; c = temp; } if ( b < c) { temp = b; b = c; c = temp; }
}
2、一元多项式求值
float Polynomial ( int n, int a[ ] , float x)
{ int i; float sum; if ( n == 0 ) { sum = a[ 0 ] ; return sum; } sum = a[ n] * x + a[ n- 1 ] ; for ( i = n- 2 ; i >= 0 ; i -- ) { sum = sum * x + a[ i] ; } return sum;
}
3、k阶斐波那契序列
Status Fibonacci ( int k, int m, int & f)
{ if ( k < 2 || m < 0 ) { return ERROR; } int i, j, t[ 100 ] = { 0 , 0 } , sum = 0 ; if ( m < k- 1 ) f = 0 ; else if ( m == k- 1 ) f = 1 ; else { t[ k- 1 ] = 1 ; for ( i = k; i <= m; i ++ ) { for ( j = i - k; j < i; j ++ ) { sum + = t[ j] ; } t[ i] = sum; sum = 0 ; } f = t[ m] ; } return OK;
}
优化
上面的当求第m+1项后明显有问题,而且用来双重循环,耗费时间也比较多,因此优化为下面的解法,可以求出任意项的值,而且只用了一次循环.
Status Fibonacci ( int k, int m, int & f)
{ if ( k < 2 || m < 0 ) { return ERROR; } int i, j, sum = 0 ; int * t; t = ( int * ) malloc ( sizeof ( int ) * ( m+ 1 ) ) ; t[ 0 ] = 0 ; for ( i = 1 ; i <= m; i ++ ) { if ( i > k- 1 ) sum - = t[ i- k- 1 ] ; if ( i < k- 1 ) t[ i] = 0 ; else if ( i == k- 1 ) t[ i] = 1 ; else t[ i] = sum; sum + = t[ i] ; } f = t[ m] ; return OK;
}
4、计算i!*2^i的值
Status Series ( int a[ ] , int n)
{ int i, j = 1 ; Status sum = 1 ; for ( i = 0 ; i < n; i ++ ) { for ( j = i + 1 ; j > 0 ; j -- ) { sum * = j * 2 ; } if ( sum > MAXINT) { return OVERFLOW; } else { a[ i] = sum; } sum = 1 ; } return OK;
}
优化
上面的解法没有充分利用sum的值,其实下一次i!×2^i
等于上一次的值乘以i*2,所以没必要用两层循环
Status Series ( int a[ ] , int n)
{ int i, j = 1 ; Status sum = 1 ; for ( i = 1 ; i <= n; i ++ ) { sum * = i * 2 ; if ( sum > MAXINT) { return OVERFLOW; } else { a[ i- 1 ] = sum; } } return OK;
}
5、统计男女总分及团体总分
void Scores ( ResultType * result, ScoreType * score)
{ int i = 0 ; for ( i = 0 ; i < 5 ; i ++ ) { score[ i] . malescore = 0 ; score[ i] . femalescore = 0 ; score[ i] . totalscore = 0 ; } for ( i = 0 ; ; i ++ ) { if ( strcmp ( result[ i] . sport, "" ) == 0 && result[ i] . gender == male&& result[ i] . schoolname == ' ' && strcmp ( result[ i] . result, "" ) == 0 && result[ i] . score == 0 ) { break ; } switch ( result[ i] . schoolname) { case 'A' : { if ( result[ i] . gender == male) { score[ 0 ] . malescore + = result[ i] . score; } else { score[ 0 ] . femalescore + = result[ i] . score; } score[ 0 ] . totalscore + = result[ i] . score; break ; } case 'B' : { if ( result[ i] . gender == male) { score[ 1 ] . malescore + = result[ i] . score; } else { score[ 1 ] . femalescore + = result[ i] . score; } score[ 1 ] . totalscore + = result[ i] . score; break ; } case 'C' : { if ( result[ i] . gender == male) { score[ 2 ] . malescore + = result[ i] . score; } else { score[ 2 ] . femalescore + = result[ i] . score; } score[ 2 ] . totalscore + = result[ i] . score; break ; } case 'D' : { if ( result[ i] . gender == male) { score[ 3 ] . malescore + = result[ i] . score; } else { score[ 3 ] . femalescore + = result[ i] . score; } score[ 3 ] . totalscore + = result[ i] . score; break ; } case 'E' : { if ( result[ i] . gender == male) { score[ 4 ] . malescore + = result[ i] . score; } else { score[ 4 ] . femalescore + = result[ i] . score; } score[ 4 ] . totalscore + = result[ i] . score; break ; } default : break ; } }
}
优化
以上解法case部分的代码高度重复,仅仅是下标不同,因此可以先设index记录它们的下标,然后将重复代码独立出来
void Scores ( ResultType * result, ScoreType * score)
{ int i = 0 ; for ( i = 0 ; i < 5 ; i ++ ) { score[ i] . malescore = 0 ; score[ i] . femalescore = 0 ; score[ i] . totalscore = 0 ; } for ( i = 0 ; ; i ++ ) { if ( strcmp ( result[ i] . sport, "" ) == 0 && result[ i] . gender == male&& result[ i] . schoolname == ' ' && strcmp ( result[ i] . result, "" ) == 0 && result[ i] . score == 0 ) { break ; } int index = - 1 ; switch ( result[ i] . schoolname) { case 'A' : { index = 0 ; break ; } case 'B' : { index = 1 ; break ; } case 'C' : { index = 2 ; break ; } case 'D' : { index = 3 ; break ; } case 'E' : { index = 4 ; break ; } default : break ; } if ( index != - 1 ) { if ( result[ i] . gender == male) { score[ index] . malescore + = result[ i] . score; } else { score[ index] . femalescore + = result[ i] . score; } score[ index] . totalscore + = result[ i] . score; } }
}
6、对序列S的第i个元素赋值e
Status Assign ( Sequence & S, int i, ElemType e)
{ if ( & S == null) { return ERROR ; } if ( S. elem == null) { return ERROR; } if ( i < 0 || i >= S. length) { return ERROR; } S. elem[ i] = e; return OK;
}
7、由长度为n的一维数组a构建一个序列S
Status CreateSequence ( Sequence & S, int n, ElemType * a)
{ if ( n <= 0 || a == null) { return ERROR; } S. length = n; S. elem = a; return OK;
}
8、构建一个值为x的结点
LinkList MakeNode ( ElemType x)
{ LNode link; link. data = x; link. next = NULL ; return & link;
}
9、构建长度为2且两个结点的值依次为x和y的链表
LinkList CreateLinkList ( ElemType x, ElemType y)
{ LNode link; link. data = x; LinkList p; p = ( LinkList) malloc ( sizeof ( LNode) ) ; if ( p == NULL ) return NULL ; link. next = p; p-> data = y; p-> next = NULL ; return & link;
}
10、构建长度为2的升序链表
LinkList CreateOrdLList ( ElemType x, ElemType y)
{ if ( x > y) { x = x+ y; y = x- y; x = x- y; } LNode link; link. data = x; LinkList p; p = ( LinkList) malloc ( sizeof ( LNode) ) ; if ( p == NULL ) return NULL ; link. next = p; p-> data = y; p-> next = NULL ; return & link;
}
更多内容请关注微信公众号“默夜不孤”