【Pointers On C】C和指针部分编程题 solution

第6章 指针


6.18.3   编写函数reverse_string,  函数原型为 void reverse_string(char *string);

#define NUL '\0'

void reverse_string(char *str)
{
	char *tail = str;
	char *head = str;
	int len = 0;
	for (; *tail++ != NUL;) len ++;
	cout << len << endl;
	for(int i = 0; i < len/2; i ++)
	{
		char tmp = *(str+i) ;
		*(str+i) = *(str+len-i-1);
		*(str+len-i-1) = tmp;
	}

}


第7章 函数

7.11.1  递归编写Hermite Polynomials, 函数原型为 int hermite(int n, int x);

int hermite(int n, int x)
{
	if(n <= 0) return 1;
	if(n == 1) return 2*x;
	return  2*x * hermite(n-1, x) - 2*(n-1) * hermite(n-2, x);
}


7.11.1  函数原型 int ascii_to_integer(char *string)

int ascii_to_integer(char *string)
{
	int ans = 0;
	while(*string != '\0')
		ans = ans * 10 + (*string ++) - '0';
	return ans;
}

第8章 数组

8.8.5  矩阵乘法, 函数原型 void matrix_multiply(int *m1, int *m2, int *r, int x, int y, int z);

void matrix_multiplication(int *m1, int *m2, int *r, int x, int y, int z)
{
	for(int i = 0; i < x; i ++)
		for(int j = 0; j < z; j ++)
			for(int k = 0; k < y; k ++)
				*(r+(i*z+j)) += (*(m1+(i*y+k))) * (*(m2+(k*z+j)));
	for(int i = 0; i < x; i ++)
	{
		for(int j = 0; j < z; j ++)
			cout << *(r+i*x+j) << " ";
		cout << endl;
	}
	
}

int main()
{

	int a[][2] = {2, -6, 3, 5, 1, -1};
	int b[][4] = {4, -2, -4, -5, -7, -3, 6, 7};
	int c[3][4] = {0};
	memset(c, 0, sizeof(c));
	matrix_multiplication(&a[0][0], &b[0][0], &c[0][0], 3, 2, 4);

	return 0;
}

8.8.8 八皇后

const int N = 10;
bool mark[N][N];
int q[N][N];
int dir[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, -1, -1, 1};

bool safe(int row, int col)
{
	memset(mark, false, sizeof(mark));
	for(int i = 0; i < 8; i ++)
		for(int j = 0; j < 8; j ++)
		{
			if(q[i][j]){
				for(int k = 0; k < 8; k ++)
				{
					int tx = i + dir[k][0];
					int ty = j + dir[k][1];
					while(tx < 8 && ty < 8 && tx >= 0 && ty >= 0)
					{
						mark[tx][ty] = true;
						tx += dir[k][0];
						ty += dir[k][1];
					}
				}
			}
		}
	return (!mark[row][col]);
}

void queen(int row, int col)
{
	if(row == 7)
	{
		for(int i = 0; i < 8; i ++){
			for(int j = 0; j < 8; j ++) cout << q[i][j];
			cout << endl;
		}
		cout << "--------" << endl;
		return ;
	}
	for(int i = 0; i < 8; i ++)
	{
		if(safe(row+1, i)){
			q[row+1][i] = 1;
			queen(row+1, i);
			q[row+1][i] = 0;
		}
	}
}

int main()
{
	for(int i = 0; i < 8; i ++)
	{
		q[0][i] = 1;
		queen(0, i);
		q[0][i] = 0;
	}
	return 0;
}


 

Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Contents A Quick Start ........................................................................................................ 1 Basic Concepts ...................................................................................................... 7 Data ....................................................................................................................... 11 Statements ............................................................................................................. 15 Operators and Expressions .................................................................................... 23 Pointers .................................................................................................................. 29 Functions ............................................................................................................... 37 Arrays .................................................................................................................... 43 Strings, Characters, and Bytes .............................................................................. 55 Structures and Unions ........................................................................................... 69 Dynamic Memory Allocation ................................................................................ 75 Using Structures and Pointers ............................................................................... 79 Advanced Pointer Topics ...................................................................................... 87 The Preprocessor ................................................................................................... 93 Input/Output Functions .......................................................................................... 95 Standard Library .................................................................................................... 119 Classic Abstract Data Types ................................................................................. 129 Runtime Environment ........................................................................................... 145
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值