#include <iostream>#include <cstring>using namespace std;#define MAX_SIZE 100int main(){ int n; cin >> n;//n * n方阵 int pn[MAX_SIZE][MAX_SIZE]; int row =-1, col = n-1; memset(pn, 0, sizeof(pn));//初始化为0 int count = 1; while (count <= n * n) { while (row < n-1 && pn[row+1][col] == 0) pn[++row][col] = count++; while (col >= 1 && pn[row][col-1] == 0) pn[row][--col] = count++; while (row >= 1 && pn[row-1][col] == 0) pn[--row][col] = count++; while (col < n-1 && pn[row][col+1] == 0) pn[row][++col] = count++; //cout <<"count=" << count << endl; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << pn[i][j] << " "; cout << endl; }//输出 return 0;}
1.void *memset(void *s,int c,size_t n)
总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。
2.memset() 函数常用于内存空间初始化。
char str[100];memset(str,0,100);
3.memset()的深刻内涵:用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;例:char a[100];memset(a, '/0', sizeof(a));
memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),会造成b的内存地址溢出。strcpy就只能拷贝字符串了,它遇到'/0'就结束拷贝;例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字符串长度(第一个‘/0’之前)是否超过50位,如超过,则会造成b的内存地址溢出。
4.头文件<memory.h>或<string.h>
