void quickSort(int *arr, int nLen) { if(arr == NULL || nLen < 1) return; int Begin = 0; int End = nLen - 1; int key = arr[Begin]; while(Begin < End) { while(Begin < End && arr[End] >= key) --End; if(Begin < End) arr[Begin++] = arr[End]; while(Begin < End && arr[Begin] < key) ++Begin; if(Begin < End) arr[End--] = arr[Begin]; } arr[Begin] = key; quickSort(arr, Begin); quickSort(arr + Begin + 1, nLen - Begin - 1); } void Print(int *arr, int nLen) { if(arr == NULL || nLen < 1) return; printf("%d", arr[0]); for(int i = 1; i < nLen; ++i) { printf(",%d", arr[i]); } printf("\n"); } int main() { const int max_len = 1030; const int size = 130; char str[max_len]; int arr[size]; int nLen,i; while(gets(str)) { nLen = 0; i = 0; while(str[i] != '\0') { sscanf(&str[i], "%d", &arr[nLen]); ++nLen; while(str[i] != '\0' && str[i] != ',') ++i; if(str[i] == ',') ++i; } quickSort(arr, nLen); Print(arr, nLen); } return 0; }第二题解析
bool IsTrue(unsigned long long num) { while(num % 2 == 0) num /= 2; while(num % 3 == 0) num /= 3; while(num % 5 == 0) num /= 5; return num == 1; } int main() { const int size = 501; unsigned long long arr[size]; unsigned long long base = 1; int count = 0; while(count < size) { if(IsTrue(base)) { arr[count++] = base; } ++base; } int n; while(scanf("%d", &n) != EOF) { if(n < 1 || n > 500) printf("-1\n"); else printf("%d\n", arr[n - 1]); } return 0; }第三题解析:
#include<stdio.h> #include<string.h> int main() { const int N = 10; int map[N][N]; int stack[N]; bool visited[N]; int n,a,b; int Begin; int i,j; while(scanf("%d %d %d", &n, &a, &b) != EOF) { for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) { scanf("%d", &map[i][j]); } } for(i = 0; i < n; ++i) { int index = 0; map[i][0] = 1; for(j = 0; j < n; ++j) { if(map[i][j] && i != j) map[i][++index] = j;//记录下可达的点 } map[i][++index] = -1;//-1结束 } //深搜 memset(visited, 0, N * sizeof(bool)); int top = -1; int ans = 0; stack[++top] = a;//a入栈 visited[a] = true; while(top > -1)//栈不空 { Begin = stack[top];//取栈顶元素 for(i = map[Begin][0]; map[Begin][i] != -1; ++i) { if(map[Begin][i] != b && !visited[map[Begin][i]])//未访问过 { stack[++top] = map[Begin][i]; visited[map[Begin][i]] = true; map[Begin][0] = i + 1; break; } if(map[Begin][i] == b) ++ans; } if(map[Begin][i] == -1)//这个点的所有路径都走过了,退栈 { --top; } }// end for while printf("%d\n", ans); } return 0; }贴出三道题的题目图片: