编辑: 旋风 | 2019-07-16 |
1、#include string.h #include
2、main()
3、{
4、 char*src= hello,world ;
5、 char* dest=NULL;
6、 int len=strlen(src);
7、 dest=(char*)malloc(len);
8、 char* d=dest;
9、 char* s=src[len];
s=&
src[len-1]
10、 while(len--!=0)
11、 d++=s--;
*d++ = *s--;
*d = '
\0'
;
12、 printf( %s ,dest);
13、 return 0;
14、} 答: 方法1: int main(){ char* src = hello,world ;
int len = strlen(src);
char* dest = (char*)malloc(len+1);
//要为\0分配一个空间 char* d = dest;
char* s = &
src[len-1];
//指向最后一个字符 while( len-- !=
0 ) *d++=*s--;
*d = 0;
//尾部要加\0 printf( %s\n ,dest);
free(dest);
// 使用完,应当释放空间,以免造成内存汇泄露 return 0;
} 方法2: #include #include main() { char str[]= hello,world ;
int len=strlen(str);
char t;
for(int i=0;
i>
)%256 } 请问hash(16),hash(256)的值分别是: A.1.16;
B.8.32;
C.4.16;
D.1.32 四.找错题: 1.请问下面程序有什么错误? int a[60][250][1000],i,j,k;
for(k=0;
knext->
front = pNode->
front;
} pNode->
front->
next = pNode->
next;
} Node *pNextNode = pNode->
next;
delete pNode;
pNode = pNextNode;
bRet = TRUE;
//不要break或return, 删除所有 } else { pNode = pNode->
next;
} } return bRet;
} void DE(Node *pHeadA, Node *pHeadB) { if (pHeadA == NULL || pHeadB == NULL) { return;
} Node *pNode = pHeadA;
while (pNode != NULL) { if (DeteleNode(pHeadB, pNode->
data)) { if (pNode->
front == NULL) { pHeadA = pNode->
next;
pHeadA->
front = NULL;
} else { pNode->
front->
next = pNode->
next;
if (pNode->
next != NULL) { pNode->
next->
front = pNode->
front;
} } Node *pNextNode = pNode->
next;
delete pNode;
pNode = pNextNode;
} else { pNode = pNode->
next;
} } } 2. 编程实现:找出两个字符串中最大公共子字符串,如 abccade , dgcadde 的最大子串为 cad int GetCommon(char *s1, char *s2, char **r1, char **r2) { int len1 = strlen(s1);
int len2 = strlen(s2);
int maxlen = 0;
for(int i = 0;
i <
len1;
i++) { for(int j = 0;
j <
len2;
j++) { if(s1[i] == s2[j]) { int as = i, bs = j, count = 1;
while(as +
1 <
len1 &
&
bs +
1 <
len2 &
&
s1[++as] == s2[++bs]) count++;
if(count >
maxlen) { maxlen = count;
*r1 = s1 + i;
*r2 = s2 + j;
} } } } 3. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系 列库函数 char* test3(long num) { char* buffer = (char*)malloc(11);
buffer[0] = '
0'
;
buffer[1] = '
x'
;
buffer[10] = '
\0'
;
char* temp = buffer + 2;
for (int i=0;
i <
8;
i++) { temp[i] = (char)(num28);
temp[i] = temp[i] >
=
0 ? temp[i] : temp[i] + 16;
temp[i] = temp[i] <
10 ? temp[i] +
48 : temp[i] + 55;
} return buffer;
} 输入N, 打印 N*N 矩阵 比如 N = 3,打印:
1 2
3 8
9 4
7 6
5 N = 4,打印:
1 2
3 4
12 13
14 5
11 16
15 6
10 9
8 7 解答:
1 #define N
15 int s[N][N];
void main() { int k = 0, i = 0, j = 0;
int a = 1;
for( ;
k <
(N+1)/2;
k++ ) { while( j <
N-k ) s[i][j++] = a++;
i++;
j--;
while( i <
N-k ) s[i++][j] = a++;
i--;
j--;
while( j >
k-1 ) s[i][j--] = a++;
i--;
j++;
while( i >