18. lpIter->Next = NULL; 19. LPLINKLIST lpPrev = NULL; 20. 21. bool bInserted = false; 22. for ( LPLINKLIST lpI = lpHead; 23. lpI != NULL; 24. lpPrev = lpI, lpI = lpI->Next ) 25. { 26. if ( lpIter->Value < lpI->Value ) 27. { 28. lpIter->Next = lpI; 29. if ( lpPrev == NULL ) 30. lpHead = lpIter; 31. else 32. lpPrev->Next = lpIter; 33. 34. bInserted = true; 35. break; 36. } 37. } 38. 39. if ( !bInserted ) 40. lpPrev->Next = lpIter; 41. 42. lpIter = lpNext; 43. } 44.
45. return lpHead; 46. }
Write a function to print the Fibonacci numbers.
int fib ( int n ) { if (n == 0) return 1; else return fib(n-1) + fib(n-2); }
int fibo (int n) { int i, current, one_back, two_back; if (n <= 1) return 1; else { one_back = 1; two_back = 1; for ( i = 2; i <= n; i++) { current = one_back + two_back; one_back = two_back; two_back = current; } /* end for loop */ } /* end else block */ return current; }
有一个有序数组,其中可能包含重复的元素,去除其重复的部分返回数组的长度。如一个数组包含:1,3,7,7,8,9,9,9,10,运行后应当为1,3,7,8,9,10,返回长度6。
private static int CountAfterRemoveDups(int[] data) {
if ( data == null ) throw new ArgumentNullException(\ if ( data.Length == 0 ) return 0;
int result = 1;
int lastNondup = data[0];
for ( int i = 1; i < data.Length; ++i ) {
if ( data[i] != lastNondup ) {
result++;
lastNondup = data[i]; } }
return result; }
将一个字符串中的整数输出(比如“14ab5eer87”,应该输出“14,5,87”)。易犯的错误:没有分组输出,即输出为14587了。
private static string PrintGroup(string data) {
if ( data == null ) throw new ArgumentNullException(\ char[] result = new char[data.Length];
int resultIter = 0; bool doGroup = false;
foreach ( char c in data ) {
if ( c >= '0' && c <= '9' ) {
result[resultIter++] = c; doGroup = true; } else {
if ( doGroup ) {
result[resultIter++] = ','; doGroup = false; } } }
return new string(result); }
Implement an algorithm to sort an array. Why did you pick the method you did?
Bubble sort:
for(int x=0; x Quick Sort: void swap(int *x,int *y) { int temp; temp = *x; *x = *y; *y = temp; } int choose_pivot(int i,int j ) { return((i+j) /2); } void quicksort(int list[],int m,int n) { int key,i,j,k; if( m < n) { k = choose_pivot(m,n); swap(&list[m],&list[k]); key = list[m]; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i] <= key)) i++; while((j >= m) && (list[j] > key)) j--; if( i < j) swap(&list[i],&list[j]); } // swap two elements swap(&list[m],&list[j]); } } // recursively sort the lesser list quicksort(list,m,j-1); quicksort(list,j+1,n); Insertion Sort: void insertion_sort(int x[],int length) { int key,i; for(int j=1;j key=x[j]; i=j-1; while(x[i]>key && i>=0) { x[i+1]=x[i]; i--; } x[i+1]=key; } } Switch every pair of words in a string (\ private static string ReverseWordPair(string input) { if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(\); char[] result = new char[input.Length]; int resultIter = 0; int begin = 0; // skip leading whitespace while (begin < input.Length && input[begin] == ' ') { result[resultIter++] = input[begin]; begin++; } ReverseWordPairImp(result, resultIter, input, begin); return new string(result); } private static void ReverseWordPairImp(char[] result, int resultIter, string input, int inputIter) { int[] indics = new int[2]; int i = inputIter; indics[0] = inputIter; for (; i < input.Length; ++i) {
相关推荐: