【题目19】一些简单的算法练习题(1)

1. 比较两个字符串,用O(n)时间和恒量空间。

 

源代码:

 #include <stdio.h> #include <assert.h> int compare(const char *s1, const char *s2) { assert(s1 != NULL && s2 != NULL); const char *p = s1; const char *q = s2; while( *p != '/0' && *q != '/0') { if(*p++ == *q++) continue; else if(*p++ > *q++) return 1; else return -1; } if(*p == '/0' && *q != '/0') return -1; if(*p != '/0' && *q == '/0') return 1; return 0; } int main() { char* s1 = "hello"; char* s2 = "world"; int ret = compare(s1,s2); printf("compare /"%s/" and /"%s/"/n",s1,s2); if(ret == 0) printf("s1 = s2/n"); else if(ret == -1) printf("s1 < s2/n"); else printf("s1 > s2/n"); s1 = "good"; s2 = "good"; printf("compare /"%s/" and /"%s/"/n",s1,s2); ret = compare(s1,s2); if(ret == 0) printf("s1 = s2/n"); else if(ret == -1) printf("s1 < s2/n"); else printf("s1 > s2/n"); s1 = "aaabbb"; s2 = "aaab"; ret = compare(s1,s2); printf("compare /"%s/" and /"%s/"/n",s1,s2); if(ret == 0) printf("s1 = s2/n"); else if(ret == -1) printf("s1 < s2/n"); else printf("s1 > s2/n"); return 0; }

 2. 假设你有一个用1001个整数组成的数组,这些整数是任意排列的,但是你知道所有的整数都在

1到1000(包括1000)之间。此外,除一个数字出现两次外,其他所有数字只出现一次。假设你只

能对这个数组做一次处理,用一种算法找出重复的那个数字。如果你在运算中使用了辅助的存储方

式,那么你能找到不用这种方式的算法吗?

#include <stdio.h> int FindShowTwice(int *a,int len) { bool *b = new bool[len-1]; int i; for(i = 0; i < len-1; i++) b[i] = false; for(i = 0; i < len; i++) { if(b[a[i] - 1] == true) return a[i]; if(b[a[i] - 1] == false) b[a[i] - 1] = true; } delete[] b; return -1; } int main() { int a[1001]; for(int i = 0; i < 1001; i++) a[i] = i+1; a[1000] = 15; printf("%d /n",FindShowTwice(a,1001)); return 0; }

3.给定两个排好序的数组,怎样高效得判断这两个数组中存在相同的数字

#include <stdio.h> bool FindSameNum(int*a,int lenA, int* b, int lenB) { int i = 0, j = 0; while(true) { if(i == lenA && j == lenB) return false; if(a[i] == b[j]) break; if(a[i] > b[j]) j++; if(a[i] < b[j]) i++; } return true; } int main() { int a[] = {2,3,5,6,7,8,32,43}; int b[] = {4,12,24,32,44,55}; if(FindSameNum(a,sizeof(a)/sizeof(int),b,sizeof(b)/sizeof(int))) printf("Have the same number./n"); else printf("Don't have the same number./n"); return 0; }

4. 删除字符串中指定的某个字符

#include <stdio.h> void DelSomeChar(char* str, int len, char c) { int i = 0,j = 0; for(i = 0; i < len; i++) if(str[i] != c) str[j++] = str[i]; str[j] = '/0'; } int main() { char s[] = "abafdfafsdfasdfasdfaa"; int len = sizeof(s)/sizeof(char); DelSomeChar(s,len,'a'); printf("%s/n",s); return 0; }

5. 删除字符串中特定的多个字符(>=1)

解法转载自:http://zhedahht.blog.163.com/blog/static/25411174200801931426484/

#include <stdio.h> #include <stdlib.h> #include <string.h> void DelMultiChars(char *pStrSrc, char* pStrDel) { if(NULL == pStrSrc || NULL == pStrDel) return; const unsigned int nTableSize = 256; int hashTable[nTableSize]; memset(hashTable,0,sizeof(hashTable)); const char*pTemp = pStrDel; while('/0' != *pTemp) { hashTable[*pTemp] = 1; ++pTemp; } char* pSlow = pStrSrc; char* pFast = pStrSrc; while('/0' != *pFast) { if(1 != hashTable[*pFast]) { *pSlow = *pFast; ++pSlow; } ++pFast; } *pSlow = '/0'; } int main() { char src[] = "hello,world"; char del[] = "wl"; printf("%s/n",src); DelMultiChars(src,del); printf("%s/n",src); return 0; }

6. 请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语

用一个简单的数学知识来解答。

#define MAX(a,b) ((a+b)+ abs(a-b))/2

你可能感兴趣的:(c,算法,null,delete,存储)