读程序,写出程序运行的结果
1 #include <stdio.h> 2 void selectsort(int v[], int n) { 3 int i, j, min, temp; 4 for( i = 0; i < n-1; i++) { 5 min = i; 6 for( j = i + 1; j < n; j++) 7 if(v[j] < v[min]) 8 min = j; 9 temp = v[min]; 10 v[min] = v[i]; 11 v[i] = temp; 12 } 13 } 14 int main() { 15 int i; 16 int s[] = { 5, 4, 3, 2, 1}; 17 selectsort(s, 5); 18 for(i = 0; i < 5; i++) 19 printf("%3d", s[i]); 20 }
假定int, unsigned是32位的。
1 #include <stdio.h> 2 #define MAXVAL 10 3 int queue[MAXVAL]; 4 int front = 0; 5 int back = 0; 6 int empty() { 7 return front == back; 8 } 9 int full() { 10 return (back + 1)%MAXVAL == front; 11 } 12 int size() { 13 return (back - front + MAXVAL) % MAXVAL; 14 } 15 void enqueue(int v) { 16 if(full()) 17 return; 18 queue[back++] = v; 19 back = back % MAXVAL; 20 } 21 int dequeue() { 22 int v; 23 if( empty() ) 24 return 0; 25 v = queue[front++]; 26 front = front % MAXVAL; 27 return v; 28 } 29 main() { 30 int i; 31 for(i = 1; !full(); i++) 32 enqueue(i); 33 print("%d\n", size()); 34 while(!empty()) 35 printf("%d\n", dequeue()); 36 }
1 #include <stdio.h> 2 #include <ctype.h> 3 int stricmp(char s[], char t[]) { 4 int i; 5 for( i = 0; s[i] != '\0' && t[i] != '\0'; i++) 6 if(tolower(s[i]) != tolower(t[i])) 7 break; 8 return tolower(s[i]) - tolower(t[i]); 9 } 10 main() { 11 char s1[] = { 'a', 'b', 'c', 'd', '\0'}; 12 char s2[] = "ABCD"; 13 char s3[] = "abcde"; 14 int s1s2 = stricmp(s1, s2); 15 int s2s3 = stricmp(s2, s3); 16 printf("%s is %s %s\n", s1, s1s2==0 ? "equal to" : s1s2 > 0 ? "great than" : "less than" , s2); 17 printf("%s is %s %s\n", s2, s2s3==0 ? "equal to" : s2s3 > 0 ? "great than" : "less than" , s3); 18 }
编程题,按要求编写程序
- 分别编写递归方式和非递归方式的reverse(s)函数,用于将字符串s倒过来
- 分别编写递归方式和非递归方式的fact(n)函数,用于计算n的阶乘n!
- 分别编写递归方式和非递归方式的combination(m, n),用于计算组合数