|
【冒泡排序法】 #include <stdio.h> #include <stdlib.h>
int main() { int i, j, n, temp, *nums; scanf("%d", &n); nums = (int *)calloc(n, sizeof(int)); for (i = 0; i < n; i++) scanf("%d", nums + i); for (i = 1; i <= n - 1; i++) // 大循環:[1, n-1],共比較n-1輪,特別注意這裡是1開頭 { for (j = 0; j < n - i; j++) // 小循環:[0, n-i),每輪進行n-i次 { if (nums[j] < nums[j + 1]) // 這裡寫<是從大到小, >是從小到大 { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } for (i = 0; i < n; i++) printf("%d ", nums[i]); putchar('\n'); free(nums); return 0; }
|
|
#include <stdio.h> #define N 10 void fun(int *nums) { int cnt, num; for (cnt = 0, num = 0; cnt < 10; num++) { if (num % 2 == 1 && num % 3 == 1 && num % 5 == 1) { nums[cnt] = num; cnt++; } } } int main() { int i; int nums[N]; FILE *fp; if ((fp = fopen("tmp.txt", "w")) == NULL) { printf("文件打不開!\n"); return 1; } fun(nums); for (i = 0; i < N; i++) { printf("%d ", nums[i]); fprintf(fp, "%d ", nums[i]); } fclose(fp); return 0; } /*void fun(int *nums) { int i, num = 0; for (i = 0; i < N; num++) { if (num % 2 == 1 && num % 3 == 1 && num % 5 == 1) { nums[i] = num; i++; } } }*/ 编写中出现的错误:3处 以后练习时要尽量做到写一次就编译通过且正常运行。因为考试时根本无法在电脑上实际调试。
|
|
【冒泡排序法复习】 #include <stdio.h> #include <stdlib.h>
int main() { int *nums, n, i, j, temp; scanf("%d", &n); nums = (int *)calloc(n, sizeof(int)); for (i = 0; i < n; i++) scanf("%d", nums + i); for (i = 1; i <= n - 1; i++) { for (j = 0; j < n - i; j++) { if (nums[j] > nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } for (i = 0; i < n; i++) printf("%d ", nums[i]); free(nums); return 0; }
|
|
【冒泡排序法复习】 #include <stdio.h> #include <stdlib.h> int main() {...
错误1:最后忘了putchar('\n')!!!!!!!!!! 错误2:之前忘了 int *nums, n, i, j, temp;最后的 分号
|
|
#include <stdio.h>
int main() { char s1[20] = "成都"; char *s2 = "大学"; int i; for (i = 0; s1[i] != '\0'; i++); while (*s2 != '\0') s1[i++] = *s2++; s1[i] = '\0'; puts(s1); return 0; } 存在的错误:忘了最后的s1[i] = '\0';
|
|
Both *p++ and (*p)++ return the current value of *p. But *p++ increases the pointer while (*p)++ makes the value incremented.
|
|
#include <stdio.h>
int main() { int a[3][4]; int b[4][3]; int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { scanf("%d", *(a + i) + j); b[j][i] = a[i][j]; } } for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) printf("%4d", b[i][j]); putchar('\n'); } return 0; }
|
|
#include <stdio.h> #include <string.h>
int main() { char strs[10][100]; int i; int max_i = 0; for (i = 0; i < 10; i++) gets(strs[i]); for (i = 1; i < 10; i++) { if (strcmp(strs[max_i], strs[i]) < 0) max_i = i; } printf("最大的串是:%s\n", strs[max_i]); return 0; }
|
|
#include <stdio.h>
int main() { int nums[3][4]; int i, j; for (i = 0; i < 2; i++) for (j = 0; j < 4; j++) scanf("%d", &nums[i][j]); for (j = 0; j < 4; j++) nums[2][j] = nums[0][j] + nums[1][j]; for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) printf("%4d", nums[i][j]); putchar('\n'); } return 0; }
|
|
#include <stdio.h> #include <string.h>
int main() { char str[21]; int i, len; int flag = 0; gets(str); len = strlen(str); for (i = 0; i < len - i - 1; i++) { if (str[i] != str[len - i - 1]) { flag = 1; break; } } if (flag == 1) puts("不構成回文\n"); else puts("構成回文\n"); return 0; }
|