当前位置:首页 > 第五章习题答案
Liming Luwei 3.
#include
char ? s;
Node ? q; };
int main() {
Node a[ ] = { { \a+1 }, { \
a+2 }, { \
Node ?p = a;
cout << p->s << endl; cout << p->q->s << endl; cout << p->q->q->s << endl; cout << p->q->q->q->s << endl; }
【解答】
Mary Jack Jim Mary 5.3 思考题
1.判断一个整数n的奇偶性,可以利用位运算判断吗?请你试一试。 【解答】
可以。一个整数当最低位为1时,它是奇数,否则为偶数。以下函数返回对参数k的奇偶判断。
bool odd( int k ) {
return 1&k; }
2.长度为N的数组可以表示N个元素的集合,若有:
S[i]==1,表示对应元素在集合中
如何实现集合的基本运算?请你试一试。并从内存和处理要求上与5.2.2节中集合的实现方法进行比较。 【解答】
长度为N的数组S可以表示有N个元素的集合。当S[i]==1,表示元素i+1在集合中;当S[i]==0,表示元素i+1不在集合中。集合运算通过对数组元素操作完成。
用数组实现集合运算的空间和时间消耗高于用无符号整数和位运算实现集合运算。 用数组实现集合运算程序如下。
#include
void setPut( unsigned *S ); //输
入集合S的元素
void setDisplay( const unsigned *S ); //
输出集合S中的全部元素
bool putX( unsigned *S, unsigned x ); //
元素x并入集合
void Com( unsigned *C, const unsigned *A,
const unsigned *B); //求并集C=A∪B
void setInt( unsigned *C, const unsigned A,
const unsigned B); //求交集C=A∩B
void setDif( const unsigned A, const unsigned
B ); //求差集C=A-B
bool Inc( const unsigned *A, const unsigned
*B ); //判蕴含
bool In( const unsigned *S, const unsigned
x ); //判属于x∈S
bool Null( const unsigned *S );
//判空集
const int N=32; //输入集合元素 void setPut(unsigned *S) {
unsigned x;
cin >> x;
while( x>0&&x<=N) {
putX( S, x ); //把输入元素并入集合S cin >> x; } }
//输出集合S中的全部元素 void setDisplay( const unsigned *S ) {
cout << \ if (Null(S)) cout<<\ }\\n\ else {
for( int i=0; i if( S[i] ) cout << i+1 << \ } cout << \ //擦除最后的逗号 }
共分享92篇相关文档