例 5.1.3 周期串
复制代码
1
2fgets(str, MAX, stdin); scanf("%s",str);
fgets()函数执行后,str会读到‘n’符号停下,也就是提取一行,‘n’也被读进字串,因此strlen(str)会比原本输入的字符串多一个;除了一种很特殊的情况,读入一行时,只一行并不是以’n‘结束,而是以EOF结束。
scanf()遇到’n‘,space,tab就会停下,而且这些不会被读入字串,所以strlen的长度即为输入字串的长度。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <stdio.h> #include <string.h> #define MAX 200 int main() { char str[MAX]; int period, n, ok, j; scanf("%s",str); n = strlen(str); printf("%d",n); for(period = 1; period <= n; period++) if(n % period == 0) { ok = 1;<span style="white-space:pre"> </span>//第一次忘了在这里初始化 for(j = period; j < n; j++) { if(str[j] != str[j % period]) {ok = 0;break;} } if(ok) break; } printf("%dn",period); }
5.2.2 阶乘的精确值
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <stdio.h> #include <string.h> #define MAX 3000 int arr[MAX]; int main() { int c , n, i, k, j; scanf("%d", &n); arr[0] = 1; for(j = 2; j <= n; j++) { c = 0; for(i = 0; i < MAX; i++) { k = arr[i] * j + c; arr[i] = k % 10; c = k / 10; } } for(i = MAX; i > -1; i--) if(arr[i]) break; for(i = j; i >= 0; i--) printf("%d", arr[i]); return 0; }
5.3.1 6174问题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55#include <stdio.h> #include <string.h> #define MAX 3000 int main() { int t, n, i = 0, a[10], j, found = 0, min, max; int answer[100], k = 0; scanf("%d", &n); answer[k++] = n; printf("%d ", n); while(found == 0) { min = 0; max = 0;i = 0; while(n != 0) { a[i++] = n % 10; n /= 10; } bubblesort(a, 4); int n1 = 0, n2 = 0; for(i = 0; i < 4; i++) { n1 = n1 * 10 + a[i]; n2 = n2 * 10 + a[3 - i]; } n = n2 - n1; for(i = 0; i < k; i++) if(n == answer[i]) {found = 1;break;} answer[k++] = n; printf("-> %d ", n); } return 0; } void bubblesort(int a[], int n) { int swap = 1, j = 0, i, temp; while(swap) { swap = 0; j++; for(i = 0; i < n - j; i++) if(a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; swap = 1; } } }
5.3.2 字母重排
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <stdio.h> #include <string.h> #define MAX 3000 char word[1000][10], sorted[1000][10]; int cmp_char(const void* _a, const void* _b) { char* a = (char*)_a; char* b = (char*)_b; return *a - *b; } int cmp_string(const void* _a, const void* _b) { char* a = (char*)_a; char* b = (char*)_b; return strcmp(a, b); } int main() { int n = 0, i; while(1) { scanf("%s",word[n]); if(word[n][0] == '*') break; n++; } qsort(word, n, sizeof(word[0]), cmp_string); for(i = 0; i < n; i++) { strcpy(sorted[i], word[i]); qsort(sorted[i], strlen(sorted[i]),sizeof(char),cmp_char); } char s[10]; while(scanf("%s",s) == 1) { qsort(s, strlen(s), sizeof(char), cmp_char); int found = 0; for(i = 0; i < n; i++) { if(strcmp(s, sorted[i]) == 0) {printf("%s ", word[i]);found = 1;} } if(found == 0) printf(":("); printf("n"); } return 0; }
summary:
复制代码
1
2void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
-
base -- This is the pointer to the first element of the array to be sorted.
-
nitems -- This is the number of elements in the array pointed by base.
-
size -- This is the size in bytes of each element in the array.
-
compar -- This is the function that compares two elements.
最后
以上就是细心学姐最近收集整理的关于算法竞赛入门经典第五章的全部内容,更多相关算法竞赛入门经典第五章内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复