当前位置:   article > 正文

华为机试 108 题(C 语言解答)_华为机考 c卷

华为机考 c卷

Nowcoder题库链接:华为机试

  • HJ1 字符串最后一个单词的长度(字符串)

  1. 输入:hello nowcoder
  2. 输出:8
  3. 说明:
  4. 最后一个单词为nowcoder,长度为8

示例代码: HJ1.c

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #define LEN 5001
  5. char * s_gets(char * st, int n);
  6. int get_length(char * st); // 获取字符串最后一个单词长度
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. printf("%d", get_length(str));
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. int get_length(char * st)
  31. {
  32. int count = 0;
  33. while (*st)
  34. {
  35. if (isspace(*st))
  36. count = 0;
  37. else
  38. count++;
  39. st++;
  40. }
  41. return count;
  42. }

  • HJ2 计算某字符出现次数(字符串)

  1. 输入:ABCabc
  2. A
  3. 输出:2

示例代码:HJ2.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 1001
  5. char * s_gets(char * st, int n);
  6. int appear(char * st, int ch);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. char ch;
  11. if (s_gets(str, LEN) != NULL && *str != '\0')
  12. {
  13. scanf("%c", &ch);
  14. printf("%d", appear(str, ch));
  15. }
  16. return 0;
  17. }
  18. char * s_gets(char * st, int n)
  19. {
  20. char * ret_val;
  21. char * find;
  22. ret_val = fgets(st, n, stdin);
  23. if (ret_val)
  24. {
  25. find = strchr(st, '\n');
  26. if (find)
  27. *find = '\0';
  28. else
  29. while (getchar() != '\n')
  30. continue;
  31. }
  32. return ret_val;
  33. }
  34. int appear(char * st, int ch)
  35. {
  36. int count = 0;
  37. int s;
  38. if (isupper(ch))
  39. s = tolower(ch);
  40. else
  41. s = toupper(ch);
  42. while (*st)
  43. {
  44. if (*st == s || *st == ch)
  45. count++;
  46. st++;
  47. }
  48. return count;
  49. }

  • HJ3 明明的随机数(基础数学)

  1. 输入:
  2. 3
  3. 2
  4. 2
  5. 1
  6. 输出:
  7. 1
  8. 2
  9. 说明:输入解释:
  10. 第一个数字是3,也即这个小样例的N=3,说明用计算机生成了31500之间的随机整数,接下来每行一个随机数字,共3行,也即这3个随机数字为:
  11. 2
  12. 2
  13. 1
  14. 所以样例的输出为:
  15. 1
  16. 2

示例代码:HJ3.c

  1. #include <stdio.h>
  2. #define LEN 501
  3. int main(void)
  4. {
  5. int n;
  6. int i;
  7. static int mark[LEN];
  8. int num;
  9. while (scanf("%d", &n) == 1)
  10. {
  11. for (i = 0; i < n; i++)
  12. {
  13. scanf("%d", &num);
  14. mark[num] = 1;
  15. }
  16. for (i = 1; i < LEN; i++)
  17. {
  18. if (mark[i])
  19. printf("%d\n", i);
  20. }
  21. for (i = 1; i < LEN; i++)
  22. mark[i] = 0;
  23. }
  24. return 0;
  25. }

  • HJ4 字符串分隔(字符串)

  1. 输入:abc
  2. 输出:abc00000

示例代码:HJ4.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 101
  4. #define SIZE 8
  5. char * s_gets(char * st, int n);
  6. void get_det(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. get_det(str);
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. void get_det(char * st)
  31. {
  32. while (*st)
  33. {
  34. int i;
  35. if ((int) strlen(st) < SIZE)
  36. {
  37. printf("%s", st);
  38. for (i = 0; i < SIZE - (int) strlen(st); i++)
  39. putchar('0');
  40. putchar('\n');
  41. break;
  42. }
  43. for (i = 0; i < SIZE; i++, st++)
  44. putchar(*st);
  45. putchar('\n');
  46. }
  47. }

  • HJ5 进制转换(字符串)

  1. 输入:0xAA
  2. 输出:170

示例代码:HJ5.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 21
  5. char * s_gets(char * st, int n);
  6. void get_dec(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. while (s_gets(str, LEN) != NULL && *str != '\0')
  11. get_dec(str);
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. void get_dec(char * st)
  31. {
  32. int i;
  33. int num = 0;
  34. char str[LEN];
  35. int temp;
  36. for (i = 2; i < (int) strlen(st); i++)
  37. {
  38. if (isdigit(st[i]))
  39. temp = st[i] - '0';
  40. else
  41. temp = st[i] - 'A' + 10;
  42. num = num * 16 + temp;
  43. }
  44. sprintf(str, "%d", num);
  45. puts(str);
  46. }

  • HJ6 质数因子(基础数学)

  1. 输入:180
  2. 输出:2 2 3 3 5

示例代码:HJ6.c

  1. #include <stdio.h>
  2. void get_div(long n);
  3. int main(void)
  4. {
  5. long int num;
  6. scanf("%ld", &num);
  7. get_div(num);
  8. return 0;
  9. }
  10. void get_div(long n)
  11. {
  12. int i;
  13. for (i = 2; i * i <= n; i++)
  14. {
  15. while (n % i == 0)
  16. {
  17. printf("%d ", i);
  18. n /= i;
  19. }
  20. }
  21. if (n - 1) // 说明只能被本身整除,又由于 1 不是质数,故需要大于 1
  22. printf("%ld\n", n);
  23. }

  • HJ7 取近似值(基础数学)

  1. 输入:5.5
  2. 输出:6
  3. 说明:
  4. 0.5>=0.5,所以5.5需要向上取整为6

示例代码:HJ7.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. double num;
  5. scanf("%lf", &num);
  6. printf("%d", (int) (num + 0.5));
  7. return 0;
  8. }

  • HJ8 合并表记录(结构体)

  1. 输入:
  2. 4
  3. 0 1
  4. 0 2
  5. 1 2
  6. 3 4
  7. 输出:
  8. 0 3
  9. 1 2
  10. 3 4

示例代码:HJ8.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. typedef struct list {
  5. int x;
  6. int y;
  7. bool marked;
  8. } List;
  9. void get_item(List * pl, int n);
  10. void com_item(List * pl, int n);
  11. void show_item(List * pl, int n);
  12. void order_item(List * pl, int n);
  13. int main(void)
  14. {
  15. int n;
  16. List * pl;
  17. scanf("%d", &n);
  18. pl = (List *) malloc(n * sizeof(List));
  19. get_item(pl, n);
  20. com_item(pl, n);
  21. order_item(pl, n);
  22. show_item(pl, n);
  23. free(pl);
  24. return 0;
  25. }
  26. void get_item(List * pl, int n)
  27. {
  28. int i;
  29. for (i = 0; i < n; i++)
  30. {
  31. scanf("%d%d", &(pl + i)->x, &(pl + i)->y);
  32. (pl + i)->marked = false;
  33. }
  34. }
  35. void com_item(List * pl, int n)
  36. {
  37. int i, j;
  38. for (i = 0; i < n - 1; i++)
  39. {
  40. for (j = i + 1; j < n; j++)
  41. {
  42. if ((pl + i)->marked == true)
  43. break;
  44. if ((pl + i)->x == (pl + j)->x && (pl + j)->marked == false)
  45. {
  46. (pl + i)->y += (pl + j)->y;
  47. (pl + j)->marked = true;
  48. }
  49. }
  50. }
  51. }
  52. void show_item(List * pl, int n)
  53. {
  54. int i;
  55. for (i = 0; i < n; i++)
  56. {
  57. if ((pl + i)->marked == false)
  58. printf("%d %d\n", (pl + i)->x, (pl + i)->y);
  59. }
  60. }
  61. void order_item(List * pl, int n)
  62. {
  63. int i, j;
  64. int temp;
  65. for (i = 0; i < n - 1; i++)
  66. {
  67. for (j = i + 1; j < n; j++)
  68. {
  69. if ((pl + i)->x > (pl + j)->x && (pl + j)->marked == false && (pl + i)->marked == false)
  70. {
  71. temp = (pl + i)->x;
  72. (pl + i)->x = (pl + j)->x;
  73. (pl + j)->x = temp;
  74. temp = (pl + i)->y;
  75. (pl + i)->y = (pl + j)->y;
  76. (pl + j)->y = temp;
  77. }
  78. }
  79. }
  80. }

  • HJ9 提取不重复的整数(字符串)

  1. 输入:9876673
  2. 输出:37689

示例代码:HJ9.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 100
  4. int no_repeat(int n);
  5. int main(void)
  6. {
  7. int num;
  8. scanf("%d", &num);
  9. printf("%d", no_repeat(num));
  10. return 0;
  11. }
  12. int no_repeat(int n)
  13. {
  14. static char str[LEN], str2[LEN];
  15. int i, j;
  16. int number = 0, count = 0;
  17. int flag;
  18. sprintf(str, "%d", n);
  19. for (i = (int) strlen(str) - 1; i > -1; i--)
  20. {
  21. flag = 0;
  22. for (j = 0; j < (int) strlen(str2); j++)
  23. {
  24. if (*(str + i) == *(str2 + j))
  25. {
  26. flag = 1;
  27. break;
  28. }
  29. }
  30. if (!flag)
  31. {
  32. *(str2 + strlen(str2)) = *(str + i);
  33. count++;
  34. }
  35. }
  36. for (i = 0; i < count; i++)
  37. number = number * 10 + str2[i] - '0';
  38. return number;
  39. }

  • HJ10 字符个数统计(字符串)

  1. 输入:abc
  2. 输出:3

示例代码:HJ10.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. #define TYPE 128
  5. char * s_gets(char * st, int n);
  6. int get_count(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. printf("%d", get_count(str));
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. int get_count(char * st)
  31. {
  32. int count = 0;
  33. static int ascii[TYPE];
  34. int i;
  35. int flag;
  36. while (*st)
  37. {
  38. flag = 0;
  39. for (i = 0; i < count; i++)
  40. {
  41. if (*st == ascii[i])
  42. {
  43. st++;
  44. flag = 1;
  45. break;
  46. }
  47. }
  48. if (flag == 0)
  49. ascii[count++] = *st;
  50. }
  51. return count;
  52. }

  • HJ11 数字颠倒(字符串)

  1. 输入:1516000
  2. 输出:0006151

示例代码:HJ11.c

  1. #include <stdio.h>
  2. #include <string.h> // 提供 strlen() 函数原型
  3. #define LEN 20
  4. int main(void)
  5. {
  6. int num; // 待输入的数字
  7. char str[LEN]; // 保存转换后的字符串
  8. int i;
  9. scanf("%d", &num);
  10. sprintf(str, "%d", num);
  11. for (i = strlen(str) - 1; i > -1; i--)
  12. putchar(*(str + i));
  13. return 0;
  14. }

  • HJ12 字符串反转(字符串)

  1. 输入:abcd
  2. 输出:dcba

示例代码:HJ12.c

  1. #include <stdio.h>
  2. #include <string.h> // 提供 strlen()、strchr() 函数
  3. #define LEN 1001 // 最多1000个字符包括1001个字符,多余一个给空字符
  4. char * s_gets(char *, int n); // 获取最多 n - 1 个字符串
  5. int main(void)
  6. {
  7. char str[LEN];
  8. int i;
  9. if (s_gets(str, LEN) != NULL && *str != '\0')
  10. {
  11. for (i = strlen(str) - 1; i >= 0 ; i--)
  12. putchar(*(str + i));
  13. }
  14. return 0;
  15. }
  16. char * s_gets(char * st, int n)
  17. {
  18. char * ret_val;
  19. char * find;
  20. ret_val = fgets(st, n, stdin);
  21. if (ret_val)
  22. {
  23. find = strchr(st, '\n');
  24. if (find)
  25. *find = '\0';
  26. else
  27. while (getchar() != '\n')
  28. continue;
  29. }
  30. return ret_val;
  31. }

  • HJ13 句子逆序(字符串)

  1. 输入:I am a boy
  2. 输出:boy a am I

示例代码:HJ13.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. char * s_gets(char * st, int n);
  5. void reverse(char * st);
  6. int main(void)
  7. {
  8. char str[LEN];
  9. if (s_gets(str, LEN) != NULL && *str != '\0')
  10. reverse(str);
  11. return 0;
  12. }
  13. char * s_gets(char * st, int n)
  14. {
  15. char * ret_val;
  16. char * find;
  17. ret_val = fgets(st, n, stdin);
  18. if (ret_val)
  19. {
  20. find = strchr(st, '\n');
  21. if (find)
  22. *find = '\0';
  23. else
  24. while (getchar() != '\n')
  25. continue;
  26. }
  27. return ret_val;
  28. }
  29. void reverse(char * st)
  30. {
  31. int i;
  32. for (i = (int) strlen(st) - 1; i > -1; i--)
  33. if (*(st + i) == ' ')
  34. {
  35. printf("%s ", st + i + 1);
  36. *(st + i) = '\0';
  37. }
  38. else if (i == 0)
  39. puts(st);
  40. }

  • HJ14 字符串排序(字符串)

  1. 输入:
  2. 9
  3. cap
  4. to
  5. cat
  6. card
  7. two
  8. too
  9. up
  10. boat
  11. boot
  12. 输出:
  13. boat
  14. boot
  15. cap
  16. card
  17. cat
  18. to
  19. too
  20. two
  21. up

示例代码:HJ14.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define LEN 101
  5. void order_str(char **, int n);
  6. int main(void)
  7. {
  8. int n;
  9. int i;
  10. char temp[LEN];
  11. scanf("%d", &n);
  12. char * str[n];
  13. for (i = 0; i < n; i++)
  14. {
  15. scanf("%s", temp);
  16. str[i] = (char *) malloc(((int) strlen(temp) + 1) * sizeof(char));
  17. strcpy(str[i], temp);
  18. }
  19. order_str(str, n);
  20. for (i = 0; i < n; i++) // 怎么申请,怎么释放
  21. free(str[i]);
  22. return 0;
  23. }
  24. void order_str(char ** str, int n)
  25. {
  26. int i, j;
  27. char * temp;
  28. for (i = 0; i < n - 1; i++)
  29. {
  30. for (j = i + 1; j < n; j++)
  31. {
  32. if (strcmp(str[i], str[j]) > 0)
  33. {
  34. temp = str[i];
  35. str[i] = str[j];
  36. str[j] = temp;
  37. }
  38. }
  39. }
  40. for (i = 0; i < n; i++)
  41. puts(str[i]);
  42. }

  • HJ15 求int型正整数在内存中存储时1的个数(位操作)

  1. 输入:5
  2. 输出:2

示例代码:HJ15.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int count = 0;
  5. int num;
  6. scanf("%d", &num);
  7. while (num)
  8. {
  9. count += (num & 1);
  10. num >>= 1;
  11. }
  12. printf("%d", count);
  13. return 0;
  14. }

  • HJ16 购物单(动态规划,背包问题)

  1. 输入:
  2. 50 5
  3. 20 3 5
  4. 20 3 5
  5. 10 3 0
  6. 10 2 0
  7. 10 1 0
  8. 输出:
  9. 130
  10. 说明:
  11. 由第 1 行可知总钱数 N 为 50 以及希望购买的物品个数 m 为 5
  12. 2 和第 3 行的 q 为 5,说明它们都是编号为 5 的物品的附件;
  13. 4 ~ 6 行的 q 都为 0,说明它们都是主件,它们的编号依次为 3 ~ 5
  14. 所以物品的价格与重要度乘积的总和的最大值为 10 * 1 + 20 * 3 + 20 * 3 = 130

示例代码:HJ16.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 60 // 最多 60 件物品
  4. #define M 32000 // 最多预算数
  5. #define MAX(a, b) ((a) > (b)) ? (a) : (b) // 宏函数参数最好加括号避免出错
  6. int main(void)
  7. {
  8. int money; // 预算
  9. int n; // 货物数量
  10. int v; // 单个物品价格
  11. int p; // 单个物品满意度
  12. int q; // 物品所属编号
  13. static int price[N][4]; // 存储可能的四种组合的商品的价格,静态变量自动初始化为 0
  14. static int weight[N][4]; // 存储可能出现的四种组合的满意度
  15. int merge_price[N][4]; // 存储合并后的价格
  16. int merge_weight[N][4]; // 存储合并后的满意度
  17. int count = 0; // 记录组合商品数量
  18. static int dp[N][M]; // M 数量的金钱在购买前 N 个已经出现物品中采取的所有方案中所能最多获取的满意度
  19. int max = 0; // 存储最大满意度
  20. int i, j, k;
  21. while (scanf("%d%d", &money, &n) == 2)
  22. {
  23. /* 数据处理开始,录入并合并数据*/
  24. for (i = 0; i < n; i++)
  25. {
  26. scanf("%d%d%d", &v, &p, &q);
  27. if (0 == q)
  28. {
  29. price[i][0] = v;
  30. weight[i][0] = v * p;
  31. }
  32. else if (0 == price[q - 1][1]) // 表明是数组编号为 q - 1第 1 件附件
  33. {
  34. price[q - 1][1] = v;
  35. weight[q - 1][1] = v * p;
  36. }
  37. else // 表明是数组编号为 q - 1的物品的第 2 件附件
  38. {
  39. price[q - 1][2] = v;
  40. weight[q - 1][2] = v * p;
  41. price[q - 1][3] = price[q - 1][1] + v;
  42. weight[q - 1][3] = weight[q - 1][1] + v * p;
  43. }
  44. }
  45. // 合并可能出现的商品的价格和满意度
  46. for (i = 0; i < n; i++)
  47. {
  48. if (price[i][0]) // 该商品是主件
  49. {
  50. for (j = 1; j < 4; j++) // 只能购买包含主件的商品,合并可能购买的商品价格
  51. {
  52. price[i][j] += price[i][0];
  53. weight[i][j] += weight[i][0];
  54. }
  55. for (j = 0; j < 4; j++) // 剔除为 0 的数据
  56. {
  57. merge_price[count][j] = price[i][j];
  58. merge_weight[count][j] = weight[i][j];
  59. }
  60. count++;
  61. }
  62. }
  63. /* 数据处理完成 ,动态规划开始 */
  64. for (i = 1; i <= count; i++) // 对于 1 ~ count 件物品
  65. {
  66. for (j = 10; j <= money; j += 10)
  67. {
  68. max = dp[i - 1][j]; // 数量 j 的金钱购买前 i - 1 件已经出现物品最多获得满意度
  69. /* 开始尝试购买才出现的第 i 件物品(数组下标为 i - 1) */
  70. for (k = 0; k < 4; k++) // 购买必须包含主件的商品之一,取最大值
  71. {
  72. if (!merge_price[i - 1][k]) // 不存在该组合
  73. break;
  74. if (j - merge_price[i - 1][k] >= 0) // 如果买得起某个组合,比较这笔钱 没花之前的所能购买到的最大满意度加上购买该组合新增的满意度之和 和 不购买该组合之前所得满意度的大小
  75. max = MAX(max, dp[i - 1][j - merge_price[i - 1][k]] + merge_weight[i - 1][k]); // dp[i - 1] 指的是已经出现的前 i - 1 个物品的最大满意度,不是第 i 个,回退时金钱和商品编号都要减
  76. }
  77. dp[i][j] = max; // 买不起或值会变小则不变,买得起且满意度增加则改变
  78. /* 获得尝试购买后的最大满意度,尝试结束 */
  79. }
  80. }
  81. printf("%d\n", dp[count][money]);
  82. }
  83. return 0;
  84. }

  • HJ17 坐标移动(字符串)

  1. 输入:A10;S20;W10;D30;X;A1A;B10A11;;A10;
  2. 输出:10,-10

示例代码:HJ17.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #define LEN 10001
  6. void get_cordinate(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. while (scanf("%s", str) == 1)
  11. get_cordinate(str);
  12. return 0;
  13. }
  14. void get_cordinate(char * st)
  15. {
  16. bool in_com = false;
  17. char str[4] = "WSAD";
  18. char order_1;
  19. int order_2 = 0;
  20. int x = 0, y = 0;
  21. char * st_b = st;
  22. while (*st)
  23. {
  24. if (strchr(str, *st) && in_com == false && (st == st_b || *(st - 1) == ';'))
  25. {
  26. order_1 = *st;
  27. in_com = true;
  28. }
  29. else if (in_com && isdigit(*st))
  30. {
  31. order_2 = order_2 * 10 + *st - '0';
  32. }
  33. else if (in_com && !isdigit(*st) && *st != ';')
  34. {
  35. in_com = false;
  36. order_2 = 0;
  37. }
  38. if (in_com && *st == ';')
  39. {
  40. switch (order_1)
  41. {
  42. case 'W':
  43. y += order_2;
  44. break;
  45. case 'S':
  46. y -= order_2;
  47. break;
  48. case 'A':
  49. x -= order_2;
  50. break;
  51. case 'D':
  52. x += order_2;
  53. break;
  54. }
  55. in_com = false;
  56. order_2 = 0;
  57. }
  58. st++;
  59. }
  60. printf("%d,%d\n", x, y);
  61. }

  • HJ18 识别有效的IP地址和掩码并进行分类统计(字符串)

  1. 输入:
  2. 10.70.44.68~255.254.255.0
  3. 1.0.0.1~255.0.0.0
  4. 192.168.0.2~255.255.255.0
  5. 19..0.~255.255.255.0
  6. 输出:
  7. 1 0 1 0 0 2 1
  8. 说明:
  9. 10.70.44.68~255.254.255.0的子网掩码非法,19..0.~255.255.255.0的IP地址非法,所以错误IP地址或错误掩码的计数为2
  10. 1.0.0.1~255.0.0.0是无误的A类地址;
  11. 192.168.0.2~255.255.255.0是无误的C类地址且是私有IP;
  12. 所以最终的结果为1 0 1 0 0 2 1

示例代码:HJ18.c

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #define LEN 101
  7. bool IsLegal_Ip(char * st);
  8. bool IsLegal_Mask(char * st);
  9. int main(void)
  10. {
  11. char str[LEN];
  12. char * ip;
  13. char * mask;
  14. char * ip_1;
  15. char * ip_2;
  16. int ip1;
  17. int ip2;
  18. int a_count = 0;
  19. int b_count = 0;
  20. int c_count = 0;
  21. int d_count = 0;
  22. int e_count = 0;
  23. int w_count = 0;
  24. int p_count = 0;
  25. while (scanf("%s", str) == 1)
  26. {
  27. ip = strtok(str, "~");
  28. mask = strtok(NULL, "~");
  29. if (IsLegal_Mask(mask) && IsLegal_Ip(ip))
  30. {
  31. ip_1 = strtok(ip, ".");
  32. ip_2 = strtok(NULL, ".");
  33. ip1 = atoi(ip_1);
  34. ip2 = atoi(ip_2);
  35. if (!(ip1 == 0 || ip1 == 127))
  36. {
  37. if (ip1 > 0 && ip1 < 127)
  38. {
  39. a_count++;
  40. if (ip1 == 10)
  41. p_count++;
  42. }
  43. if (ip1 > 127 && ip1 <= 191)
  44. {
  45. b_count++;
  46. if (ip1 == 172 && ip2 >= 16 && ip2 <= 31)
  47. p_count++;
  48. }
  49. if (ip1 > 191 && ip1 <= 223)
  50. {
  51. c_count++;
  52. if (ip1 == 192 && ip2 == 168)
  53. p_count++;
  54. }
  55. if (ip1 > 223 && ip1 <= 239)
  56. d_count++;
  57. if (ip1 > 239 && ip1 <= 255)
  58. e_count++;
  59. }
  60. }
  61. else
  62. {
  63. ip_1 = strtok(ip, ".");
  64. ip1 = atoi(ip_1);
  65. if (ip1 != 127 && ip1 != 0)
  66. w_count++;
  67. }
  68. }
  69. printf("%d %d %d %d %d %d %d", a_count, b_count, c_count,
  70. d_count, e_count, w_count, p_count);
  71. return 0;
  72. }
  73. bool IsLegal_Ip(char * st)
  74. {
  75. char * st_b = st;
  76. int num_count = 0;
  77. int dot_count = 0;
  78. int num = 0;
  79. while (*st)
  80. {
  81. if ((st == st_b && !(*st >= '0' && *st <= '9'))
  82. || (*st == '.' && !(*(st + 1) >= '0' && *(st + 1) <= '9')))
  83. return false;
  84. if (st == st_b && *st == '0' && isdigit(*(st + 1)))
  85. return false;
  86. if (*st == '.' && *(st + 1) == '0' && isdigit(*(st + 2)))
  87. return false;
  88. if (isdigit(*st))
  89. {
  90. num = 10 * num + *st - '0';
  91. if (!isdigit(*(st + 1)))
  92. num_count++;
  93. }
  94. if (*st == '.')
  95. {
  96. dot_count++;
  97. if (!(num >= 0 && num < 256))
  98. return false;
  99. num = 0;
  100. }
  101. st++;
  102. }
  103. if (!(num_count == 4 && dot_count == 3) || !(num >= 0 && num < 256))
  104. return false;
  105. return true;
  106. }
  107. bool IsLegal_Mask(char * st)
  108. {
  109. char * mask_part;
  110. int num;
  111. int i, j;
  112. bool count_zero = false;
  113. int count_one = 0;
  114. int temp;
  115. mask_part = strtok(st, ".");
  116. for (i = 0; i < 4; i++)
  117. {
  118. num = atoi(mask_part);
  119. if (num == 255)
  120. count_one++;
  121. if ((i == 0 && num == 0) || num < 0 || num > 255)
  122. return false;
  123. for (j = 0; j < 8; j++)
  124. {
  125. temp = (num >> (7 - j)) & 1;
  126. if (temp == 0)
  127. count_zero = true;
  128. if (temp == 1 && count_zero)
  129. return false;
  130. }
  131. if (i < 3)
  132. mask_part = strtok(NULL, ".");
  133. }
  134. if (count_one == 4)
  135. return false;
  136. return true;
  137. }

  • HJ19 简单错误记录(字符串)

  1. 输入:
  2. D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
  3. E:\je\rzuwnjvnuz 633
  4. C:\km\tgjwpb\gy\atl 637
  5. F:\weioj\hadd\connsh\rwyfvzsopsuiqjnr 647
  6. E:\ns\mfwj\wqkoki\eez 648
  7. D:\cfmwafhhgeyawnool 649
  8. E:\czt\opwip\osnll\c 637
  9. G:\nt\f 633
  10. F:\fop\ywzqaop 631
  11. F:\yay\jc\ywzqaop 631
  12. D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
  13. 输出:
  14. rzuwnjvnuz 633 1
  15. atl 637 1
  16. rwyfvzsopsuiqjnr 647 1
  17. eez 648 1
  18. fmwafhhgeyawnool 649 1
  19. c 637 1
  20. f 633 1
  21. ywzqaop 631 2
  22. 说明:
  23. 由于D:\cfmwafhhgeyawnool 649的文件名长度超过了16个字符,达到了17,所以第一个字符'c'应该被忽略。
  24. 记录F:\fop\ywzqaop 631和F:\yay\jc\ywzqaop 631由于文件名和行号相同,因此被视为同一个错误记录,哪怕它们的路径是不同的。
  25. 由于循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准,所以D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645不会被记录。

示例代码HJ19.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6. #define LEN 101
  7. char * s_gets(char * st, int n);
  8. int main(void)
  9. {
  10. char temp[LEN];
  11. char ** pwrong;
  12. int count = 0;
  13. char * start;
  14. char * delima;
  15. char * pos;
  16. char ** record;
  17. static int record_num[8];
  18. static int record_line[8];
  19. int record_n = 0;
  20. bool appear = false, appear2 = false;
  21. int len;
  22. int len_num;
  23. int num;
  24. int i;
  25. if ((pwrong = (char **) malloc(sizeof(char *) * (LEN - 1))) == NULL)
  26. puts("Wrong in memory allocation!");
  27. if ((record = (char **) malloc(sizeof(char *) * 8)) == NULL)
  28. puts("Wrong in memory allocation!");
  29. while (s_gets(temp, LEN) != NULL && *temp != '\0')
  30. {
  31. len_num = 0;
  32. pwrong[count] = (char *) malloc(sizeof(char) * ((int) sizeof(temp) + 1));
  33. strcpy(pwrong[count], temp);
  34. len = strlen(temp);
  35. delima = temp;
  36. while (strtok(delima, "\\") != NULL)
  37. delima = delima + (int) strlen(delima) + 1;
  38. for (i = len - 1; i > - 1; i--)
  39. {
  40. if (isdigit(temp[i]))
  41. len_num++;
  42. if (temp[i] == '\0')
  43. {
  44. start = temp + i + 1;
  45. break;
  46. }
  47. }
  48. len = strlen(start);
  49. delima = strtok(start, " ");
  50. delima = strtok(NULL, " ");
  51. num = atoi(delima);
  52. if (len - len_num - 1 <= 16)
  53. start = pwrong[count] + i + 1;
  54. else
  55. start = pwrong[count] + i + 1 + len - len_num - 1 - 16;
  56. pos = pwrong[count] + i + 1;
  57. for (i = 0; i < count; i++)
  58. {
  59. if (strstr(pwrong[i], pos) != NULL)
  60. {
  61. appear = true;
  62. break;
  63. }
  64. }
  65. for (i = 0; i < record_n; i++)
  66. {
  67. if (strcmp(record[i], start) == 0 && record_line[i] == num)
  68. {
  69. appear2 = true;
  70. record_num[i]++;
  71. break;
  72. }
  73. }
  74. if (!appear && !appear2)
  75. {
  76. if (record_n < 8)
  77. {
  78. record_num[record_n] = 1;
  79. record_line[record_n] = num;
  80. record[record_n++] =start;
  81. }
  82. else
  83. {
  84. for (i = 0; i < 7; i++)
  85. {
  86. record[i] = record[i + 1];
  87. record_num[i] = record_num[i + 1];
  88. record_line[i] = record_line[i + 1];
  89. }
  90. record[7] = start;
  91. record_num[7] = 1;
  92. record_line[7] = num;
  93. }
  94. }
  95. appear = appear2 = false;
  96. count++;
  97. }
  98. for (i = 0; i < record_n; i++)
  99. printf("%s %d\n", record[i], record_num[i]);
  100. for (i = 0; i < count; i++)
  101. free(pwrong[i]);
  102. free(pwrong);
  103. free(record);
  104. return 0;
  105. }
  106. char * s_gets(char * st, int n)
  107. {
  108. char * ret_val;
  109. char * find;
  110. ret_val = fgets(st, n, stdin);
  111. if (ret_val)
  112. {
  113. find = strchr(st, '\n');
  114. if (find)
  115. *find = '\0';
  116. else
  117. while (getchar() != '\n')
  118. continue;
  119. }
  120. return ret_val;
  121. }

  • HJ20 密码验证合格程序(字符串)

  1. 输入:
  2. 021Abc9000
  3. 021Abc9Abc1
  4. 021ABC9000
  5. 021$bc9000
  6. 输出:
  7. OK
  8. NG
  9. NG
  10. OK

示例代码:HJ20.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #define LEN 101
  6. char * s_gets(char * st, int n);
  7. void cut_str(char * dest, char * start, char * end);
  8. bool is_include(char * st_1, char * st_2);
  9. int get_max(char * st_1, char * st_2);
  10. bool is_good(char * str);
  11. int main(void)
  12. {
  13. char str[LEN];
  14. while (s_gets(str, LEN) != NULL && *str != '\0')
  15. printf("%s\n", is_good(str) ? "OK" : "NG");
  16. return 0;
  17. }
  18. char * s_gets(char * st, int n)
  19. {
  20. char * ret_val;
  21. char * find;
  22. ret_val = fgets(st, n, stdin);
  23. if (ret_val)
  24. {
  25. find = strchr(st, '\n');
  26. if (find)
  27. *find = '\0';
  28. else
  29. while (getchar() != '\n')
  30. continue;
  31. }
  32. return ret_val;
  33. }
  34. void cut_str(char * dest, char * start, char * end)
  35. {
  36. int i;
  37. for (i = 0; i <= end - start; i++)
  38. dest[i] = *(start + i);
  39. dest[i] = '\0';
  40. }
  41. bool is_include(char * st_1, char * st_2)
  42. {
  43. int i, j;
  44. int len_1 = strlen(st_1);
  45. int len_2 = strlen(st_2);
  46. int count = 0;
  47. for (i = 0; i < len_1; i++)
  48. {
  49. for (j = count; j < len_2; j++)
  50. {
  51. if (st_2[j] == st_1[i])
  52. {
  53. count++;
  54. if (count == len_2)
  55. return true;
  56. break;
  57. }
  58. else
  59. {
  60. if (count > 0)
  61. {
  62. i-= count;
  63. count = 0;
  64. }
  65. break;
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. int get_max(char * st_1, char * st_2)
  72. {
  73. char dest[LEN];
  74. int len_2 = strlen(st_2);
  75. int j;
  76. int time;
  77. time = len_2 - 2;
  78. for (j = 0; j < time; j++)
  79. {
  80. cut_str(dest, st_2 + j, st_2 + j + 2);
  81. if (is_include(st_1 + 3 + j, dest))
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. bool is_good(char * str)
  87. {
  88. int len = strlen(str);
  89. int lo_ch = 0, up_ch = 0, num = 0, other = 0;
  90. int i;
  91. int count = 0;
  92. for (i = 0; i < len; i++)
  93. {
  94. if (islower(str[i]))
  95. lo_ch =1;
  96. else if (isupper(str[i]))
  97. up_ch = 1;
  98. else if (isdigit(str[i]))
  99. num = 1;
  100. else
  101. other = 1;
  102. }
  103. count += lo_ch + up_ch + num + other;
  104. if (len > 8 && !get_max(str, str) && count > 2)
  105. return true;
  106. return false;
  107. }

HJ21 简单密码(字符串)

  1. 输入:YUANzhi1987
  2. 输出:zvbo9441987

示例代码:HJ21.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 1001
  5. char * s_gets(char * st, int n);
  6. void lower_num(char * st);
  7. void upper_lower(char * st);
  8. int main(void)
  9. {
  10. char str[LEN];
  11. if (s_gets(str, LEN) != NULL && *str != '\0')
  12. {
  13. lower_num(str);
  14. upper_lower(str);
  15. }
  16. return 0;
  17. }
  18. char * s_gets(char * st, int n)
  19. {
  20. char * ret_val;
  21. char * find;
  22. ret_val = fgets(st, n, stdin);
  23. if (ret_val)
  24. {
  25. find = strchr(st, '\n');
  26. if (find)
  27. *find = '\0';
  28. else
  29. while (getchar() != '\n')
  30. continue;
  31. }
  32. return ret_val;
  33. }
  34. void lower_num(char * st)
  35. {
  36. char str[8][5] = { "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
  37. int i;
  38. while (*st)
  39. {
  40. for (i = 0; i < 8; i++)
  41. if (strchr(str[i], *st))
  42. *st = i + 2 + '0';
  43. st++;
  44. }
  45. }
  46. void upper_lower(char * st)
  47. {
  48. char * st_b = st;
  49. while (*st)
  50. {
  51. if (isupper(*st))
  52. {
  53. *st = tolower(*st);
  54. if (*st - 'a' == 25)
  55. *st = *st - 25;
  56. else
  57. *st = *st + 1;
  58. }
  59. st++;
  60. }
  61. puts(st_b);
  62. }

  • HJ22 汽水瓶(基础数学)

  1. 输入:
  2. 3
  3. 10
  4. 81
  5. 0
  6. 输出:
  7. 1
  8. 5
  9. 40
  10. 说明:
  11. 样例 1 解释:用三个空瓶换一瓶汽水,剩一个空瓶无法继续交换
  12. 样例 2 解释:用九个空瓶换三瓶汽水,剩四个空瓶再用三个空瓶换一瓶汽水,剩两个空瓶,向老板借一个空瓶再用三个空瓶换一瓶汽水喝完得一个空瓶还给老板

示例代码HJ22.c

  1. #include <stdio.h>
  2. #define N 3
  3. void get_water(int bottles);
  4. int main(void)
  5. {
  6. int num;
  7. while (scanf("%d", &num) == 1)
  8. get_water(num);
  9. return 0;
  10. }
  11. void get_water(int bottles)
  12. {
  13. int water = 0;
  14. while (bottles >= 3)
  15. {
  16. water += bottles / 3;
  17. bottles = bottles % 3 + bottles / 3;
  18. }
  19. if (bottles > 1)
  20. water++;
  21. if (water)
  22. printf("%d\n", water);
  23. }

  • HJ23 删除字符串中出现次数最少的字符(字符串)

  1. 输入:aabcddd
  2. 输出:aaddd

示例代码:HJ23.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 21
  5. char * s_gets(char * st, int n);
  6. void delete_mini(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. delete_mini(str);
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. void delete_mini(char * st)
  31. {
  32. int i, j;
  33. int len = strlen(st);
  34. static int stat[LEN];
  35. static char str[LEN];
  36. bool flag;
  37. int min;
  38. for (i = 0; i < len; i++)
  39. {
  40. flag = false;
  41. for (j = 0; j < (int) strlen(str); j++)
  42. {
  43. if (*(st + i) == *(str + j))
  44. {
  45. stat[j]++;
  46. flag = true;
  47. break;
  48. }
  49. }
  50. if (!flag)
  51. {
  52. stat[(int) strlen(str)] = 1;
  53. *(str + (int) strlen(str)) = *(st + i);
  54. }
  55. }
  56. min = stat[0];
  57. for (i = 1; i < (int) strlen(str); i++)
  58. {
  59. if (min > stat[i])
  60. min = stat[i];
  61. }
  62. while (*st)
  63. {
  64. for (i = 0; i < (int) strlen(str); i++)
  65. {
  66. if (*st == *(str + i) && stat[i] > min)
  67. putchar(*st);
  68. }
  69. st++;
  70. }
  71. }

  • HJ24 合唱队(动态规划,最长子序列问题)

  1. 输入:8
  2. 186 186 150 200 160 130 197 200
  3. 输出:4
  4. 说明:
  5. 由于不允许改变队列元素的先后顺序,所以最终剩下的队列应该为186 200 160 130150 200 160 130

示例代码:HJ24.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void)
  4. {
  5. int n;
  6. int i, j;
  7. int * ar;
  8. int * dp_left, * dp_right;
  9. int max = 0;
  10. while (scanf("%d", &n) == 1)
  11. {
  12. ar = (int *) malloc(sizeof(int) * n);
  13. dp_left = (int *) malloc(sizeof(int) * n);
  14. dp_right = (int *) malloc(sizeof(int) * n);
  15. for (i = 0; i < n; i++) // 初始化没有比该数更小的
  16. {
  17. scanf("%d", ar + i);
  18. dp_left[i] = 0;
  19. dp_right[i] = 0;
  20. }
  21. for (i = 1; i < n - 1; i++)
  22. {
  23. for (j = 0; j < i; j++)
  24. {
  25. if (ar[j] < ar[i])
  26. dp_left[i] = (dp_left[i] < dp_left[j] + 1) ? dp_left[j] + 1 : dp_left[i];
  27. }
  28. }
  29. for (i = n - 2; i > 0; i--)
  30. {
  31. for (j = n - 1; j > i; j--)
  32. {
  33. if (ar[j] < ar[i])
  34. dp_right[i] = (dp_right[i] < dp_right[j] + 1) ? dp_right[j] + 1 : dp_right[i];
  35. }
  36. }
  37. for (i = 1; i < n - 1; i++)
  38. {
  39. if (dp_left[i] && dp_right[i])
  40. max = (max > dp_left[i] + dp_right[i]) ? max : dp_left[i] + dp_right[i];
  41. }
  42. printf("%d\n", n - (max + 1));
  43. max = 0;
  44. }
  45. free(ar);
  46. free(dp_left);
  47. free(dp_right);
  48. return 0;
  49. }

  • HJ25 数据分类处理(排序)

  1. 输入:
  2. 15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
  3. 5 6 3 6 3 0
  4. 输出:
  5. 30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786
  6. 说明:
  7. 将序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)排序去重后,可得0,3,6
  8. 序列I没有包含0的元素。
  9. 序列I中包含3的元素有:I[0]的值为123、I[3]的值为453、I[7]的值为3、I[9]的值为453456、I[13]的值为453、I[14]的值为123
  10. 序列I中包含6的元素有:I[1]的值为456、I[2]的值为786、I[4]的值为46、I[8]的值为665、I[9]的值为453456、I[11]的值为456、I[12]的值为786
  11. 最后按题目要求的格式进行输出即可。

示例代码:HJ25.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #define LEN 100
  6. typedef struct {
  7. int num; // 规则数据
  8. int count; // 符合条件的数量
  9. int id[LEN];
  10. } Rule;
  11. typedef Rule * Pr;
  12. bool is_match(int rule, int data); // 判断数据是否符合规则
  13. void pro_rule(Pr pRule, int * pR_n); // 处理规则数据(去重、排序)
  14. int main(void)
  15. {
  16. int * list; // 存储出现的数据
  17. Pr rule; // 记录出现的规则
  18. int l_n; // 记录待处理的数据
  19. int r_n; // 记录规则数量
  20. int total = 0; // 记录符合规则的数量
  21. int rule_count = 0; // 记录不至少有一个数据符合规则的规则
  22. bool find = false; // 记录是否找到符合要求的规则
  23. int i, j;
  24. while (scanf("%d", &l_n) == 1)
  25. {
  26. list = (int *) malloc(sizeof(int) * l_n);
  27. for (i = 0; i < l_n; i++) // 读入待处理数据
  28. scanf("%d", list + i);
  29. scanf("%d", &r_n);
  30. rule = (Pr) malloc(sizeof(Rule) * r_n);
  31. for (i = 0; i < r_n; i++) // 读入规则
  32. {
  33. scanf("%d", &(rule + i)->num);
  34. (rule + i)->count = 0;
  35. }
  36. pro_rule(rule, &r_n);
  37. for (i = 0; i < r_n; i++)
  38. {
  39. for (j = 0; j < l_n; j++)
  40. {
  41. if (is_match(rule[i].num, list[j]))
  42. {
  43. find = true;
  44. rule[i].id[rule[i].count] = j;
  45. rule[i].count++;
  46. total++;
  47. }
  48. }
  49. if (find == true)
  50. {
  51. rule_count++;
  52. find = false;
  53. }
  54. }
  55. printf("%d ", (rule_count + total) * 2);
  56. for (i = 0; i < r_n; i++)
  57. {
  58. if (rule[i].count)
  59. {
  60. printf("%d %d ", rule[i].num, rule[i].count);
  61. for (j = 0; j < rule[i].count; j++)
  62. printf("%d %d ", rule[i].id[j], list[rule[i].id[j]]);
  63. }
  64. }
  65. total = rule_count = 0;
  66. }
  67. free(list);
  68. free(rule);
  69. return 0;
  70. }
  71. bool is_match(int rule, int data)
  72. {
  73. char str_r[LEN];
  74. char str_d[LEN];
  75. sprintf(str_r, "%d", rule);
  76. sprintf(str_d, "%d", data);
  77. if (strstr(str_d, str_r))
  78. return true;
  79. return false;
  80. }
  81. void pro_rule(Pr pRule, int * pR_n)
  82. {
  83. int temp;
  84. int i, j;
  85. /* 排序 */
  86. for (i = 0; i < *pR_n - 1; i++)
  87. {
  88. for (j = i + 1; j < *pR_n; j++)
  89. {
  90. if ((pRule + i)->num > (pRule + j)->num)
  91. {
  92. temp = (pRule + i)->num;
  93. (pRule + i)->num = (pRule + j)->num;
  94. (pRule + j)->num = temp;
  95. }
  96. }
  97. }
  98. /* 去重 */
  99. for (i = 1, j = 0; i < *pR_n; i++)
  100. {
  101. temp = (pRule + j)->num;
  102. if ((pRule + i)->num != temp)
  103. {
  104. j++;
  105. (pRule + j)->num = (pRule + i)->num;
  106. }
  107. }
  108. *pR_n = j + 1;
  109. }

  • HJ26 字符串排序(字符串)

  1. 输入:A Famous Saying: Much Ado About Nothing (2012/8).
  2. 输出:A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

示例代码:HJ26.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #define LEN 1001
  6. #define COUNT 125
  7. #define SIZE 26
  8. int main(void)
  9. {
  10. char str[LEN];
  11. static int str_mark[LEN];
  12. static int lower[SIZE][COUNT];
  13. static int upper[SIZE][COUNT];
  14. static int lower_time[SIZE];
  15. static int upper_time[SIZE];
  16. int len = 0;
  17. int upper_len;
  18. int lower_len;
  19. int i, j, k;
  20. char ch;
  21. char up_ch, lo_ch;
  22. static int l_k, u_k;
  23. bool find_l, find_u;
  24. while ((ch = getchar()) != '\n')
  25. {
  26. str[len] = ch;
  27. if (isupper(ch))
  28. {
  29. upper[ch - 'A'][0]++; // 出现的次数
  30. upper_time[ch - 'A']++; // 备份出现的次数
  31. upper_len = upper[ch - 'A'][0]; // 更新该字符出现的次数
  32. upper[ch - 'A'][upper_len] = len; // 记录该字符出现的位置
  33. str_mark[len] = 1;
  34. }
  35. else if (islower(ch))
  36. {
  37. lower[ch - 'a'][0]++;
  38. lower_time[ch - 'a']++;
  39. lower_len = lower[ch - 'a'][0];
  40. lower[ch - 'a'][lower_len] = len;
  41. str_mark[len] = 1;
  42. }
  43. len++;
  44. }
  45. for (i = 0; i < len; i++)
  46. {
  47. if (str_mark[i] == 1)
  48. {
  49. find_l = false;
  50. find_u = false;
  51. for (j = 0; j < SIZE; j++)
  52. {
  53. if (upper[j][0] > 0)
  54. {
  55. find_u = true;
  56. up_ch = j + 'A';
  57. break;
  58. }
  59. }
  60. for (k = 0; k < SIZE; k++)
  61. {
  62. if (lower[k][0] > 0)
  63. {
  64. find_l = true;
  65. lo_ch = k + 'a';
  66. break;
  67. }
  68. }
  69. if (find_l && find_u)
  70. {
  71. if (lo_ch < tolower(up_ch))
  72. ch = lo_ch;
  73. else if (lo_ch > tolower(up_ch))
  74. ch = up_ch;
  75. else
  76. {
  77. l_k = lower_time[k] - lower[k][0] + 1;
  78. u_k = upper_time[j] - upper[j][0] + 1;
  79. ch = lower[k][l_k] < upper[j][u_k] ? lo_ch : up_ch;
  80. }
  81. }
  82. else if (find_l)
  83. ch = lo_ch;
  84. else
  85. ch = up_ch;
  86. if (ch == lo_ch)
  87. lower[k][0]--;
  88. else
  89. upper[j][0]--;
  90. putchar(ch);
  91. }
  92. else
  93. putchar(str[i]);
  94. }
  95. return 0;
  96. }

  • HJ27 查找兄弟单词(字符串)

  1. 输入:6 cab ad abcd cba abc bca abc 1
  2. 输出:
  3. 3
  4. bca
  5. 说明:
  6. abc的兄弟单词有cab cba bca,所以输出3
  7. 经字典序排列后,变为bca cab cba,所以第1个字典序兄弟单词为bca

示例代码:HJ27.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #define LEN 12
  6. #define SIZE 26
  7. bool is_brother(char * st_1, char * st_2);
  8. void order(char ** pst, int n);
  9. int main(void)
  10. {
  11. int n;
  12. int sec;
  13. char temp[LEN];
  14. char ** pstr;
  15. char ** pbro;
  16. char * orig;
  17. int i;
  18. int count = 0;
  19. while (scanf("%d", &n) == 1)
  20. {
  21. pstr = (char **) malloc(sizeof(char *) * n);
  22. pbro = (char **) malloc(sizeof(char *) * n);
  23. for (i = 0; i < n; i++)
  24. {
  25. scanf("%s", temp);
  26. pstr[i] = (char *) malloc(sizeof(char) * ((int) strlen(temp) + 1));
  27. strcpy(pstr[i], temp);
  28. }
  29. scanf("%s", temp);
  30. orig = (char *) malloc(sizeof(char) * ((int) strlen(temp) + 1));
  31. strcpy(orig, temp);
  32. scanf("%d", &sec);
  33. for (i = 0; i < n; i++)
  34. {
  35. if (is_brother(pstr[i], orig))
  36. pbro[count++] = pstr[i];
  37. }
  38. printf("%d\n", count);
  39. if (sec <= count)
  40. {
  41. order(pbro, count);
  42. printf("%s\n", pbro[sec - 1]);
  43. }
  44. }
  45. for (i = 0; i < n; i++)
  46. free(pstr[i]);
  47. free(pstr);
  48. free(orig);
  49. free(pbro);
  50. return 0;
  51. }
  52. bool is_brother(char * st_1, char * st_2)
  53. {
  54. int len_1 = strlen(st_1);
  55. int len_2 = strlen(st_2);
  56. int st_1ch[SIZE], st_2ch[SIZE];
  57. int i;
  58. memset(st_1ch, 0, sizeof(st_1ch));
  59. memset(st_2ch, 0, sizeof(st_2ch));
  60. if (len_1 != len_2)
  61. return false;
  62. if (strcmp(st_1, st_2) == 0)
  63. return false;
  64. for (i = 0; i < len_1; i++)
  65. {
  66. st_1ch[st_1[i] - 'a']++;
  67. st_2ch[st_2[i] - 'a']++;
  68. }
  69. for (i = 0; i < SIZE; i++)
  70. {
  71. if (st_1ch[i] != st_2ch[i])
  72. return false;
  73. }
  74. return true;
  75. }
  76. void order(char ** pst, int n)
  77. {
  78. int i, j;
  79. char * temp;
  80. for (i = 0; i < n - 1; i++)
  81. {
  82. for (j = i + 1; j < n; j++)
  83. {
  84. if (strcmp(pst[i], pst[j]) > 0)
  85. {
  86. temp = pst[i];
  87. pst[i] = pst[j];
  88. pst[j] = temp;
  89. }
  90. }
  91. }
  92. }

  • HJ28 素数伴侣(二分图,匈牙利算法)

示例代码:HJ28.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. /* 判断是否为素数 */
  6. bool IsPrime(int num);
  7. /* 标记所有能够匹配的素数伴侣对 */
  8. void MarkMate(int evenN, int oddN, int * even, int * odd, bool ** mark);
  9. /* 判断当前偶数能够匹配到一个未被匹配的奇数 */
  10. bool FindPrimeMate(int num, int oddN, bool ** mark, bool * used, int * evenIdOfOdd);
  11. int main(void)
  12. {
  13. int n; // 一个正偶数,保存要输入的数的个数
  14. int * ar; // 保存输入的数字
  15. int * even; // 保存奇数
  16. int * odd; // 保存偶数
  17. int evenN = 0; // 保存偶数数量
  18. int oddN = 0; // 保存奇数数量
  19. bool ** mark; // 记录所有能够匹配的奇数和偶数下标
  20. bool * used; // 记录目前该奇数是否已被使用
  21. int * evenIdOfOdd; // 记录与奇数匹配的偶数的下标
  22. int temp; // 临时保存输入的数
  23. int count = 0; // 记录匹配对数
  24. int i, j;
  25. while (scanf("%d", &n) == 1)
  26. {
  27. ar = (int *) malloc(sizeof(int) * n);
  28. even = (int *) malloc(sizeof(int) * n);
  29. odd = (int *) malloc(sizeof(int) * n);
  30. for (i = 0; i < n; i++)
  31. {
  32. scanf("%d", &temp);
  33. if (0 == temp % 2)
  34. even[evenN++] = temp;
  35. else
  36. odd[oddN++] = temp;
  37. }
  38. mark = (bool **) malloc(sizeof(bool *) * evenN);
  39. used = (bool *) malloc(sizeof(bool) * oddN);
  40. for (i = 0; i < evenN; i++)
  41. mark[i] = (bool *) malloc(sizeof(bool) * oddN);
  42. for (i = 0; i < evenN; i++)
  43. {
  44. for (j = 0; j < oddN; j++)
  45. mark[i][j] = false;
  46. }
  47. evenIdOfOdd = (int *) malloc(sizeof(int) * oddN);
  48. memset(evenIdOfOdd, -1, sizeof(int) * oddN);
  49. /* 标记能够匹配的一对数字 */
  50. MarkMate(evenN, oddN, even, odd, mark);
  51. /* 开始配对 */
  52. for (i = 0; i < evenN; i++)
  53. {
  54. memset(used, false, sizeof(bool) * oddN);
  55. if (FindPrimeMate(i, oddN, mark, used, evenIdOfOdd))
  56. count++;
  57. }
  58. /* 配对完成 */
  59. printf("%d", count);
  60. }
  61. free(ar);
  62. free(even);
  63. free(odd);
  64. for (i = 0; i < evenN; i++)
  65. free(mark[i]);
  66. free(mark);
  67. free(evenIdOfOdd);
  68. return 0;
  69. }
  70. bool IsPrime(int num)
  71. {
  72. int i;
  73. for (i = 2; i * i <= num; i++)
  74. {
  75. if (num % i == 0)
  76. return false;
  77. }
  78. return true;
  79. }
  80. void MarkMate(int evenN, int oddN, int * even, int * odd, bool ** mark)
  81. {
  82. int i, j;
  83. for (i = 0; i < evenN; i++)
  84. {
  85. for (j = 0; j < oddN; j++)
  86. {
  87. if (IsPrime(even[i] + odd[j]))
  88. mark[i][j] = true;
  89. }
  90. }
  91. }
  92. /* 匈牙利算法 */
  93. bool FindPrimeMate(int num, int oddN, bool ** mark, bool * used, int * evenIdOfOdd)
  94. {
  95. int i;
  96. /* 找到第一个能够匹配的奇数,若该数已经被匹配,则尝试给该数对应的偶数
  97. 再找一个新的数匹配并替代该数为当前偶数的匹配数,若找不到新的匹配数,则
  98. 尝试找第二个能够匹配的奇数,若最终扫描到最后一个奇数仍然无法匹配成功,
  99. 则匹配失败 */
  100. for (i = 0; i < oddN; i++)
  101. {
  102. if (mark[num][i] && !used[i])
  103. {
  104. used[i] = true;
  105. if (evenIdOfOdd[i] == -1 || FindPrimeMate(evenIdOfOdd[i], oddN, mark, used, evenIdOfOdd))
  106. {
  107. evenIdOfOdd[i] = num;
  108. return true;
  109. }
  110. }
  111. }
  112. return false;
  113. }

  • HJ29 字符串加解密(字符串)

  1. 输入:
  2. abcdefg
  3. BCDEFGH
  4. 输出:
  5. BCDEFGH
  6. abcdefg

示例代码:HJ29.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 1001
  5. void enc(char * st);
  6. void dec(char * st);
  7. int main(void)
  8. {
  9. char str_ori[LEN];
  10. char str_sec[LEN];
  11. while (scanf("%s%s", str_ori, str_sec) == 2)
  12. {
  13. enc(str_ori);
  14. dec(str_sec);
  15. }
  16. return 0;
  17. }
  18. void enc(char * st)
  19. {
  20. static char str[LEN];
  21. int len = 0;
  22. while (*st)
  23. {
  24. if (islower(*st))
  25. str[len++] = toupper((*st - 'a' + 1) % 26 + 'a');
  26. else if (isupper(*st))
  27. str[len++] = tolower((*st - 'A' + 1) % 26 + 'A');
  28. else
  29. str[len++] = (*st - '0' + 1) % 10 + '0';
  30. st++;
  31. }
  32. puts(str);
  33. }
  34. void dec(char * st)
  35. {
  36. static char str[LEN];
  37. int len = 0;
  38. while (*st)
  39. {
  40. if (islower(*st))
  41. str[len++] = toupper((*st - 'a' - 1 + 26) % 26 + 'a');
  42. else if (isupper(*st))
  43. str[len++] = tolower((*st - 'A' - 1 + 26) % 26 + 'A');
  44. else
  45. str[len++] = (*st - '0' - 1 + 10) % 10 + '0';
  46. st++;
  47. }
  48. puts(str);
  49. }

  • HJ30 字符串合并处理(字符串)

  1. 输入:
  2. ab CD
  3. 输出:
  4. 3B5D
  5. 说明:
  6. 合并后为abCD,按奇数位和偶数位排序后是CDab(请注意要按ascii码进行排序,所以C在a前面,D在b前面),转换后为3B5D

示例代码:HJ30.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #define LEN 101
  6. void order(char * st); // 排序字符串
  7. void trans(char * st); // 转换字符串
  8. char proCh(char ch); // 转换字符
  9. int main(void)
  10. {
  11. static char str[LEN * 2]; // 保存合并后的字符串
  12. char temp[LEN]; // 第二个字符串
  13. while (scanf("%s%s", str, temp) == 2)
  14. {
  15. strcat(str, temp);
  16. order(str);
  17. trans(str);
  18. }
  19. return 0;
  20. }
  21. void order(char * st)
  22. {
  23. int i, j;
  24. int len = strlen(st);
  25. char temp;
  26. if (len < 3)
  27. return;
  28. /* 排列偶数位 */
  29. for (i = 0; st[i + 2] != '\0'; i += 2)
  30. {
  31. for (j = i + 2; st[j] != '\0'; j += 2)
  32. {
  33. if (st[i] > st[j])
  34. {
  35. temp = st[i];
  36. st[i] = st[j];
  37. st[j] = temp;
  38. }
  39. }
  40. }
  41. /* 排列奇数位 */
  42. for (i = 1; st[i + 2] != '\0'; i += 2)
  43. {
  44. for (j = i + 2; st[j] != '\0'; j += 2)
  45. {
  46. if (st[i] > st[j])
  47. {
  48. temp = st[i];
  49. st[i] = st[j];
  50. st[j] = temp;
  51. }
  52. }
  53. }
  54. }
  55. void trans(char * st)
  56. {
  57. while (*st)
  58. {
  59. if ((*st >= 'a' && *st <= 'f')|| (*st >= 'A' && *st <= 'F')
  60. || isdigit(*st))
  61. *st = proCh(*st);
  62. putchar(*st);
  63. st++;
  64. }
  65. }
  66. char proCh(char ch)
  67. {
  68. int temp;
  69. int i;
  70. int num = 0;
  71. if (isupper(ch))
  72. ch = tolower(ch);
  73. if (isalpha(ch))
  74. temp = ch - 'a' + 10;
  75. else
  76. temp = ch - '0';
  77. for (i = 0; i < 4; i++)
  78. num = 2 * num + ((temp >> i) & 1) ;
  79. if (num > 9)
  80. ch = toupper(num - 10 + 'a');
  81. else
  82. ch = num + '0';
  83. return ch;
  84. }

  • HJ31 单词倒排

  1. 输入:
  2. I am a student
  3. 输出:
  4. student a am I

示例代码:HJ31.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 10001
  5. char * s_gets(char * st, int n);
  6. void reverse(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. reverse(str);
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. void reverse(char * st)
  31. {
  32. int i;
  33. for (i = (int) strlen(st) - 1; i > -1; i--)
  34. {
  35. if (!isalpha(*(st + i)))
  36. {
  37. *(st + i) = '\0';
  38. printf("%s ", st + i + 1);
  39. }
  40. if (i == 0 && isalpha(*st))
  41. printf("%s", st);
  42. }
  43. }

  • HJ32 密码截取(字符串,最长回文子串问题)

  1. 输入:
  2. ABBA
  3. 输出:
  4. 4

示例代码:HJ32.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 2501
  4. int get_max(char * st);
  5. int main(void)
  6. {
  7. char str[LEN];
  8. while (scanf("%s", str) == 1)
  9. printf("%d", get_max(str));
  10. return 0;
  11. }
  12. int get_max(char * st)
  13. {
  14. int i, j;
  15. int le, ri;
  16. int len = strlen(st);
  17. int max = 0;
  18. for (i = 0; i < len - 1; i++)
  19. {
  20. for (j = i + 1; j < len; j++)
  21. {
  22. le = i;
  23. ri = j;
  24. while (le < ri)
  25. {
  26. if (st[le] != st[ri])
  27. {
  28. break;
  29. }
  30. else
  31. {
  32. le++;
  33. ri--;
  34. }
  35. }
  36. if (le >= ri)
  37. max = (max > j - i + 1) ? max : j - i + 1;
  38. }
  39. }
  40. return max;
  41. }

  • HJ33 整数与IP地址间的转换(字符串,位运算)

  1. 输入:
  2. 10.0.3.193
  3. 167969729
  4. 输出:
  5. 167773121
  6. 10.3.3.193

示例代码:HJ33.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define SIZE 8
  4. #define LEN 32
  5. void dec_bin(long int num, char * st);
  6. void dec_bin32(long long int num, char * st);
  7. void bin_dec(char * st);
  8. void cut_str(char * dest, char * start, char * end);
  9. int main(void)
  10. {
  11. static char st_1[SIZE + 1];
  12. static char st_2[SIZE + 1];
  13. static char st_3[SIZE + 1];
  14. static char st_4[SIZE + 1];
  15. static char str[4 * SIZE + 1];
  16. int ip_1, ip_2, ip_3, ip_4;
  17. long long int ip;
  18. scanf("%d.%d.%d.%d", &ip_1, &ip_2, &ip_3, &ip_4);
  19. dec_bin(ip_1, st_1);
  20. dec_bin(ip_2, st_2);
  21. dec_bin(ip_3, st_3);
  22. dec_bin(ip_4, st_4);
  23. strcat(str, st_1);
  24. strcat(str, st_2);
  25. strcat(str, st_3);
  26. strcat(str, st_4);
  27. bin_dec(str);
  28. putchar('\n');
  29. scanf("%lld", &ip);
  30. dec_bin32(ip, str);
  31. cut_str(st_1, str, str + 7);
  32. cut_str(st_2, str + 8, str + 15);
  33. cut_str(st_3, str + 16, str + 23);
  34. cut_str(st_4, str + 24, str + 31);
  35. bin_dec(st_1);
  36. putchar('.');
  37. bin_dec(st_2);
  38. putchar('.');
  39. bin_dec(st_3);
  40. putchar('.');
  41. bin_dec(st_4);
  42. putchar('\n');
  43. return 0;
  44. }
  45. void dec_bin(long int num, char * st)
  46. {
  47. int i, j;
  48. char str[SIZE + 1];
  49. for (i = 0; i < SIZE; i++)
  50. {
  51. str[i] = (num & 1) + '0';
  52. num >>= 1;
  53. }
  54. for (i = 0, j = SIZE - 1; i < SIZE; i++, j--)
  55. st[i] = str[j];
  56. }
  57. void dec_bin32(long long int num, char * st)
  58. {
  59. int i, j;
  60. char str[LEN + 1];
  61. for (i = 0; i < LEN; i++)
  62. {
  63. str[i] = (num & 1) + '0';
  64. num >>= 1;
  65. }
  66. for (i = 0, j = LEN - 1; i < LEN; i++, j--)
  67. st[i] = str[j];
  68. }
  69. void bin_dec(char * st)
  70. {
  71. long long int num = 0;
  72. while (*st)
  73. {
  74. num = 2 * num + *st - '0';
  75. st++;
  76. }
  77. printf("%lld", num);
  78. }
  79. void cut_str(char * dest, char * start, char * end)
  80. {
  81. int i;
  82. for (i = 0; i <= end - start; i++)
  83. dest[i] = *(start + i);
  84. }

  • HJ34 图片整理(字符串)

  1. 输入:
  2. Ihave1nose2hands10fingers
  3. 输出:
  4. 0112Iaadeeefghhinnnorsssv

示例代码:HJ34.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. char * s_gets(char * st, int n);
  5. void ascii_up(char * st);
  6. int main(void)
  7. {
  8. char str[LEN];
  9. if (s_gets(str, LEN) != NULL && *str != '\0')
  10. ascii_up(str);
  11. puts(str);
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n')
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. void ascii_up(char * st)
  31. {
  32. int i, j;
  33. char ch;
  34. int len;
  35. len = strlen(st);
  36. for (i = 0; i < len - 1; i++)
  37. {
  38. for (j = i + 1; j < len; j++)
  39. {
  40. if (*(st + i) > *(st + j))
  41. {
  42. ch = *(st + i);
  43. *(st + i) = *(st + j);
  44. *(st + j) = ch;
  45. }
  46. }
  47. }
  48. }

  • HJ35 蛇形矩阵(基础数学,数组)

  1. 输入:
  2. 4
  3. 输出:
  4. 1 3 6 10
  5. 2 5 9
  6. 4 8
  7. 7

示例代码:HJ35.c

  1. #include <stdio.h>
  2. void snack_matrix(int n);
  3. int main(void)
  4. {
  5. int num;
  6. scanf("%d", &num);
  7. snack_matrix(num);
  8. return 0;
  9. }
  10. void snack_matrix(int n)
  11. {
  12. int i, j;
  13. int start = 1;
  14. int num;
  15. for (i = n; i > 0; i--)
  16. {
  17. start += n - i;
  18. num = start;
  19. for (j = 1; j <= i; j++)
  20. {
  21. if (j == 1)
  22. printf("%d ", start);
  23. else
  24. printf("%d ", num += j + n - i);
  25. }
  26. putchar('\n');
  27. }
  28. }

  • HJ36 字符串加密(字符串)

  1. 输入:
  2. nihao
  3. ni
  4. 输出:
  5. le

示例代码:HJ36.c

  1. #include <stdio.h>
  2. #define LEN 26
  3. int main(void)
  4. {
  5. static int ar[LEN];
  6. static char code[LEN];
  7. char ch;
  8. int i;
  9. int len = 0;
  10. while ((ch = getchar()) != '\n')
  11. {
  12. ar[ch - 'a']++;
  13. if (ar[ch - 'a'] == 1)
  14. code[len++] = ch;
  15. }
  16. for (i = 0; i < LEN; i++)
  17. {
  18. if (!ar[i])
  19. code[len++] = 'a' + i;
  20. }
  21. while ((ch = getchar()) != '\n')
  22. putchar(code[ch - 'a']);
  23. return 0;
  24. }

  • HJ37 统计每个月兔子的总数(排序,斐波拉契数列)

  1. 输入:
  2. 3
  3. 输出:
  4. 2

示例代码:HJ37.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int time;
  5. int i;
  6. scanf("%d", &time);
  7. int value[time + 1];
  8. value[1] = value[2] = 1;
  9. for (i = 3; i <= time; i++)
  10. value[i] = value[i - 2] + value[i - 1];
  11. printf("%d", value[time]);
  12. return 0;
  13. }

  • HJ38 求小球落地5次后所经历的路程和第5次反弹的高度(基础数学)

  1. 输入:
  2. 1
  3. 输出:
  4. 2.875
  5. 0.03125

示例代码:HJ38.c

  1. #include <stdio.h>
  2. void print_info(int n);
  3. int main(void)
  4. {
  5. int meters;
  6. scanf("%d", &meters);
  7. print_info(meters);
  8. return 0;
  9. }
  10. void print_info(int n)
  11. {
  12. float up_meter = n;
  13. float total = n;
  14. int times = 4;
  15. while (times)
  16. {
  17. up_meter = up_meter / 2;
  18. total += up_meter * 2;
  19. times--;
  20. }
  21. up_meter /= 2;
  22. printf("%.6f\n%.6f\n", total, up_meter);
  23. }

  • HJ39 判断两个IP是否属于同一子网(字符串)

  1. 输入:
  2. 255.255.255.0
  3. 192.168.224.256
  4. 192.168.10.4
  5. 255.0.0.0
  6. 193.194.202.15
  7. 232.43.7.59
  8. 255.255.255.0
  9. 192.168.0.254
  10. 192.168.0.1
  11. 输出:
  12. 1
  13. 2
  14. 0
  15. 说明:
  16. 对于第一个例子:
  17. 255.255.255.0
  18. 192.168.224.256
  19. 192.168.10.4
  20. 其中IP:192.168.224.256不合法,输出1
  21. 对于第二个例子:
  22. 255.0.0.0
  23. 193.194.202.15
  24. 232.43.7.59
  25. 2个与运算之后,不在同一个子网,输出2
  26. 对于第三个例子,2个与运算之后,如题目描述所示,在同一个子网,输出0

示例代码:HJ39.c

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #define LEN 20
  7. bool IsLegalIp(char * st);
  8. bool IsLegalMask(char * st);
  9. bool IsSubNet(char * mask, char * ip1, char * ip2);
  10. int main(void)
  11. {
  12. char mask[LEN]; // 掩码
  13. char mask_b[LEN]; // 备份掩码, 注意不要对同一字符串重复分割(strtok())
  14. char ip1[LEN]; // 第一个 ip
  15. char ip2[LEN]; // 第二个ip
  16. while (scanf("%s%s%s", mask, ip1, ip2) == 3)
  17. {
  18. strcpy(mask_b, mask);
  19. if (!(IsLegalMask(mask)) || !(IsLegalIp(ip1))
  20. || !(IsLegalIp(ip2)))
  21. printf("1\n");
  22. else if (IsSubNet(mask_b, ip1, ip2))
  23. printf("0\n");
  24. else
  25. printf("2\n");
  26. }
  27. return 0;
  28. }
  29. bool IsLegalIp(char * st)
  30. {
  31. char * st_b = st;
  32. int num_count = 0;
  33. int dot_count = 0;
  34. int num = 0;
  35. while (*st)
  36. {
  37. if ((st == st_b && !(*st >= '0' && *st <= '9'))
  38. || (*st == '.' && !(*(st + 1) >= '0' && *(st + 1) <= '9')))
  39. return false;
  40. if (st == st_b && *st == '0' && isdigit(*(st + 1)))
  41. return false;
  42. if (*st == '.' && *(st + 1) == '0' && isdigit(*(st + 2)))
  43. return false;
  44. if (isdigit(*st))
  45. {
  46. num = 10 * num + *st - '0';
  47. if (!isdigit(*(st + 1)))
  48. num_count++;
  49. }
  50. if (*st == '.')
  51. {
  52. dot_count++;
  53. if (!(num >= 0 && num < 256))
  54. return false;
  55. num = 0;
  56. }
  57. st++;
  58. }
  59. if (!(num_count == 4 && dot_count == 3) || !(num >= 0 && num < 256))
  60. return false;
  61. return true;
  62. }
  63. bool IsLegalMask(char * st)
  64. {
  65. char * mask_part;
  66. int num;
  67. int i, j;
  68. bool count_zero = false;
  69. int count_one = 0;
  70. int temp;
  71. mask_part = strtok(st, ".");
  72. for (i = 0; i < 4; i++)
  73. {
  74. num = atoi(mask_part);
  75. if (num == 255)
  76. count_one++;
  77. if ((i == 0 && num == 0) || num < 0 || num > 255)
  78. return false;
  79. for (j = 0; j < 8; j++)
  80. {
  81. temp = (num >> (7 - j)) & 1;
  82. if (temp == 0)
  83. count_zero = true;
  84. if (temp == 1 && count_zero)
  85. return false;
  86. }
  87. if (i < 3)
  88. mask_part = strtok(NULL, ".");
  89. }
  90. if (count_one == 4)
  91. return false;
  92. return true;
  93. }
  94. bool IsSubNet(char * mask, char * ip1, char * ip2)
  95. {
  96. int ip1_p[4];
  97. int ip2_p[4];
  98. int mask_p[4];
  99. int temp1;
  100. int temp2;
  101. char * temp = NULL;
  102. int i, j;
  103. temp = strtok(mask, ".");
  104. for (i = 0; i < 4; i++)
  105. {
  106. mask_p[i] = atoi(temp);
  107. if (i < 3)
  108. temp = strtok(NULL, ".");
  109. }
  110. temp = strtok(ip1, ".");
  111. for (i = 0; i < 4; i++)
  112. {
  113. ip1_p[i] = atoi(temp);
  114. if (i < 3)
  115. temp = strtok(NULL, ".");
  116. }
  117. temp = strtok(ip2, ".");
  118. for (i = 0; i < 4; i++)
  119. {
  120. ip2_p[i] = atoi(temp);
  121. if (i < 3)
  122. temp = strtok(NULL, ".");
  123. }
  124. for (i = 0; i < 4; i++)
  125. {
  126. for (j = 0; j < 8; j++)
  127. {
  128. temp1 = ((ip1_p[i] >> j) & 1) & ((mask_p[i] >> j) & 1);
  129. temp2 = ((ip2_p[i] >> j) & 1) & ((mask_p[i] >> j) & 1);
  130. if (temp1 != temp2)
  131. return false;
  132. }
  133. }
  134. return true;
  135. }

  • HJ40 统计字符(字符串)

  1. 输入:
  2. 1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
  3. 输出:
  4. 26
  5. 3
  6. 10
  7. 12

示例代码:HJ40.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 1001
  5. char * s_gets(char * st, int n);
  6. void get_count(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. get_count(str);
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. void get_count(char * st)
  31. {
  32. int ch_count = 0, space_count = 0, num_count = 0, other_count = 0;
  33. while (*st)
  34. {
  35. if (isalpha(*st))
  36. ch_count++;
  37. else if (isspace(*st))
  38. space_count++;
  39. else if (isdigit(*st))
  40. num_count++;
  41. else
  42. other_count++;
  43. st++;
  44. }
  45. printf("%d\n%d\n%d\n%d\n", ch_count, space_count, num_count, other_count);
  46. }

  • HJ41 称砝码(基础数学)

  1. 输入:
  2. 2
  3. 1 2
  4. 2 1
  5. 输出:
  6. 5
  7. 说明:
  8. 可以表示出01234五种重量。

示例代码:HJ41.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int get_type(int n, int heav[], int num[]);
  4. int main(void)
  5. {
  6. int n;
  7. int i;
  8. while (scanf("%d", &n) == 1)
  9. {
  10. int heav[n], num[n];
  11. for (i = 0; i < n; i++)
  12. scanf("%d", heav + i);
  13. for (i = 0; i < n; i++)
  14. scanf("%d", num + i);
  15. printf("%d", get_type(n, heav, num));
  16. }
  17. return 0;
  18. }
  19. int get_type(int n, int heav[], int num[])
  20. {
  21. int i, j, k;
  22. int max = 0;
  23. int count = 0;
  24. for (i = 0; i < n; i++)
  25. max += heav[i] * num[i];
  26. int mark[max + 1];
  27. for (i = 1; i <= max; i++)
  28. mark[i] = 0;
  29. mark[0] = 1;
  30. for (i = 0; i < n; i++)
  31. {
  32. for (j = 0; j < num[i]; j++)
  33. {
  34. for (k = max; k >= 0; k--)
  35. {
  36. if (mark[k])
  37. mark[k + heav[i]] = 1;
  38. }
  39. }
  40. }
  41. for (i = 0; i <= max; i++)
  42. if (mark[i])
  43. count++;
  44. return count;
  45. }

  • HJ42 学英语

  1. 输入:
  2. 22
  3. 输出:
  4. twenty two

示例代码:HJ42.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. void get_bits(int * ar, long n);
  5. void print_bits(int num, char ** s1, char ** s2, char ** s3);
  6. int main(void)
  7. {
  8. long n; // 保存输入数字
  9. static int bits[4]; // 获取不同的位的数量
  10. char * s1[20] = { "", "one", "two", "three", "four", "five",
  11. "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
  12. "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
  13. "eighteen", "nineteen" };
  14. char * s2[10] = { "", "", "twenty", "thirty", "forty", "fifty",
  15. "sixty", "seventy", "eighty", "ninety" };
  16. char * s3[4] = { "hundred", "thousand", "million", "billion" };
  17. bool flag = false;
  18. while (scanf("%ld", &n) == 1)
  19. {
  20. get_bits(bits, n);
  21. if (bits[3])
  22. {
  23. print_bits(bits[3], s1, s2, s3);
  24. printf(" %s", s3[3]);
  25. flag = true;
  26. }
  27. if (bits[2])
  28. {
  29. if (flag)
  30. putchar(' ');
  31. print_bits(bits[2], s1, s2, s3);
  32. printf(" %s", s3[2]);
  33. flag = true;
  34. }
  35. if (bits[1])
  36. {
  37. if (flag)
  38. putchar(' ');
  39. print_bits(bits[1], s1, s2, s3);
  40. printf(" %s", s3[1]);
  41. flag = true;
  42. }
  43. if (bits[0])
  44. {
  45. if (flag)
  46. putchar(' ');
  47. print_bits(bits[0], s1, s2, s3);
  48. }
  49. putchar('\n');
  50. }
  51. return 0;
  52. }
  53. void get_bits(int * ar, long n)
  54. {
  55. ar[0] = n % 1000;
  56. ar[1] = (n / 1000) % 1000; // thousand
  57. ar[2] = (n / (1000 * 1000)) % 1000; // million
  58. ar[3] = n / (1000 * 1000 * 1000); // billion
  59. }
  60. void print_bits(int num, char ** s1, char ** s2, char ** s3)
  61. {
  62. bool flag = false;
  63. if (num >= 100)
  64. {
  65. printf("%s %s", s1[num / 100], s3[0]);
  66. flag = true;
  67. num %= 100;
  68. }
  69. if (num >= 20)
  70. {
  71. if (flag)
  72. printf(" and ");
  73. printf("%s", s2[num / 10]);
  74. num %= 10;
  75. if (num)
  76. printf(" %s", s1[num]);
  77. }
  78. else if (num)
  79. {
  80. if (flag)
  81. printf(" and ");
  82. printf("%s", s1[num]);
  83. }
  84. }

HJ43 迷宫问题(DFS)

  1. 输入:
  2. 5 5
  3. 0 1 0 0 0
  4. 0 1 1 1 0
  5. 0 0 0 0 0
  6. 0 1 1 1 0
  7. 0 0 0 1 0
  8. 输出:
  9. (0,0)
  10. (1,0)
  11. (2,0)
  12. (2,1)
  13. (2,2)
  14. (2,3)
  15. (2,4)
  16. (3,4)
  17. (4,4)

示例代码:HJ43.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. bool DFS(int m, int n, int maze[][n], int visa[][n], int stack[][2], int x, int y, int top);
  4. int main(void)
  5. {
  6. int m, n; // m 行 n 列
  7. int i, j;
  8. int top = 0;
  9. scanf("%d%d", &m, &n);
  10. int maze[m][n]; // 表示迷宫
  11. int visa[m][n]; // 是否经过
  12. int stack[m * n][2]; // 记录路径的坐标
  13. for (i = 0; i < m; i++)
  14. {
  15. for (j = 0; j < n; j++)
  16. {
  17. scanf("%d", *(maze + i) + j);
  18. visa[i][j] = 0;
  19. }
  20. }
  21. for (i = 0; i < m * n; i++)
  22. {
  23. stack[i][0] = 0;
  24. stack[i][1] = 0;
  25. }
  26. DFS(m, n, maze, visa, stack, 0, 0, top);
  27. return 0;
  28. }
  29. bool DFS(int m, int n, int maze[][n], int visa[][n], int stack[][2], int x, int y, int top)
  30. {
  31. int i;
  32. /* 递归出口 */
  33. if (x == m - 1 && y == n - 1)
  34. {
  35. stack[top][0] = x;
  36. stack[top][1] = y;
  37. for (i = 0; i <= top; i++)
  38. printf("(%d,%d)\n", stack[i][0], stack[i][1]);
  39. return true;
  40. }
  41. /* 如果超出界限或者已经经过或者是墙 */
  42. else if (x >= m || x < 0 || y >= n || y < 0 || visa[x][y] || maze[x][y])
  43. return false;
  44. else
  45. {
  46. visa[x][y] = 1;
  47. stack[top][0] = x;
  48. stack[top][1] = y;
  49. top++;
  50. /* 尝试向上、下、左、右 方法走 */
  51. if (DFS(m, n, maze, visa, stack, x - 1, y, top) || DFS(m, n, maze, visa, stack, x + 1, y, top)
  52. || DFS(m, n, maze, visa, stack, x, y - 1, top) || DFS(m, n, maze, visa, stack, x, y + 1, top))
  53. return true;
  54. /* 若四个方向都走不通,则回溯,但没有必要回溯的信息,top是个复制的参数,且走不通的路没必要再走 */
  55. }
  56. return false;
  57. }

  • HJ44 Sudoku(DFS)

  1. 输入:
  2. 0 9 2 4 8 1 7 6 3
  3. 4 1 3 7 6 2 9 8 5
  4. 8 6 7 3 5 9 4 1 2
  5. 6 2 4 1 9 5 3 7 8
  6. 7 5 9 8 4 3 1 2 6
  7. 1 3 8 6 2 7 5 9 4
  8. 2 7 1 5 3 8 6 4 9
  9. 3 8 6 9 1 4 2 5 7
  10. 0 4 5 2 7 6 8 3 1
  11. 输出:
  12. 5 9 2 4 8 1 7 6 3
  13. 4 1 3 7 6 2 9 8 5
  14. 8 6 7 3 5 9 4 1 2
  15. 6 2 4 1 9 5 3 7 8
  16. 7 5 9 8 4 3 1 2 6
  17. 1 3 8 6 2 7 5 9 4
  18. 2 7 1 5 3 8 6 4 9
  19. 3 8 6 9 1 4 2 5 7
  20. 9 4 5 2 7 6 8 3 1

示例代码:HJ44.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 9
  4. int DFS(int map[][LEN], int pos, int (*popen)[2], int count);
  5. int islegal(int map[][LEN], int x, int y, int num);
  6. int main()
  7. {
  8. int map[LEN][LEN];
  9. int pos = 0;
  10. int i, j;
  11. int count = 0;
  12. int open[LEN * LEN][2];
  13. /* 读入数据并记录空位 */
  14. for (i = 0; i < LEN; i++)
  15. {
  16. for (j = 0; j < LEN; j++)
  17. {
  18. scanf("%d", &map[i][j]);
  19. if (!map[i][j])
  20. {
  21. open[count][0] = i;
  22. open[count++][1] = j;
  23. }
  24. }
  25. }
  26. /* 深度优先遍历 */
  27. if (DFS(map, pos, open, count))
  28. {
  29. putchar('\n') ;
  30. for (i = 0; i < LEN; i++)
  31. {
  32. for (j = 0; j < LEN; j++)
  33. printf("%d ", map[i][j]);
  34. printf("\n");
  35. }
  36. }
  37. return 0;
  38. }
  39. /* pos 给第 pos个缺位匹配数值 pos 0 ~ count - 1。 DFS 本质就是穷举,找到一条成功的
  40. 路线之后不断返回上一层,return true,return true...,直到最上面一层,不需要要所有情况
  41. 都成功,只需要一条成功则达到目的。而所谓回溯实质上是退回对某个地址的值的改变。回溯与
  42. 否看会不会对其他路线造成影响,若会,则消除改变(如对数组和指针进行操作,则会改变该地
  43. 址的值),否则,则可不必回溯(如参数只是一个复制)。此外DFS不是多线程,所有路线是一次
  44. 一次尝试的,尝试到成功则停止,类似于树的遍历算法,也就意味着后面的操作可能会受到前面操
  45. 作的影响(例如改变了数组的值会对后面的计算判断造成影响,此时请务必回溯。)*/
  46. int DFS(int map[][LEN], int pos, int (*popen)[2], int count)
  47. {
  48. int x, y;
  49. int i;
  50. if (pos == count) /* 递归出口,填满了 */
  51. return 1;
  52. x = popen[pos][0];
  53. y = popen[pos][1];
  54. /* 尝试填入数字 */
  55. for (i = 1; i <= 9; i++)
  56. {
  57. if (islegal(map, x, y, i))
  58. {
  59. map[x][y] = i;
  60. if (DFS(map, pos + 1, popen, count))
  61. return 1;
  62. /* 回溯,目的是不对之后合法性计算造成影响,此处传递的是地址,故类似于全局变量 */
  63. map[x][y] = 0;
  64. }
  65. }
  66. return 0;
  67. }
  68. int islegal(int map[][LEN], int x, int y, int num)
  69. {
  70. int ret = 1;
  71. int cx, cy;
  72. int i, j;
  73. /* 判断填入行是否合法 */
  74. for (i = 0; i < LEN; i++)
  75. {
  76. if (i != x && num == map[i][y])
  77. return 0;
  78. }
  79. /* 判断填入列是否合法 */
  80. for (j = 0; j < LEN; j++)
  81. {
  82. if (j != y && num == map[x][j])
  83. return 0;
  84. }
  85. /* 判断所在九宫格是否合法 */
  86. cx = x / 3;
  87. cy = y / 3;
  88. for (i = cx * 3; i < cx * 3 + 3; i++)
  89. {
  90. for (j = cy * 3; j < cy * 3 + 3; j++)
  91. {
  92. if ((i != x || j != y) && num == map[i][j])
  93. return 0;
  94. }
  95. }
  96. return ret;
  97. }

  • HJ45 名字的漂亮度(字符串)

  1. 输入:
  2. 2
  3. zhangsan
  4. lisi
  5. 输出:
  6. 192
  7. 101
  8. 说明:
  9. 对于样例lisi,让i的漂亮度为26,l的漂亮度为25,s的漂亮度为24

示例代码:HJ45.c

  1. #include <stdio.h>
  2. #define LEN 26
  3. int main(void)
  4. {
  5. static int ar[LEN];
  6. int i, j;
  7. int n;
  8. char ch;
  9. int max = 0;
  10. int score = 26;
  11. int total_score = 0;
  12. scanf("%d", &n);
  13. getchar();
  14. for (i = 0; i < n; i++)
  15. {
  16. while ((ch = getchar()) != '\n')
  17. {
  18. ar[ch - 'a']++;
  19. if (ar[ch - 'a'] > max)
  20. max = ar[ch - 'a'];
  21. }
  22. while (max)
  23. {
  24. for (j = 0; j < LEN; j++)
  25. {
  26. if (ar[j] == max)
  27. {
  28. total_score += score * max;
  29. score--;
  30. }
  31. }
  32. max--;
  33. }
  34. printf("%d\n", total_score);
  35. score = 26;
  36. max = total_score = 0;
  37. memset(ar, 0, sizeof(int) * LEN);
  38. }
  39. return 0;
  40. }

  • HJ46 截取字符串(字符串)

  1. 输入:
  2. abABCcDEF
  3. 6
  4. 输出:
  5. abABCc

示例代码:HJ46.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. char * s_gets(char * st, int n);
  5. char * get_n_word(char * st, int n);
  6. int main(void)
  7. {
  8. char str[LEN];
  9. int n;
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. {
  12. scanf("%d", &n);
  13. printf("%s", get_n_word(str, n));
  14. }
  15. return 0;
  16. }
  17. char * s_gets(char * st, int n)
  18. {
  19. char * ret_val;
  20. char * find;
  21. ret_val = fgets(st, n, stdin);
  22. if (ret_val)
  23. {
  24. find = strchr(st, '\n');
  25. if (find)
  26. *find = '\0';
  27. else
  28. while (getchar() != '\n')
  29. continue;
  30. }
  31. return ret_val;
  32. }
  33. char * get_n_word(char * st, int n)
  34. {
  35. if (n > (int) strlen(st))
  36. return NULL;
  37. else
  38. *(st + n) = '\0';
  39. return st;
  40. }

  • HJ48 从单向链表中删除指定值的节点(带头结点的单向链表)

  1. 输入:
  2. 5 2 3 2 4 3 5 2 1 4 3
  3. 输出:
  4. 2 5 4 1
  5. 说明:
  6. 形成的链表为2->5->3->4->1
  7. 删掉节点3,返回的就是2->5->4->1

示例代码:HJ48.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. typedef struct node{
  5. int n_num;
  6. struct node * next;
  7. } Node;
  8. typedef Node * List;
  9. /* 初始化要改变头指针的值,因此要用指针 */
  10. bool initia(List * list, int num);
  11. /* 如果不需要在头节点之后插入值,则不需要头节点指针 */
  12. bool insert(List list, int ins_num, int num);
  13. /* 考虑到删除头节点,加指针 */
  14. void dele_num(List * list, int del_num);
  15. /* 打印节点,不需要指针 */
  16. void show_list(List list);
  17. /* 改变头节点的位置,需要头节点的指针 */
  18. void free_list(List * list);
  19. int main(void)
  20. {
  21. List head = NULL;
  22. int total;
  23. int h_num;
  24. int i;
  25. int i_num, af_num;
  26. int del_num;
  27. scanf("%d%d", &total, &h_num);
  28. initia(&head, h_num);
  29. for (i = 0; i < total - 1; i++)
  30. {
  31. scanf("%d%d", &i_num, &af_num);
  32. if (!insert(head, i_num, af_num))
  33. {
  34. puts("Wrong!\n");
  35. exit(EXIT_FAILURE);
  36. }
  37. }
  38. scanf("%d", &del_num);
  39. dele_num(&head, del_num);
  40. show_list(head);
  41. free_list(&head);
  42. return 0;
  43. }
  44. bool initia(List * list, int num)
  45. {
  46. *list = (List) malloc(sizeof(Node));
  47. if (*list != NULL)
  48. {
  49. (*list)->n_num = num;
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool insert(List list, int ins_num, int num)
  55. {
  56. bool find = false;
  57. List temp = list;
  58. List new_node;
  59. while (temp != NULL)
  60. {
  61. if (temp->n_num == num)
  62. {
  63. find = true;
  64. break;
  65. }
  66. temp = temp->next;
  67. }
  68. if (find)
  69. {
  70. new_node = (List) malloc(sizeof(Node));
  71. if (new_node != NULL)
  72. {
  73. new_node->n_num = ins_num;
  74. new_node->next = temp->next;
  75. temp->next = new_node;
  76. }
  77. else
  78. return false;
  79. }
  80. else
  81. return false;
  82. return true;
  83. }
  84. void dele_num(List * list, int del_num)
  85. {
  86. List temp = *list;
  87. List pre;
  88. bool find;
  89. while (temp != NULL)
  90. {
  91. if (temp->n_num == del_num)
  92. {
  93. find = true;
  94. break;
  95. }
  96. pre = temp;
  97. temp = temp->next;
  98. }
  99. if (find)
  100. {
  101. if (temp == *list)
  102. {
  103. *list = (*list)->next;
  104. free(temp);
  105. }
  106. else
  107. {
  108. pre->next = temp->next;
  109. free(temp);
  110. }
  111. }
  112. }
  113. void show_list(List list)
  114. {
  115. while (list != NULL)
  116. {
  117. printf("%d ", list->n_num);
  118. list = list->next;
  119. }
  120. }
  121. void free_list(List * list)
  122. {
  123. List temp;
  124. while (*list != NULL)
  125. {
  126. temp = *list;
  127. *list = (*list)->next;
  128. free(temp);
  129. }
  130. }

  • HJ50 四则运算(栈,表达式求值,中缀转后缀与后缀转中缀算法的结合)

  1. 输入:
  2. 3+2*{1+2*[-4/(8-6)+7]}
  3. 输出:
  4. 25

示例代码:HJ50.c

  1. #include <stdio.h>
  2. #include <string.h> // 提供 strchr() 函数原型
  3. #include <ctype.h>
  4. #define LEN 1001 // 多一个给空字符
  5. // 对表达式字符串进行求值
  6. int get_value(char * st);
  7. // 对两个整数进行指定符号计算并返回
  8. int compute(int num1, int num2, char symbol);
  9. // 获取最多 n - 1 个字符
  10. char * s_gets(char * st, int n);
  11. int main(void)
  12. {
  13. char str[LEN]; // 获取保存的字符串
  14. if (s_gets(str, LEN) != NULL && *str != '\0')
  15. printf("%d", get_value(str));
  16. return 0;
  17. }
  18. int get_value(char * st)
  19. {
  20. char symbol_stack[LEN]; // 符号栈,用于保存符号
  21. int number_stack[LEN]; // 数字栈,用于保存数字
  22. int top_s = -1; // 符号栈顶标记为空栈
  23. int top_n = -1; // 数字栈顶标记为空栈
  24. int n1, n2; // 用于保存待计算的两个数字
  25. int temp; // 临时保存数字
  26. char * st_b = st; // 备份字符串始址
  27. while (*st) // 遍历字符串
  28. {
  29. // 左括号压入符号栈
  30. if (*st == '(' || *st == '[' || *st == '{')
  31. symbol_stack[++top_s] = *st;
  32. // 处理数字,先处理正数,防止重复处理数字,例如 -1*(-1-1)
  33. if (isdigit(*st))
  34. {
  35. temp = 0;
  36. while (isdigit(*st))
  37. temp = temp * 10 + *st++ - '0';
  38. // 处理到非数字字符时,回退一个字符
  39. --st;
  40. // 将正数入栈
  41. number_stack[++top_n] = temp;
  42. }
  43. // 处理负数或者减号
  44. if (*st == '-')
  45. {
  46. // 处理负数
  47. if (st == st_b || *(st - 1) == '(' || *(st - 1) == '[' || *(st - 1) == '{')
  48. {
  49. temp = 0;
  50. while (isdigit(*++st))
  51. temp = 10 * temp + *st - '0';
  52. // 负数入栈
  53. number_stack[++top_n] = 0 - temp;
  54. // 处理到非数字字符时,回退一个字符
  55. st--;
  56. }
  57. // 处理减号
  58. else
  59. {
  60. // 处理符号栈中优先级等于或高于 '-' 的运算符
  61. while (top_s >= 0 && (symbol_stack[top_s] == '+' ||
  62. symbol_stack[top_s] == '-' || symbol_stack[top_s] == '*' ||
  63. symbol_stack[top_s] == '/'))
  64. {
  65. /* 注意先出栈的作为第二个值在表达式中求值 */
  66. n2 = number_stack[top_n--];
  67. n1 = number_stack[top_n--];
  68. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  69. }
  70. // 将减号入栈
  71. symbol_stack[++top_s] = '-';
  72. }
  73. }
  74. // 处理正数或加号
  75. if (*st == '+')
  76. {
  77. if (st == st_b || *(st - 1) == '(' || *(st - 1) == '[' || *(st - 1) == '{')
  78. {
  79. // 不做处理
  80. }
  81. else
  82. {
  83. while (top_s >= 0 && (symbol_stack[top_s] == '+' ||
  84. symbol_stack[top_s] == '-' || symbol_stack[top_s] == '*' ||
  85. symbol_stack[top_s] == '/'))
  86. {
  87. n2 = number_stack[top_n--];
  88. n1 = number_stack[top_n--];
  89. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  90. }
  91. // 将加号入栈
  92. symbol_stack[++top_s] = '+';
  93. }
  94. }
  95. // 处理乘号
  96. if (*st == '*')
  97. {
  98. while (top_s >= 0 &&(symbol_stack[top_s] == '*' ||
  99. symbol_stack[top_s] == '/'))
  100. {
  101. n2 = number_stack[top_n--];
  102. n1 = number_stack[top_n--];
  103. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  104. }
  105. symbol_stack[++top_s] = '*';
  106. }
  107. // 处理除号
  108. if (*st == '/')
  109. {
  110. while (top_s >= 0 &&(symbol_stack[top_s] == '*' ||
  111. symbol_stack[top_s] == '/'))
  112. {
  113. n2 = number_stack[top_n--];
  114. n1 = number_stack[top_n--];
  115. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  116. }
  117. symbol_stack[++top_s] = '/';
  118. }
  119. // 处理 ) 括号
  120. if (*st == ')')
  121. {
  122. // 遇到左括号之前不断计算并压栈
  123. while (symbol_stack[top_s] != '(')
  124. {
  125. n2 = number_stack[top_n--];
  126. n1 = number_stack[top_n--];
  127. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  128. }
  129. // 遇到左括号,弹出栈
  130. top_s--;
  131. }
  132. // 处理 ] 括号
  133. if (*st == ']')
  134. {
  135. // 遇到左括号之前不断计算并压栈
  136. while (symbol_stack[top_s] != '[')
  137. {
  138. n2 = number_stack[top_n--];
  139. n1 = number_stack[top_n--];
  140. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  141. }
  142. // 遇到左括号,弹出栈
  143. top_s--;
  144. }
  145. // 处理 } 括号
  146. if (*st == '}')
  147. {
  148. // 遇到左括号之前不断计算并压栈
  149. while (symbol_stack[top_s] != '{')
  150. {
  151. n2 = number_stack[top_n--];
  152. n1 = number_stack[top_n--];
  153. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  154. }
  155. // 遇到左括号,弹出栈
  156. top_s--;
  157. }
  158. st++;
  159. }
  160. // 如果符号栈非空
  161. while (top_s >= 0)
  162. {
  163. n2 = number_stack[top_n--];
  164. n1 = number_stack[top_n--];
  165. number_stack[++top_n] = compute(n1, n2, symbol_stack[top_s--]);
  166. }
  167. return *number_stack;
  168. }
  169. int compute(int num1, int num2, char symbol)
  170. {
  171. switch (symbol)
  172. {
  173. case '+':
  174. return num1 + num2;
  175. case '-':
  176. return num1 - num2;
  177. case '*':
  178. return num1 * num2;
  179. case '/':
  180. return num1 / num2;
  181. default:
  182. return -1;
  183. }
  184. }
  185. char * s_gets(char * st, int n)
  186. {
  187. char * ret_val;
  188. char * find;
  189. ret_val = fgets(st, n, stdin);
  190. if (ret_val)
  191. {
  192. find = strchr(st, '\n');
  193. if (find)
  194. *find = '\0';
  195. else
  196. while (getchar() != '\n')
  197. continue;
  198. }
  199. return ret_val;
  200. }

  • HJ51 输出单向链表中倒数第k个结点(链表,查找)

  1. 输入:
  2. 8
  3. 1 2 3 4 5 6 7 8
  4. 4
  5. 输出:
  6. 5

示例代码:HJ51.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct ListNode {
  4. int m_nKey;
  5. struct ListNode * m_pNext;
  6. };
  7. typedef struct ListNode * List;
  8. List find_k(List list, int k);
  9. int main(void)
  10. {
  11. int n;
  12. int i;
  13. List head = NULL, rail, pre;
  14. List key;
  15. int k;
  16. while (scanf("%d", &n) == 1)
  17. {
  18. for (i = 0; i < n; i++)
  19. {
  20. rail = (List) malloc(sizeof(struct ListNode));
  21. rail->m_pNext = NULL;
  22. scanf("%d", &rail->m_nKey);
  23. if (head == NULL)
  24. {
  25. head = rail;
  26. pre = head;
  27. }
  28. else
  29. {
  30. pre->m_pNext = rail;
  31. pre = rail;
  32. }
  33. }
  34. scanf("%d", &k);
  35. key = find_k(head, k);
  36. if (key)
  37. printf("%d\n", key->m_nKey);
  38. while (head)
  39. {
  40. key = head;
  41. head = head->m_pNext;
  42. free(key);
  43. }
  44. }
  45. return 0;
  46. }
  47. List find_k(List list, int k)
  48. {
  49. List fast, low;
  50. if (list == NULL)
  51. return NULL;
  52. fast = low = list;
  53. while (fast != NULL && k > 1)
  54. {
  55. fast = fast->m_pNext;
  56. k--;
  57. }
  58. if (fast == NULL)
  59. return NULL;
  60. while (fast->m_pNext)
  61. {
  62. low = low->m_pNext;
  63. fast = fast->m_pNext;
  64. }
  65. return low;
  66. }

  • HJ52 计算字符串的编辑距离(字符串,动态规划)

  1. 输入:
  2. abcdefg
  3. abcdef
  4. 输出:
  5. 1

示例代码:HJ52.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. int min(int a, int b, int c);
  5. void d_p(char * str1, char * str2);
  6. int main(void)
  7. {
  8. char str1[LEN],str2[LEN];
  9. while (scanf("%s%s", str1, str2) == 2)
  10. d_p(str1, str2);
  11. return 0;
  12. }
  13. int min(int a, int b, int c)
  14. {
  15. a = (a < b) ? a : b;
  16. a = (a < c) ? a : c;
  17. return a;
  18. }
  19. void d_p(char * str1, char * str2)
  20. {
  21. int i, j;
  22. int len1 = strlen(str1);
  23. int len2 = strlen(str2);
  24. int dp[len1 + 1][len2 + 1];
  25. dp[0][0] = 0; // 初始化,若两个字符都为空
  26. for (i = 1; i< len1 + 1; i++) // 初始化,若第 2 个字符串为空
  27. dp[i][0] = i;
  28. for (i = 1; i< len2 + 1; i++) // 初始化,若第 1 个字符串为空
  29. dp[0][i] = i;
  30. for (i = 1; i < len1 + 1; i++) // 1 ~ len1 个字符
  31. {
  32. for (j = 1; j < len2 + 1; j++) // 1 ~ len2 个字符
  33. {
  34. /* 如果相等则不变 */
  35. if (str1[i - 1] == str2[j - 1])
  36. dp[i][j] = dp[i - 1][j - 1];
  37. else /* 三种情况:1.修改
  38. 2.第 2 个字符串增加 1 个字符或 第 1 个字符串删去 1 个字符
  39. 3.第 1 个字符串增加 1 个字符或 第 2 个字符串删去 1 个字符 */
  40. dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i][j - 1] + 1, dp[i - 1][j] + 1);
  41. }
  42. }
  43. printf("%d \n", dp[len1][len2]);
  44. }

  • HJ53 杨辉三角的变形(基础数学,总结规律,指数级增长,不能硬算)

  1. 输入:4
  2. 输出:3

示例代码:HJ53.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int num;
  5. int pos;
  6. while (scanf("%d", &num) == 1)
  7. {
  8. if (num < 3)
  9. pos = -1;
  10. else if (num % 2 == 1)
  11. pos = 2;
  12. else if (num % 4 == 0)
  13. pos = 3;
  14. else if (num % 4 == 2)
  15. pos = 4;
  16. printf("%d", pos);
  17. }
  18. return 0;
  19. }

  • HJ54 表达式求值(栈,表达式求值,该题与HJ50要求一致,但测试用例更加严格)

  • HJ55 挑7(穷举,基础数学)

  1. 输入:20
  2. 输出:3
  3. 说明:
  4. 输入20120之间有关的数字包括7,14,173个。

示例代码:HJ55.c

  1. #include <stdio.h>
  2. void find_seven(int n);
  3. int main(void)
  4. {
  5. int num;
  6. while (scanf("%d", &num) == 1)
  7. find_seven(num);
  8. return 0;
  9. }
  10. void find_seven(int n)
  11. {
  12. int i;
  13. int count = 0;
  14. for (i = 1; i <= n; i++)
  15. {
  16. /* 判断是否是 7 的倍数,个位是否含 7 ,10 位是否含7,百位、千位、万位是否含 7 */
  17. if (i % 7 == 0 || i % 10 == 7 || (i / 10) % 10 == 7 || (i / 100) % 10 == 7 ||
  18. (i / 1000) % 10 == 7)
  19. count++;
  20. }
  21. printf("%d\n", count);
  22. }

  • HJ56 完全数计算(穷举)

  1. 输入:1000
  2. 输出:3

示例代码:HJ56.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int num;
  5. int count = 0, total = 0;
  6. int i, j;
  7. scanf("%d", &num);
  8. for (i = 1; i <= num; i++)
  9. {
  10. for (j = 1; j <= i / 2; j++)
  11. {
  12. if (i % j == 0)
  13. total += j;
  14. }
  15. if (total == i)
  16. count++;
  17. total = 0;
  18. }
  19. printf("%d", count);
  20. return 0;
  21. }

  • HJ57 高精度整数加法(字符串,栈)

  1. 输入:
  2. 9876543210
  3. 1234567890
  4. 输出:
  5. 11111111100

示例代码:HJ57.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 10001
  4. int main(void)
  5. {
  6. char st_1[LEN], st_2[LEN]; // 常量不能改变地址
  7. int i, j;
  8. int len1, len2;
  9. int remain = 0, now;
  10. int stack[LEN];
  11. char s_temp[LEN];
  12. int i_temp;
  13. int top = -1;
  14. while (scanf("%s%s", st_1, st_2) == 2)
  15. {
  16. len1 = strlen(st_1);
  17. len2 = strlen(st_2);
  18. if (len1 < len2) /* 使 len1 >= len2 */
  19. {
  20. strcpy(s_temp, st_1);
  21. strcpy(st_1, st_2);
  22. strcpy(st_2, s_temp);
  23. i_temp = len1;
  24. len1 = len2;
  25. len2 = i_temp;
  26. }
  27. for (i = len1 - 1, j = len2 - 1; i > -1; i--, j--)
  28. {
  29. if (j > -1)
  30. now = (st_1[i] - '0') + (st_2[j] - '0');
  31. else
  32. now = (st_1[i] - '0');
  33. if (now + remain < 10)
  34. {
  35. stack[++top] = now + remain;
  36. remain = 0;
  37. }
  38. else
  39. {
  40. stack[++top] = now + remain - 10 ;
  41. remain = 1;
  42. }
  43. }
  44. if (remain == 1)
  45. stack[++top] = 1;
  46. for (i = top; i >= 0; i--)
  47. printf("%d", stack[i]);
  48. }
  49. return 0;
  50. }

  • HJ58 输入n个整数,输出其中最小的k个(基础数学)

  1. 输入:
  2. 5 2
  3. 1 3 5 7 2
  4. 输出:
  5. 1 2

示例代码:HJ58.c

  1. #include <stdio.h>
  2. void get_n_s(int ar[], int n, int k);
  3. int main(void)
  4. {
  5. int n, k;
  6. int i;
  7. scanf("%d%d", &n, &k);
  8. int ar[n];
  9. for (i = 0; i < n; i++)
  10. scanf("%d", ar + i);
  11. get_n_s(ar, n, k);
  12. return 0;
  13. }
  14. void get_n_s(int ar[], int n, int k)
  15. {
  16. int i, j;
  17. int temp;
  18. for (i = 0; i < k; i++)
  19. {
  20. for (j = i + 1; j < n; j++)
  21. {
  22. if (ar[i] > ar[j])
  23. {
  24. temp = ar[i];
  25. ar[i] = ar[j];
  26. ar[j] = temp;
  27. }
  28. }
  29. }
  30. for (i = 0; i < k; i++)
  31. printf("%d ", ar[i]);
  32. }

  • HJ59 找出字符串中第一个只出现一次的字符(字符串)

  1. 输入:asdfasdfo
  2. 输出:o

示例代码:HJ59.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 1001
  5. #define SIZE 256
  6. int main(void)
  7. {
  8. char ch;
  9. static int stat[SIZE];
  10. static char str[LEN];
  11. int len = 0;
  12. int find = 0;
  13. int i;
  14. while ((ch = getchar()) != '\n')
  15. {
  16. str[len++] = ch;
  17. stat[toascii(ch)]++;
  18. }
  19. /* 第一次出现的位置,而不是 ASCII码中第一次出现的位置 */
  20. for (i = 0; i < len; i++)
  21. {
  22. if (stat[toascii(str[i])] == 1)
  23. {
  24. putchar(str[i]);
  25. find = 1;
  26. break;
  27. }
  28. }
  29. if (!find)
  30. printf("-1");
  31. return 0;
  32. }

  • HJ60 查找组成一个偶数最接近的两个素数

  1. 输入:
  2. 20
  3. 输出:
  4. 7
  5. 13

示例代码:HJ60.c

  1. #include <stdio.h>
  2. int is_num(int n);
  3. int main(void)
  4. {
  5. int num;
  6. int i;
  7. scanf("%d", &num);
  8. for (i = 0; i < num / 2 - 2; i++)
  9. {
  10. if (is_num(num / 2 + i) && is_num(num / 2 - i))
  11. break;
  12. }
  13. printf("%d\n%d\n", num / 2 - i, num / 2 + i);
  14. return 0;
  15. }
  16. int is_num(int n)
  17. {
  18. int i;
  19. int count = 0;
  20. for (i = 2; i <= n; i++)
  21. if (n % i == 0)
  22. count++;
  23. if (count == 1)
  24. return count;
  25. return 0;
  26. }

  • HJ61 放苹果(动态规划,递归)

  1. 输入:
  2. 7 3
  3. 输出:
  4. 8

示例代码:HJ61.c

  1. #include <stdio.h>
  2. int get_count1(int m, int n); // 方法一:DP
  3. int get_count2(int m, int n); // 方法二:REVERSE
  4. int main(void)
  5. {
  6. int apple, plate;
  7. scanf("%d%d", &apple, &plate);
  8. printf("%d", get_count2(apple, plate));
  9. return 0;
  10. }
  11. /* 方法一:使用动态规划算法 */
  12. int get_count(int m, int n)
  13. {
  14. int i, j;
  15. int dp[m + 1][n + 1];
  16. /* i 个苹果放在 j 个盘子中 */
  17. for (i = 0; i <= m; i++)
  18. {
  19. for (j = 1; j <= n; j++)
  20. {
  21. if (i == 0 || i == 1 || j == 1)
  22. dp[i][j] = 1;
  23. else if (i < j)
  24. /* 有多余的盘子,题目要求不能右重复的序列 */
  25. dp[i][j] = dp[i][i];
  26. else
  27. /* 两种方案:1. 至少有一个盘子没放苹果
  28. 2. 所有盘子都至少有一个苹果 */
  29. dp[i][j] = dp[i][j - 1] + dp[i - j][j];
  30. }
  31. }
  32. return dp[m][n];
  33. }
  34. /* 方法二:使用递归算法 */
  35. int get_count2(int m, int n)
  36. {
  37. if (m == 0 || m == 1 || n == 1)
  38. return 1;
  39. if (m < n)
  40. return get_count2(m, m);
  41. else
  42. return get_count2(m, n - 1) + get_count2(m - n, n);
  43. }

  • HJ62 查找输入整数二进制中1的个数(位运算)

  1. 输入:5
  2. 输出:2
  3. 说明:5 的二进制表示是 101,有 21

示例代码:HJ62.c

  1. #include <stdio.h>
  2. int get_binary(int n);
  3. int main(void)
  4. {
  5. int num;
  6. while (scanf("%d", &num) == 1)
  7. printf("%d\n", get_binary(num));
  8. return 0;
  9. }
  10. int get_binary(int n)
  11. {
  12. int count = 0;
  13. while (n)
  14. {
  15. count += (n & 1);
  16. n >>= 1;
  17. }
  18. return count;
  19. }

  • HJ63 DNA序列(字符串)

  1. 输入:
  2. ACGT
  3. 2
  4. 输出:
  5. CG
  6. 说明:
  7. ACGT长度为2的子串有AC,CG,GT3个,其中AC和GT2个的GC-Ratio都为0.5,CG为1,故输出CG
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. int main(void)
  5. {
  6. char str[LEN];
  7. int len;
  8. int size;
  9. int i, j;
  10. int max = 0;
  11. int count = 0;
  12. int id = 0;
  13. scanf("%s", str);
  14. scanf("%d", &size);
  15. len = (int) strlen(str);
  16. for (i = 0; i < len - size + 1; i++)
  17. {
  18. for (j = i; j < i + size; j++)
  19. {
  20. if(str[j] == 'G' || str[j] == 'C')
  21. count++;
  22. }
  23. if (count > max)
  24. {
  25. max = count;
  26. id = i;
  27. }
  28. count = 0;
  29. }
  30. for (i = 0; i < size; i++)
  31. putchar(*(str + id + i));
  32. return 0;
  33. }

  • HJ64 MP3光标位置(字符串)

  1. 输入:
  2. 10
  3. UUUU
  4. 输出:
  5. 7 8 9 10
  6. 7

示例代码:HJ64.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void show_mus(int pos, int value, int n);
  4. int main(void)
  5. {
  6. int n;
  7. int i;
  8. int * ar;
  9. char ch;
  10. int stat = 0; // 初始位置
  11. int stat_value;
  12. while (scanf("%d", &n) == 1)
  13. {
  14. ar = (int *) malloc(sizeof(int) * n);
  15. for (i = 0; i < n; i++)
  16. ar[i] = i + 1;
  17. getchar();
  18. stat_value = ar[0];
  19. while ((ch = getchar()) != '\n')
  20. {
  21. switch (ch)
  22. {
  23. case 'U':
  24. if (stat_value == ar[0])
  25. {
  26. if (n <= 4)
  27. stat = n - 1;
  28. else
  29. stat = 3;
  30. stat_value = ar[n - 1];
  31. }
  32. else
  33. {
  34. stat--;
  35. if (stat < 0)
  36. stat = 0;
  37. stat_value--;
  38. }
  39. break;
  40. case 'D':
  41. if (stat_value == ar[n - 1])
  42. {
  43. stat = 0;
  44. stat_value = ar[0];
  45. }
  46. else
  47. {
  48. stat++;
  49. if (stat > 3)
  50. stat = 3;
  51. stat_value++;
  52. }
  53. break;
  54. default :
  55. puts("Error!\n");
  56. }
  57. }
  58. show_mus(stat, stat_value, n);
  59. }
  60. free(ar);
  61. return 0;
  62. }
  63. void show_mus(int pos, int value, int n)
  64. {
  65. int i;
  66. switch (pos)
  67. {
  68. case 0:
  69. for (i = 0; i < 4 && value + i <= n; i++)
  70. printf("%d ", value + i);
  71. break;
  72. case 1:
  73. printf("%d ", value - 1);
  74. for (i = 0; i < 3 && value + i <= n; i++)
  75. printf("%d ", value + i);
  76. break;
  77. case 2:
  78. printf("%d %d ", value - 2, value - 1);
  79. for (i = 0; i < 2 && value + i <= n; i++)
  80. printf("%d ", value + i);
  81. break;
  82. case 3:
  83. printf("%d %d %d ", value - 3, value - 2, value - 1);
  84. for (i = 0; i < 1 && value + i <= n; i++)
  85. printf("%d ", value + i);
  86. break;
  87. }
  88. printf("\n%d\n", value);
  89. }

  • HJ65 查找两个字符串a,b中的最长公共子串(字符串)

  1. 输入:
  2. abcdefghijklmnop
  3. abcsafjklmnopqrstuvw
  4. 输出:
  5. jklmnop

示例代码 1:HJ65.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 1001
  5. char * s_gets(char * st, int n);
  6. void cut_str(char * dest, char * start, char * end);
  7. bool is_include(char * st_1, char * st_2);
  8. void get_max(char * st_1, char * st_2, char * target);
  9. int main(void)
  10. {
  11. char st_1[LEN];
  12. char st_2[LEN];
  13. char target[LEN];
  14. while (s_gets(st_1, LEN) != NULL && s_gets(st_2, LEN) != NULL &&
  15. *st_1 != '\0' && *st_2 != '\0')
  16. {
  17. get_max(st_1, st_2, target);
  18. printf("%s\n", target);
  19. }
  20. return 0;
  21. }
  22. char * s_gets(char * st, int n)
  23. {
  24. char * ret_val;
  25. char * find;
  26. ret_val = fgets(st, n, stdin);
  27. if (ret_val)
  28. {
  29. find = strchr(st, '\n');
  30. if (find)
  31. *find = '\0';
  32. else
  33. while (getchar() != '\n')
  34. continue;
  35. }
  36. return ret_val;
  37. }
  38. void cut_str(char * dest, char * start, char * end)
  39. {
  40. int i;
  41. for (i = 0; i <= end - start; i++)
  42. dest[i] = *(start + i);
  43. dest[i] = '\0';
  44. }
  45. bool is_include(char * st_1, char * st_2)
  46. {
  47. int i, j;
  48. int len_1 = strlen(st_1);
  49. int len_2 = strlen(st_2);
  50. int count = 0;
  51. for (i = 0; i < len_1; i++)
  52. {
  53. for (j = count; j < len_2; j++)
  54. {
  55. if (st_2[j] == st_1[i])
  56. {
  57. count++;
  58. if (count == len_2)
  59. return true;
  60. break;
  61. }
  62. else
  63. {
  64. if (count > 0)
  65. {
  66. i-= count;
  67. count = 0;
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. void get_max(char * st_1, char * st_2, char * target)
  76. {
  77. char dest[LEN];
  78. int len_1 = strlen(st_1);
  79. int len_2 = strlen(st_2);
  80. int i, j;
  81. int time;
  82. char s_temp[LEN];
  83. if (len_1 < len_2)
  84. {
  85. strcpy(s_temp, st_1);
  86. strcpy(st_1, st_2);
  87. strcpy(st_2, s_temp);
  88. len_1 = strlen(st_1);
  89. len_2 = strlen(st_2);
  90. }
  91. for (i = len_2; i >= 1; i--)
  92. {
  93. time = len_2 - i + 1;
  94. for (j = 0; j < time; j++)
  95. {
  96. cut_str(dest, st_2 + j, st_2 + i + j - 1);
  97. if (is_include(st_1, dest))
  98. {
  99. strcpy(target, dest);
  100. return;
  101. }
  102. }
  103. }
  104. }

示例代码 2,HJ65.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. int main(void)
  5. {
  6. char str1[LEN];
  7. char str2[LEN];
  8. char s_temp[LEN];
  9. char * s_max;
  10. int max;
  11. int len1, len2;
  12. int count;
  13. int i, j;
  14. /* 注意是在较短字符串中先出现,因此将较短字符串作为第一层循环 */
  15. while (scanf("%s%s", str1, str2) != EOF)
  16. {
  17. max = 0;
  18. len1 = strlen(str1);
  19. len2 = strlen(str2);
  20. if (len1 > len2)
  21. {
  22. strcpy(s_temp, str1);
  23. strcpy(str1, str2);
  24. strcpy(str2, s_temp);
  25. len1 = strlen(str1);
  26. len2 = strlen(str2);
  27. }
  28. for (i = 0; i < len1; i++)
  29. {
  30. for (j = 0; j < len2; j++)
  31. {
  32. count = 0;
  33. while (str1[i + count] == str2[j + count] && str1[i + count] != '\0')
  34. count++;
  35. if (count > max)
  36. {
  37. max = count;
  38. s_max = str1 + i;
  39. }
  40. }
  41. }
  42. if (max)
  43. {
  44. for (i = 0; i < max; i++)
  45. putchar(*(s_max + i));
  46. }
  47. }
  48. return 0;
  49. }

  • HJ66 配置文件恢复

  1. 输入:
  2. reset
  3. reset board
  4. board add
  5. board delet
  6. reboot backplane
  7. backplane abort
  8. 输出:
  9. reset what
  10. board fault
  11. where to add
  12. no board at all
  13. impossible
  14. install first

示例代码:HJ66.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 21
  5. #define N 2
  6. typedef struct {
  7. char order[N][LEN];
  8. } Order;
  9. int main(void)
  10. {
  11. char input[LEN];
  12. Order com[6] = { { {"reset", ""} }, { {"reset", "board"} }, { {"board", "add"} },
  13. { {"board", "delete"} }, { {"reboot", "backplane"} }, { {"backplane", "abort"} } };
  14. char respond[7][LEN] = { "reset what", "board fault", "where to add", "no board at all",
  15. "impossible", "install first", "unknown command" };
  16. int order_words = 0;
  17. int order_match = 0;
  18. int i;
  19. char * order_1;
  20. char * order_2;
  21. int len1, len2;
  22. int pre;
  23. char ch1, ch2;
  24. char * temp;
  25. while (fgets(input, LEN, stdin) != NULL && *input != '\n')
  26. {
  27. if ((temp = strchr(input, '\n')))
  28. *temp = '\0';
  29. order_words = strchr(input, ' ') == NULL ? 1 : 2;
  30. if (order_words == 1)
  31. {
  32. len1 = strlen(input);
  33. if (len1 <= (int) strlen(com[0].order[0]))
  34. {
  35. ch1 = *(com[0].order[0] + len1);
  36. *(com[0].order[0] + len1) = '\0';
  37. if (strstr(com[0].order[0], input))
  38. {
  39. order_match = 1;
  40. pre = 0;
  41. }
  42. *(com[0].order[0] + len1) = ch1;
  43. }
  44. }
  45. else
  46. {
  47. order_1 = strtok(input, " ");
  48. order_2 = strtok(NULL, " ");
  49. len1 = strlen(order_1);
  50. len2 = strlen(order_2);
  51. for (i = 1; i < 6; i++)
  52. {
  53. if (len1 <= (int) strlen(com[i].order[0]) && len2 <= (int) strlen(com[i].order[1]))
  54. {
  55. ch1 = *(com[i].order[0] + len1);
  56. ch2 = *(com[i].order[1] + len2);
  57. *(com[i].order[0] + len1) = '\0';
  58. *(com[i].order[1] + len2) = '\0';
  59. if (strstr(com[i].order[0], order_1) && strstr(com[i].order[1], order_2))
  60. {
  61. order_match++;
  62. pre = i;
  63. }
  64. *(com[i].order[0] + len1) = ch1;
  65. *(com[i].order[1] + len2) = ch2;
  66. }
  67. }
  68. }
  69. if (order_match == 1)
  70. puts(respond[pre]);
  71. else
  72. puts(respond[6]);
  73. order_match = 0;
  74. }
  75. return 0;
  76. }

  • HJ67 24点游戏算法(DFS)

  1. 输入:7 2 1 10
  2. 输出:true

示例代码:HJ67.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define LEN 4
  4. bool DFS(float * ar, float f_num, bool * visa);
  5. int main(void)
  6. {
  7. float ar[LEN];
  8. bool visa[LEN] = { false }; // 初始化为未使用
  9. bool find = false;
  10. int i;
  11. for(i = 0; i < LEN; i++) // 读入数据
  12. scanf("%f", ar + i);
  13. for (i = 0; i < LEN; i++)
  14. {
  15. visa[i] = true;
  16. if (DFS(ar, ar[i], visa))
  17. {
  18. puts("true");
  19. find = true;
  20. break;
  21. }
  22. visa[i] = false;
  23. }
  24. if (!find)
  25. puts("false");
  26. return 0;
  27. }
  28. bool DFS(float * ar, float f_num, bool * visa)
  29. {
  30. int i;
  31. if(f_num == 24) // 递归出口
  32. return true;
  33. for(i = 0; i < 4; i++)
  34. {
  35. if(!visa[i])
  36. {
  37. visa[i] = true; // 标记已访问, 注意由于题目并不要求,未考虑除 0 的情况
  38. if(DFS(ar, f_num + ar[i], visa) || DFS(ar, f_num - ar[i], visa)
  39. || DFS(ar, f_num * ar[i], visa) || DFS(ar, f_num / ar[i], visa))
  40. return true;
  41. visa[i] = false; // 未找到则回退
  42. }
  43. }
  44. return false;
  45. }

  • HJ68 成绩排序

  1. 输入:
  2. 3
  3. 0
  4. fang 90
  5. yang 50
  6. ning 70
  7. 输出:
  8. fang 90
  9. ning 70
  10. yang 50

示例代码 1(不推荐):HJ68.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 20
  5. typedef struct {
  6. char name[SIZE];
  7. int score;
  8. } Student;
  9. typedef Student * Ps;
  10. void sort(Ps student, int n, int choose);
  11. void show(Ps student, Ps b_student, int n, int choose);
  12. int main(void)
  13. {
  14. int n;
  15. int choose;
  16. Ps student;
  17. Ps b_student;
  18. int i;
  19. while (scanf("%d%d", &n, &choose) != EOF)
  20. {
  21. student = (Ps) malloc(sizeof(Student) * n);
  22. b_student = (Ps) malloc(sizeof(Student) * n);
  23. for (i = 0; i < n; i++)
  24. {
  25. scanf("%s", (student + i)->name);
  26. scanf("%d", &(student + i)->score);
  27. strcpy((b_student + i)->name, (student + i)->name);
  28. (b_student + i)->score = (student + i)->score;
  29. }
  30. sort(student, n, choose);
  31. show(student, b_student, n, choose);
  32. }
  33. free(student);
  34. free(b_student);
  35. return 0;
  36. }
  37. void sort(Ps student, int n, int choose)
  38. {
  39. char s_temp[SIZE];
  40. int i_temp;
  41. int i, j;
  42. for (i = 0; i < n - 1; i++)
  43. {
  44. for (j = i + 1; j < n; j++)
  45. {
  46. if (choose == 0)
  47. {
  48. if ((student + i)->score < (student + j)->score)
  49. {
  50. i_temp = (student + i)->score;
  51. (student + i)->score = (student + j)->score;
  52. (student + j)->score = i_temp;
  53. strcpy(s_temp, (student + i)->name);
  54. strcpy((student + i)->name, (student + j)->name);
  55. strcpy((student +j)->name, s_temp);
  56. }
  57. }
  58. else if (choose == 1)
  59. {
  60. if ((student + i)->score > (student + j)->score)
  61. {
  62. i_temp = (student + i)->score;
  63. (student + i)->score = (student + j)->score;
  64. (student + j)->score = i_temp;
  65. strcpy(s_temp, (student + i)->name);
  66. strcpy((student + i)->name, (student + j)->name);
  67. strcpy((student +j)->name, s_temp);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. void show(Ps student, Ps b_student, int n, int choose)
  74. {
  75. int i, j;
  76. int count = 0;
  77. int pre = 100;
  78. int pre2 = 0;
  79. int s_count;
  80. /* 从小到小 */
  81. if (choose == 0)
  82. {
  83. for (i = 0; i < n; i++)
  84. {
  85. if (i < n - 1)
  86. {
  87. if ((student + i)->score > (student + i + 1)->score && (student + i)->score < pre)
  88. {
  89. pre = (student + i)->score;
  90. printf("%s %d\n", (student + i)->name, (student + i)->score);
  91. count = 0;
  92. }
  93. else
  94. {
  95. if ((student + i)->score != pre)
  96. count = 0;
  97. count++;
  98. s_count = 0;
  99. for (j = 0; j < n; j++)
  100. {
  101. if ((student + i)->score == (b_student + j)->score)
  102. s_count++;
  103. if (s_count == count)
  104. {
  105. printf("%s %d\n", (b_student + j)->name, (b_student + j)->score);
  106. pre = (b_student + j)->score;
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. else
  113. {
  114. if ((student + i)->score < pre)
  115. count = 0;
  116. if (count == 0)
  117. printf("%s %d\n", (student + i)->name, (student + i)->score);
  118. else
  119. {
  120. s_count = 0;
  121. for (j = 0; j < n; j++)
  122. {
  123. if ((student + i)->score == (b_student + j)->score)
  124. s_count++;
  125. if (s_count == count + 1)
  126. {
  127. printf("%s %d\n", (b_student + j)->name, (b_student + j)->score);
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. /* 从小到大 */
  136. if (choose == 1)
  137. {
  138. for (i = 0; i < n; i++)
  139. {
  140. if (i < n - 1)
  141. {
  142. if ((student + i)->score < (student + i + 1)->score && (student + i)->score > pre2)
  143. {
  144. pre2 = (student + i)->score;
  145. printf("%s %d\n", (student + i)->name, (student + i)->score);
  146. count = 0;
  147. }
  148. else
  149. {
  150. if ((student + i)->score != pre2)
  151. count = 0;
  152. count++;
  153. s_count = 0;
  154. for (j = 0; j < n; j++)
  155. {
  156. if ((student + i)->score == (b_student + j)->score)
  157. s_count++;
  158. if (s_count == count)
  159. {
  160. printf("%s %d\n", (b_student + j)->name, (b_student + j)->score);
  161. pre2 = (b_student + j)->score;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. else
  168. {
  169. if ((student + i)->score > pre2)
  170. count = 0;
  171. if (count == 0)
  172. printf("%s %d\n", (student + i)->name, (student + i)->score);
  173. else
  174. {
  175. s_count = 0;
  176. for (j = 0; j < n; j++)
  177. {
  178. if ((student + i)->score == (b_student + j)->score)
  179. s_count++;
  180. if (s_count == count + 1)
  181. {
  182. printf("%s %d\n", (b_student + j)->name, (b_student + j)->score);
  183. break;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }

示例代码 2:HJ68.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 20
  4. typedef struct {
  5. char name[SIZE];
  6. int score;
  7. int id;
  8. } Student;
  9. typedef Student * Pst;
  10. int BSCompare(const void * p1, const void * p2);
  11. int SBCompare(const void * p1, const void * p2);
  12. void showInfo(Student * student, int n);
  13. int main(void)
  14. {
  15. int n; // 学生数量
  16. int choose; // 排序方式
  17. Pst student; // 存储学生信息
  18. int i;
  19. while (scanf("%d%d", &n, &choose) != EOF)
  20. {
  21. student = (Pst) malloc(sizeof(Student) * n);
  22. for (i = 0; i < n; i++)
  23. {
  24. scanf("%s", (student + i)->name);
  25. scanf("%d", &(student + i)->score);
  26. (student + i)->id = i;
  27. }
  28. if (choose == 0)
  29. qsort(student, n, sizeof(Student), BSCompare);
  30. if (choose == 1)
  31. qsort(student, n, sizeof(Student), SBCompare);
  32. showInfo(student, n);
  33. }
  34. free(student);
  35. return 0;
  36. }
  37. int BSCompare(const void * p1, const void * p2)
  38. {
  39. const Pst ps1 = (Pst) p1;
  40. const Pst ps2 = (Pst) p2;
  41. if (ps1->score != ps2->score)
  42. return ps2->score - ps1->score;
  43. else
  44. return ps1->id - ps2->id;
  45. }
  46. /* 如果返回负数,则 p1 所指排在左边 */
  47. int SBCompare(const void * p1, const void * p2)
  48. {
  49. const Pst ps1 = (Pst) p1;
  50. const Pst ps2 = (Pst) p2;
  51. if (ps1->score != ps2->score)
  52. return ps1->score - ps2->score;
  53. else
  54. return ps1->id - ps2->id;
  55. }
  56. void showInfo(Student * student, int n)
  57. {
  58. int i;
  59. for (i = 0; i < n; i++)
  60. printf("%s %d\n", (student + i)->name, (student + i)->score);
  61. }

  • HJ69 矩阵乘法(基础数学,线性代数,矩阵)

  1. 输入:
  2. 2
  3. 3
  4. 2
  5. 1 2 3
  6. 3 2 1
  7. 1 2
  8. 2 1
  9. 3 3
  10. 输出:
  11. 14 13
  12. 10 11

示例代码:HJ69.c

  1. #include <stdio.h>
  2. void get_matrix(int row, int col, int m[][col]);
  3. void matrix_mult(int m, int n, int s, int a[][n], int b[][s], int c[][s]);
  4. void show_matrix(int row, int col, int m[][col]);
  5. int main(void)
  6. {
  7. int m, n, s; // m x n * n x s = m x s
  8. scanf("%d%d%d", &m, &n, &s);
  9. int a[m][n], b[n][s], c[m][s];
  10. get_matrix(m, n, a);
  11. get_matrix(n, s, b);
  12. matrix_mult(m, n, s, a, b, c);
  13. show_matrix(m, s, c);
  14. return 0;
  15. }
  16. void get_matrix(int row, int col, int m[][col])
  17. {
  18. int i, j;
  19. for (i = 0; i < row; i++)
  20. {
  21. for (j = 0; j < col; j++)
  22. scanf("%d", *(m + i) + j);
  23. }
  24. }
  25. void matrix_mult(int m, int n, int s, int a[][n], int b[][s], int c[][s])
  26. {
  27. int i, j, index;
  28. int sum;
  29. for (i = 0; i < m; i++)
  30. {
  31. for (j = 0; j < s; j++)
  32. {
  33. sum = 0;
  34. for (index = 0; index < n; index++)
  35. sum += a[i][index] * b[index][j];
  36. c[i][j] = sum;
  37. }
  38. }
  39. }
  40. void show_matrix(int row, int col, int m[][col])
  41. {
  42. int i, j;
  43. for (i = 0; i < row; i++)
  44. {
  45. for (j = 0; j < col; j++)
  46. printf("%d ", m[i][j]);
  47. putchar('\n');
  48. }
  49. }

  • HJ70 矩阵乘法计算量估算(基础数学,线性代数,矩阵)

  1. 输入:
  2. 3
  3. 50 10
  4. 10 20
  5. 20 5
  6. (A(BC))
  7. 输出:
  8. 3500

示例代码:HJ70.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 1001
  5. #define N 2
  6. int get_num(int m, int n, int s);
  7. int get_total(int arr[][N], char * st);
  8. int main(void)
  9. {
  10. int lists;
  11. int i;
  12. char str[LEN];
  13. scanf("%d", &lists);
  14. int arr[lists][N];
  15. for (i = 0; i < lists; i++)
  16. scanf("%d%d", *(arr + i), *(arr + i) + 1);
  17. scanf("%s", str);
  18. printf("%d", get_total(arr, str));
  19. return 0;
  20. }
  21. int get_num(int m, int n, int s)
  22. {
  23. return m * n * s;
  24. }
  25. int get_total(int arr[][N], char * st)
  26. {
  27. int number_stack[LEN];
  28. int top_n = -1;
  29. int count = 0;
  30. int total = 0;
  31. int m, s;
  32. while (*st)
  33. {
  34. if (isupper(*st))
  35. {
  36. number_stack[++top_n] = count;
  37. count++;
  38. }
  39. if (*st == ')')
  40. {
  41. m = number_stack[top_n--];
  42. s = number_stack[top_n];
  43. total += get_num(arr[m][0], arr[m][1], arr[s][0]);
  44. arr[s][1] = arr[m][1];
  45. }
  46. st++;
  47. }
  48. return total;
  49. }

  • HJ71 字符串通配符(字符串)

  1. 输入:
  2. te?t*.*
  3. txt12.xls
  4. 输出:
  5. false

示例代码:HJ71.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #define LEN 101
  6. bool judge(char * st_t, char * st_s);
  7. int main(void)
  8. {
  9. char st_t[LEN];
  10. char st_s[LEN];
  11. while (scanf("%s%s", st_t, st_s) == 2)
  12. printf("%s\n", judge(st_t, st_s) ? "true" : "false");
  13. return 0;
  14. }
  15. /* 重点是消耗完待匹配字符串后也要消耗完通配符 */
  16. bool judge(char * st_t, char * st_s)
  17. {
  18. bool in_supper = false;
  19. char * t_b, * s_b;
  20. while (*st_s)
  21. {
  22. if (*st_t == '*')
  23. {
  24. t_b = st_t;
  25. st_t++;
  26. in_supper = true;
  27. s_b = st_s;
  28. }
  29. else if(tolower(*st_s) == tolower(*st_t) || (*st_t == '?' && (isalpha(*st_s) || isdigit(*st_s))))
  30. {
  31. st_t++;
  32. st_s++;
  33. }
  34. else if (in_supper)
  35. {
  36. st_s = s_b + 1;
  37. st_t = t_b;
  38. }
  39. else
  40. return false;
  41. }
  42. while (*st_t == '*' && *st_t != '\0')
  43. st_t++;
  44. if (!*st_t)
  45. return true;
  46. return false;
  47. }

  • HJ72 百钱买百鸡问题(基础数学)

  1. 输入:
  2. 1
  3. 输出:
  4. 0 25 75
  5. 4 18 78
  6. 8 11 81
  7. 12 4 84

示例代码:HJ72.c

  1. #include <stdio.h>
  2. #define CHIKEN_M 5
  3. #define CHIKEN_F 3
  4. #define CHIKEN_S (1 / 3)
  5. int main(void)
  6. {
  7. int num, n1, n2, n3;
  8. scanf("%d", &num);
  9. for (n1 = 0; n1 <= 20; n1++)
  10. {
  11. for (n2 = 0; n2 <= (100 - n1 * CHIKEN_M) / CHIKEN_F; n2++)
  12. {
  13. n3 = (100 - n1 * CHIKEN_M - n2 * CHIKEN_F) * 3;
  14. if ((n1 + n2 + n3) == 100)
  15. printf("%d %d %d\n", n1, n2, n3);
  16. }
  17. }
  18. return 0;
  19. }

  • HJ73 计算日期到天数转换(基础数学)

  1. 输入:2012 12 31
  2. 输出:366

示例代码:HJ73.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MONTH 12
  4. bool is_leap(int year);
  5. int main(void)
  6. {
  7. int year;
  8. int month;
  9. int day;
  10. int i;
  11. int days;
  12. int years[MONTH] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  13. while (scanf("%d%d%d", &year, &month, &day) == 3)
  14. {
  15. days = 0;
  16. if (month > 1)
  17. {
  18. if (is_leap(year))
  19. years[1] = 29;
  20. }
  21. for (i = 0; i < month - 1; i++)
  22. days += years[i];
  23. days += day;
  24. printf("%d\n", days);
  25. years[1] = 28;
  26. }
  27. return 0;
  28. }
  29. bool is_leap(int year)
  30. {
  31. return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
  32. }

  • HJ74 参数解析(字符串)

  1. 输入:
  2. xcopy /s c:\\ d:\\e
  3. 输出:
  4. 4
  5. xcopy
  6. /s
  7. c:\\
  8. d:\\e

示例代码 1:HJ74.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #define LEN 1001
  6. char * s_gets(char * st, int n);
  7. void get_info(char * st);
  8. void print_n_st(char * st, int n);
  9. int main(void)
  10. {
  11. char str[LEN];
  12. while (s_gets(str, LEN) != NULL && *str != '\0')
  13. get_info(str);
  14. return 0;
  15. }
  16. char * s_gets(char * st, int n)
  17. {
  18. char * ret_val;
  19. char * find;
  20. ret_val = fgets(st, n, stdin);
  21. if (ret_val)
  22. {
  23. find = strchr(st, '\n');
  24. if (find)
  25. *find = '\0';
  26. else
  27. while (getchar() != '\n')
  28. continue;
  29. }
  30. return ret_val;
  31. }
  32. void get_info(char * st)
  33. {
  34. bool in_com = false;
  35. int in_call = 0;
  36. int count = 0;
  37. int stat[LEN][2];
  38. int len;
  39. char * st_b = st;
  40. int i;
  41. while (*st)
  42. {
  43. if (*st == '"')
  44. {
  45. in_call++;
  46. }
  47. if (!isspace(*st) && in_com == false && *st != '"')
  48. {
  49. len = 1;
  50. in_com = true;
  51. }
  52. else if (!isspace(*st) && in_com == true && in_call != 2)
  53. {
  54. len++;
  55. }
  56. else if (isspace(*st) && in_call == 1)
  57. len++;
  58. if ((isspace(*st) && in_com == true && in_call == 0) || in_call == 2)
  59. {
  60. in_com = false;
  61. stat[count][0] = len;
  62. stat[count][1] = st - st_b - len;
  63. count++;
  64. in_call = 0;
  65. }
  66. st++;
  67. }
  68. if (in_com == true)
  69. {
  70. stat[count][0] = len;
  71. stat[count][1] = st - st_b - len;
  72. count++;
  73. }
  74. printf("%d\n", count);
  75. for (i = 0; i < count; i++)
  76. {
  77. print_n_st(st_b + stat[i][1], stat[i][0]);
  78. }
  79. }
  80. void print_n_st(char * st, int n)
  81. {
  82. int i;
  83. for (i = 0; i < n; i++)
  84. putchar(*(st + i));
  85. putchar('\n');
  86. }

示例代码 2:HJ74.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #define LEN 1001
  6. int main(void)
  7. {
  8. int count, count2;
  9. char str[LEN];
  10. char * s_temp;
  11. int len;
  12. bool in;
  13. int i;
  14. while (fgets(str, LEN, stdin) != NULL && *str != '\n')
  15. {
  16. count = 0;
  17. count2 = 0;
  18. in = false;
  19. if ((s_temp = strchr(str, '\n')))
  20. *s_temp = '\0';
  21. else
  22. while (getchar() != '\n')
  23. continue;
  24. for (i = 0; i < (int) strlen(str); i++)
  25. {
  26. if (str[i] == '"' && in == false)
  27. {
  28. in = true;
  29. count2++;
  30. continue;
  31. }
  32. if (str[i] == '"' && in == true)
  33. {
  34. in = false;
  35. count2++;
  36. continue;
  37. }
  38. if (in == true)
  39. continue;
  40. if (isspace(str[i]))
  41. count++;
  42. }
  43. count += 1;
  44. printf("%d\n", count);
  45. len = strlen(str);
  46. strtok(str, "\"");
  47. for (i = 0; i < count2 - 1; i++)
  48. strtok(NULL, "\"");
  49. for (i = 0; i < len; i++)
  50. {
  51. if (!isspace(str[i]) && str[i] != '\0')
  52. putchar(str[i]);
  53. if (isspace(str[i]))
  54. putchar('\n');
  55. if (str[i] == '\0')
  56. {
  57. puts(str + i + 1);
  58. i += strlen(str + i + 1) + 2;
  59. }
  60. }
  61. }
  62. return 0;
  63. }

  • HJ75 公共子串计算(字符串)

  1. 输入:
  2. asdfas
  3. werasdfaswer
  4. 输出:
  5. 6

示例代码 1:HJ75.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 151
  5. char * s_gets(char * st, int n);
  6. void cut_str(char * dest, char * start, char * end);
  7. bool is_include(char * st_1, char * st_2);
  8. int get_max(char * st_1, char * st_2);
  9. int main(void)
  10. {
  11. char st_1[LEN];
  12. char st_2[LEN];
  13. while (s_gets(st_1, LEN) != NULL && s_gets(st_2, LEN) != NULL &&
  14. *st_1 != '\0' && *st_2 != '\0')
  15. {
  16. printf("%d\n", get_max(st_1, st_2));
  17. }
  18. return 0;
  19. }
  20. char * s_gets(char * st, int n)
  21. {
  22. char * ret_val;
  23. char * find;
  24. ret_val = fgets(st, n, stdin);
  25. if (ret_val)
  26. {
  27. find = strchr(st, '\n');
  28. if (find)
  29. *find = '\0';
  30. else
  31. while (getchar() != '\n')
  32. continue;
  33. }
  34. return ret_val;
  35. }
  36. void cut_str(char * dest, char * start, char * end)
  37. {
  38. int i;
  39. for (i = 0; i <= end - start; i++)
  40. dest[i] = *(start + i);
  41. dest[i] = '\0';
  42. }
  43. bool is_include(char * st_1, char * st_2)
  44. {
  45. int i, j;
  46. int len_1 = strlen(st_1);
  47. int len_2 = strlen(st_2);
  48. int count = 0;
  49. for (i = 0; i < len_1; i++)
  50. {
  51. for (j = count; j < len_2; j++)
  52. {
  53. if (st_2[j] == st_1[i])
  54. {
  55. count++;
  56. if (count == len_2)
  57. return true;
  58. break;
  59. }
  60. else
  61. {
  62. if (count > 0)
  63. {
  64. i-= count;
  65. count = 0;
  66. }
  67. break;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. int get_max(char * st_1, char * st_2)
  74. {
  75. char dest[LEN];
  76. int len_1 = strlen(st_1);
  77. int len_2 = strlen(st_2);
  78. int i, j;
  79. int time;
  80. if (len_1 > len_2)
  81. {
  82. for (i = len_2; i >= 1; i--)
  83. {
  84. time = len_2 - i + 1;
  85. for (j = 0; j < time; j++)
  86. {
  87. cut_str(dest, st_2 + j, st_2 + i + j - 1);
  88. if (is_include(st_1, dest))
  89. return i;
  90. }
  91. }
  92. }
  93. else
  94. {
  95. for (i = len_1; i >= 1; i--)
  96. {
  97. time = len_1 - i + 1;
  98. for (j = 0; j < time; j++)
  99. {
  100. cut_str(dest, st_1 + j, st_1 + i + j - 1);
  101. if (is_include(st_2, dest))
  102. return i;
  103. }
  104. }
  105. }
  106. return 0;
  107. }

示例代码 2:HJ75.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 151
  5. char * s_gets(char * st, int n);
  6. void cut_str(char * dest, char * start, char * end);
  7. int get_max(char * st_1, char * st_2);
  8. int main(void)
  9. {
  10. char st_1[LEN];
  11. char st_2[LEN];
  12. while (s_gets(st_1, LEN) != NULL && s_gets(st_2, LEN) != NULL &&
  13. *st_1 != '\0' && *st_2 != '\0')
  14. {
  15. printf("%d\n", get_max(st_1, st_2));
  16. }
  17. return 0;
  18. }
  19. char * s_gets(char * st, int n)
  20. {
  21. char * ret_val;
  22. char * find;
  23. ret_val = fgets(st, n, stdin);
  24. if (ret_val)
  25. {
  26. find = strchr(st, '\n');
  27. if (find)
  28. *find = '\0';
  29. else
  30. while (getchar() != '\n')
  31. continue;
  32. }
  33. return ret_val;
  34. }
  35. void cut_str(char * dest, char * start, char * end)
  36. {
  37. int i;
  38. for (i = 0; i <= end - start; i++)
  39. dest[i] = *(start + i);
  40. dest[i] = '\0';
  41. }
  42. int get_max(char * st_1, char * st_2)
  43. {
  44. char dest[LEN];
  45. int len_1 = strlen(st_1);
  46. int len_2 = strlen(st_2);
  47. int i_temp;
  48. char s_temp[LEN];
  49. int i, j;
  50. int time;
  51. if (len_1 < len_2)
  52. {
  53. strcpy(s_temp, st_1);
  54. strcpy(st_1, st_2);
  55. strcpy(st_2, s_temp);
  56. i_temp = len_1;
  57. len_1 = len_2;
  58. len_2 = i_temp;
  59. }
  60. for (i = len_2; i >= 1; i--)
  61. {
  62. time = len_2 - i + 1;
  63. for (j = 0; j < time; j++)
  64. {
  65. cut_str(dest, st_2 + j, st_2 + i + j - 1);
  66. if (strstr(st_1, dest))
  67. return i;
  68. }
  69. }
  70. return 0;
  71. }

示例代码 3:HJ75.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 151
  4. int main(void)
  5. {
  6. char str1[LEN];
  7. char str2[LEN];
  8. int i, j;
  9. int len1, len2;
  10. char s_temp[LEN];
  11. int i_temp;
  12. int max;
  13. int count;
  14. while (scanf("%s%s", str1, str2) != EOF)
  15. {
  16. max = 0;
  17. len1 = strlen(str1);
  18. len2 = strlen(str2);
  19. if (len1 < len2)
  20. {
  21. strcpy(s_temp, str1);
  22. strcpy(str1, str2);
  23. strcpy(str2, s_temp);
  24. i_temp = len1;
  25. len1 = len2;
  26. len2 = i_temp;
  27. }
  28. for (i = 0; i < len1; i++)
  29. {
  30. for (j = 0; j < len2; j++)
  31. {
  32. count = 0;
  33. while (str1[i + count] == str2[j + count] && str1[i + count] != '\0')
  34. count++;
  35. if (count > max)
  36. max = count;
  37. }
  38. }
  39. printf("%d", max);
  40. }
  41. return 0;
  42. }

  • HJ76 尼科彻斯定理(基础数学)

  1. 输入:6
  2. 输出:31+33+35+37+39+41

示例代码:HJ76.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 1001
  4. #define SIZE 10 // 至少容纳 100 的 3 次方
  5. void get_str(int n);
  6. int main(void)
  7. {
  8. int num;
  9. scanf("%d", &num);
  10. get_str(num);
  11. return 0;
  12. }
  13. void get_str(int n)
  14. {
  15. int i;
  16. static char num[SIZE];
  17. static char notation[LEN];
  18. int start;
  19. if (n % 2 == 0)
  20. start = n * n - 1 - (n / 2 - 1) * 2;
  21. else
  22. start = n * n - (n / 2) * 2;
  23. for (i = 0; i < n; i++)
  24. {
  25. sprintf(num, "%d", start);
  26. strcpy(notation + strlen(notation), num);
  27. start += 2;
  28. if (i != n - 1)
  29. strcpy(notation + strlen(notation), "+");
  30. }
  31. puts(notation);
  32. }

  • HJ77 火车进站(栈,DFS)

  1. 输入:
  2. 3
  3. 1 2 3
  4. 输出:
  5. 1 2 3
  6. 1 3 2
  7. 2 1 3
  8. 2 3 1
  9. 3 2 1
  10. 说明:
  11. 第一种方案:1进、1出、2进、2出、3进、3
  12. 第二种方案:1进、1出、2进、3进、3出、2
  13. 第三种方案:1进、2进、2出、1出、3进、3
  14. 第四种方案:1进、2进、2出、3进、3出、1
  15. 第五种方案:1进、2进、3进、3出、2出、1
  16. 请注意,[3,1,2]这个序列是不可能实现的。

示例代码:HJ77.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #define MAX 10000
  6. #define LEN 10
  7. void DFS(int * train, int * station, int * out_station, int * kind, char (*res)[LEN], int top_sta, int top_out, int start, int n);
  8. int SBCompare(const void * p1, const void * p2);
  9. int main(void)
  10. {
  11. int n; // n 辆火车
  12. int * train; // 保存火车的编号
  13. int * station; // 保存火车站的火车
  14. int * out_station; // 保存出站的火车
  15. int kind; // 合理的方案数
  16. char res[MAX][LEN]; // 保存合理的方案
  17. int i, j;
  18. while (scanf("%d", &n) != EOF)
  19. {
  20. kind = 0;
  21. train = (int *) malloc(sizeof(int) * n);
  22. station = (int *) malloc(sizeof(int) * n);
  23. out_station = (int *) malloc(sizeof(int) * n);
  24. for (i = 0; i < n; i++)
  25. scanf("%d", train + i);
  26. DFS(train, station, out_station, &kind, res, -1, -1, 0, n);
  27. qsort(res, kind, sizeof(res[0]), SBCompare);
  28. for (i = 0; i < kind; i++)
  29. {
  30. for (j = 0; j < n; j ++)
  31. printf("%c ", res[i][j]);
  32. putchar('\n');
  33. }
  34. }
  35. free(train);
  36. free(station);
  37. free(out_station);
  38. return 0;
  39. }
  40. void DFS(int * train, int * station, int * out_station, int * kind, char (*res)[LEN], int top_sta, int top_out, int start, int n)
  41. {
  42. int i;
  43. if (top_out + 1 == n)
  44. {
  45. for (i = 0; i < n; i++)
  46. res[*kind][i] = out_station[i] + '0';
  47. (*kind)++;
  48. return;
  49. }
  50. /* 可以选择是否入站 */
  51. if (start < n)
  52. {
  53. /* 选择入站 */
  54. station[++top_sta] = train[start++];
  55. DFS(train, station, out_station, kind, res, top_sta, top_out, start, n);
  56. /* 暂时不入站 */
  57. top_sta--;
  58. start--;
  59. }
  60. /* 可以选择是否出站 */
  61. if(top_sta >= 0)
  62. {
  63. /* 选择出站 */
  64. out_station[++top_out] = station[top_sta--];
  65. DFS(train, station, out_station, kind, res, top_sta, top_out, start, n);
  66. /* 暂时不出站 */
  67. station[++top_sta] = out_station[top_out--];
  68. }
  69. }
  70. int SBCompare(const void * p1, const void * p2)
  71. {
  72. return strcmp((char *) p1, (char *) p2);
  73. }

  • HJ80 整型数组合并(排序)

  1. 输入:
  2. 3
  3. 1 2 5
  4. 4
  5. -1 0 3 2
  6. 输出:
  7. -101235

示例代码 1:HJ80.c

  1. #include <stdio.h>
  2. #include <limits.h>
  3. void order_up(int ar[], int n);
  4. void com_arr(int m, int n, int ar_a[], int ar_b[]);
  5. int main(void)
  6. {
  7. int m, n;
  8. int i;
  9. scanf("%d", &m);
  10. int ar_a[m];
  11. for (i = 0; i < m; i++)
  12. scanf("%d", ar_a + i);
  13. scanf("%d", &n);
  14. int ar_b[n];
  15. for (i = 0; i < n; i++)
  16. scanf("%d", ar_b + i);
  17. order_up(ar_a, m);
  18. order_up(ar_b, n);
  19. com_arr(m, n, ar_a, ar_b);
  20. return 0;
  21. }
  22. void order_up(int ar[], int n)
  23. {
  24. int i, j;
  25. int temp;
  26. for (i = 0; i < n - 1; i++)
  27. for (j = i + 1; j < n; j++)
  28. if (ar[i] > ar[j])
  29. {
  30. temp = ar[i];
  31. ar[i] = ar[j];
  32. ar[j] = temp;
  33. }
  34. }
  35. void com_arr(int m, int n, int ar_a[], int ar_b[])
  36. {
  37. int i, j;
  38. int temp = INT_MIN;
  39. for (i = 0; i < m; i++)
  40. {
  41. for (j = 0; j < n; j++)
  42. {
  43. if (ar_b[j] < ar_a[i] && ar_b[j] > temp)
  44. {
  45. printf("%d", ar_b[j]);
  46. temp = ar_b[j];
  47. }
  48. }
  49. if (ar_a[i] > temp)
  50. {
  51. printf("%d", ar_a[i]);
  52. temp = ar_a[i];
  53. }
  54. }
  55. for (i = 0; i < n; i++)
  56. {
  57. if (ar_b[i] > temp)
  58. printf("%d", ar_b[i]);
  59. }
  60. }

示例代码 2:HJ80.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LEN 1000
  4. int SBCompare(const void * p1, const void * p2);
  5. int main(void)
  6. {
  7. int ar[LEN];
  8. int m, n;
  9. int i;
  10. while (scanf("%d", &m) == 1)
  11. {
  12. for (i = 0; i < m; i++)
  13. scanf("%d", ar + i);
  14. scanf("%d", &n);
  15. for (i = m; i < m + n; i++)
  16. scanf("%d", ar + i);
  17. qsort(ar, m + n, sizeof(int), SBCompare);
  18. for (i = 0; i < m + n; i++)
  19. {
  20. if (ar[i] != ar[i + 1])
  21. printf("%d", ar[i]);
  22. }
  23. }
  24. return 0;
  25. }
  26. int SBCompare(const void * p1, const void * p2)
  27. {
  28. return *((int *) p1) - *((int *) p2);
  29. }

  • HJ81 字符串字符匹配(字符串)

  1. 输入:
  2. bc
  3. abc
  4. 输出:
  5. true
  6. 说明:
  7. 其中abc含有bc,输出"true"

示例代码 1:HJ81.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 1001
  5. char * s_gets(char * st, int n);
  6. bool is_have(char * st1, char * st2);
  7. int main(void)
  8. {
  9. char str1[LEN], str2[LEN];
  10. while (s_gets(str1, LEN) != NULL && s_gets(str2, LEN) != NULL && *str1 != '\0' &&
  11. *str2 != '\0')
  12. if (is_have(str1, str2))
  13. printf("true\n");
  14. else
  15. printf("false\n");
  16. return 0;
  17. }
  18. char * s_gets(char * st, int n)
  19. {
  20. char * ret_val;
  21. char * find;
  22. ret_val = fgets(st, n, stdin);
  23. if (ret_val)
  24. {
  25. find = strchr(st, '\n');
  26. if (find)
  27. *find = '\0';
  28. else
  29. while (getchar() != '\n')
  30. continue;
  31. }
  32. return ret_val;
  33. }
  34. bool is_have(char * st1, char * st2)
  35. {
  36. int i, j;
  37. int len1 = strlen(st1); // short
  38. int len2 = strlen(st2); // long
  39. int count;
  40. for (i = 0; i < len1; i++)
  41. {
  42. count = 0;
  43. for (j = 0; j < len2; j++)
  44. {
  45. if (st2[j] == st1[i])
  46. {
  47. count++;
  48. break;
  49. }
  50. }
  51. if (!count)
  52. return false;
  53. }
  54. return true;
  55. }

示例代码 2:HJ81.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 201
  5. int main(void)
  6. {
  7. char str1[LEN];
  8. char str2[LEN];
  9. bool find;
  10. int i;
  11. while (scanf("%s%s", str1, str2) != EOF)
  12. {
  13. find = true;
  14. for (i = 0; i < (int) strlen(str1); i++)
  15. {
  16. if (!strchr(str2, str1[i]))
  17. {
  18. find = false;
  19. break;
  20. }
  21. }
  22. if (!find)
  23. puts("false");
  24. else
  25. puts("true");
  26. }
  27. return 0;
  28. }

示例代码 3:HJ81.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 26
  5. int main(void)
  6. {
  7. static int str1[LEN];
  8. static int str2[LEN];
  9. bool find = true;
  10. char ch;
  11. int i;
  12. while ((ch = getchar()) != '\n')
  13. str1[ch - 'a'] = 1;
  14. while ((ch = getchar()) != '\n')
  15. str2[ch - 'a'] = 1;
  16. for (i = 0; i < LEN; i++)
  17. {
  18. if (str1[i] == 1 && str2[i] != 1)
  19. find = false;
  20. }
  21. if (find)
  22. puts("true");
  23. else
  24. puts("false");
  25. return 0;
  26. }

  • HJ82 将真分数分解为埃及分数(基础数学)

  1. 输入:
  2. 8/11
  3. 2/4
  4. 输出:
  5. 1/2+1/5+1/55+1/110
  6. 1/3+1/6
  7. 说明:
  8. 第二个样例直接输出1/2也是可以的

示例代码 1:HJ82.c

  1. #include <stdio.h>
  2. void caculate(int a, int b);
  3. int main(void)
  4. {
  5. int a, b;
  6. while (scanf("%d/%d", &a, &b) == 2)
  7. caculate(a, b);
  8. return 0;
  9. }
  10. void caculate(int a, int b)
  11. {
  12. if (a == 1)
  13. {
  14. printf("1/%d\n", b);
  15. return;
  16. }
  17. if (b % a == 0)
  18. {
  19. printf("1/%d\n", b / a);
  20. return;
  21. }
  22. printf("1/%d+", b / a + 1);
  23. caculate(a - b % a, b * (b / a + 1));
  24. }

示例代码 2:HJ82.c

  1. #include <stdio.h>
  2. int gcd(int a, int b);
  3. int main()
  4. {
  5. int a, b;
  6. while (scanf("%d/%d", &a, &b) != EOF)
  7. {
  8. int under[b];
  9. int top = -1;
  10. int q, r, div;
  11. div = gcd(a, b);
  12. a /= div;
  13. b /= div;
  14. if (a == 1)
  15. under[++top] = b;
  16. else
  17. {
  18. while (a != 1) // b = a * q + r -> a + b = a * (q + 1) + r
  19. {
  20. q = b / a;
  21. r = b % a;
  22. under[++top] = q + 1;
  23. a = a - r;
  24. b = b * (q + 1);
  25. div = gcd(a, b);
  26. a /= div;
  27. b /= div;
  28. }
  29. under[++top] = b;
  30. }
  31. for (int i = 0; i <= top; i++)
  32. {
  33. if (i != 0)
  34. printf("+");
  35. printf("1/%d", under[i]);
  36. }
  37. printf("\n");
  38. }
  39. return 0;
  40. }
  41. int gcd(int a, int b) // 最大公因数
  42. {
  43. int t;
  44. while (b != 0)
  45. {
  46. t = a % b;
  47. a = b;
  48. b = t;
  49. }
  50. return a;
  51. }

  • HJ83 二维数组操作(数组)

  1. 输入:
  2. 4 9
  3. 5 1 2 6
  4. 0
  5. 8
  6. 2 3
  7. 4 7
  8. 4 2 3 2
  9. 3
  10. 3
  11. 4 7
  12. 输出:
  13. 0
  14. -1
  15. 0
  16. -1
  17. 0
  18. 0
  19. -1
  20. 0
  21. 0
  22. -1
  23. 说明:
  24. 本组样例共有2组样例输入。
  25. 第一组样例:
  26. 1.初始化数据表为49列,成功
  27. 2.交换第51列和第26列的数据,失败。因为行的范围应该是(0,3),不存在第5行。
  28. 3.在第0行上方添加一行,成功。
  29. 4.在第8列左边添加一列,失败。因为列的总数已经达到了9的上限。
  30. 5.查询第2行第3列的值,成功。
  31. 第二组样例:
  32. 1.初始化数据表为47列,成功
  33. 2.交换第42列和第32列的数据,失败。因为行的范围应该是(0,3),不存在第4行。
  34. 3.在第3行上方添加一行,成功。
  35. 4.在第3列左边添加一列,成功。
  36. 5.查询第47列的值,失败。因为虽然添加了一行一列,但数据表会在添加后恢复成47列的形态,所以行的区间仍然在[0,3],列的区间仍然在[0,6],无法查询到(4,7)坐标。

示例代码:HJ83.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int m, n;
  5. int x1, y1, x2, y2;
  6. int x, y;
  7. while (scanf("%d%d", &m, &n) == 2)
  8. {
  9. if (m > 0 && m < 10 && n > 0 && n < 10)
  10. puts("0");
  11. else
  12. {
  13. puts("-1");
  14. return -1;
  15. }
  16. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  17. if (x1 >= 0 && x1 < m && y1 >= 0 && y1 < n
  18. && x2 >= 0 && x2 < m && y2 >= 0 && y2 < n)
  19. puts("0");
  20. else
  21. puts("-1");
  22. scanf("%d", &x);
  23. if (x >= 0 && x < m && m < 9)
  24. puts("0");
  25. else
  26. puts("-1");
  27. scanf("%d", &y);
  28. if (y >= 0 && y < n && n < 9)
  29. puts("0");
  30. else
  31. puts("-1");
  32. scanf("%d%d", &x, &y);
  33. if (x >= 0 && x < m && y >= 0 && y < n)
  34. puts("0");
  35. else
  36. puts("-1");
  37. }
  38. return 0;
  39. }

HJ84 统计大写字母个数(字符串)

  1. 输入:A 1 0 1 1150175017(&^%&$vabovbaoadd 123#$%#%#O
  2. 输出:2

示例代码 1:HJ84.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 251
  5. char * s_gets(char * st, int n);
  6. int get_upper(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. if (s_gets(str, LEN) != NULL && *str != '\0')
  11. printf("%d", get_upper(str));
  12. return 0;
  13. }
  14. char * s_gets(char * st, int n)
  15. {
  16. char * ret_val;
  17. char * find;
  18. ret_val = fgets(st, n, stdin);
  19. if (ret_val)
  20. {
  21. find = strchr(st, '\n');
  22. if (find)
  23. *find = '\0';
  24. else
  25. while (getchar() != '\n')
  26. continue;
  27. }
  28. return ret_val;
  29. }
  30. int get_upper(char * st)
  31. {
  32. int count = 0;
  33. while (*st)
  34. {
  35. if (isupper(*st))
  36. count++;
  37. st++;
  38. }
  39. return count;
  40. }

示例代码 2:HJ84.c

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main(void)
  4. {
  5. char ch;
  6. int count = 0;
  7. while ((ch = getchar()) != '\n')
  8. {
  9. if (isupper(ch))
  10. count++;
  11. }
  12. printf("%d", count);
  13. return 0;
  14. }

HJ85 最长回文子串(字符串)

  1. 输入:cdabbacc
  2. 输出:4
  3. 说明:abba为最长的回文子串

示例代码:HJ85.c

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define LEN 351
  4. int main(void){
  5. char str[LEN];
  6. int max = 0;
  7. int i, j;
  8. int l, r;
  9. scanf("%s", str);
  10. for (i = 0; i < (int) strlen(str) - 1; i++) // 此处 i 是左端点
  11. {
  12. for (j = i + 1; j < (int) strlen(str); j++) // cdabbacc
  13. {
  14. l = i, r = j;
  15. while (l < r)
  16. {
  17. if (str[r] != str[l])
  18. break;
  19. else
  20. {
  21. l++;
  22. r--;
  23. }
  24. }
  25. if (l >= r) // 包含 abcba 这种情况,最终停在同一位置
  26. max = max > (j - i + 1) ? max : (j - i + 1);
  27. }
  28. }
  29. printf("%d", max);
  30. return 0;
  31. }

  • HJ86 求最大连续bit数(位运算)

  1. 输入:200
  2. 输出:2
  3. 说明:200的二进制表示是11001000,最多有2个连续的1

示例代码:HJ86.c

  1. #include <stdio.h>
  2. int get_continue(int n);
  3. int main(void)
  4. {
  5. int num;
  6. while (scanf("%d", &num) == 1)
  7. printf("%d\n", get_continue(num));
  8. return 0;
  9. }
  10. int get_continue(int n)
  11. {
  12. int pre = 0, curr = 0;
  13. while (n)
  14. {
  15. if (n & 1)
  16. curr++;
  17. else
  18. {
  19. if (curr > pre)
  20. pre = curr;
  21. curr = 0;
  22. }
  23. n >>= 1;
  24. }
  25. return pre > curr ? pre : curr;
  26. }

  • HJ87 密码强度等级(字符串)

  1. 输入:38$@NoNoN
  2. 输出:VERY_SECURE
  3. 说明:
  4. 样例的密码长度大于等于8个字符,得25分;
  5. 大小写字母都有所以得20分;
  6. 有两个数字,所以得20分;
  7. 包含大于1符号,所以得25分;
  8. 由于该密码包含大小写字母、数字和符号,所以奖励部分得5分;
  9. 经统计得该密码的密码强度为25+20+20+25+5=95分。

示例代码:HJ87.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define LEN 301
  5. char * s_gets(char * st, int n);
  6. int get_score(char * st);
  7. int main(void)
  8. {
  9. char str[LEN];
  10. int score;
  11. char judge[7][12] = { "VERY_SECURE", "SECURE", "VERY_STRONG", "STRONG",
  12. "AVERAGE", "WEAK", "VERY_WEAK" };
  13. if (s_gets(str, LEN) != NULL && *str != '\0')
  14. score = get_score(str);
  15. if (score >= 90)
  16. puts(judge[0]);
  17. else if (score >= 80)
  18. puts(judge[1]);
  19. else if (score >= 70)
  20. puts(judge[2]);
  21. else if (score >= 60)
  22. puts(judge[3]);
  23. else if (score >= 50)
  24. puts(judge[4]);
  25. else if (score >= 25)
  26. puts(judge[5]);
  27. else if (score >= 0)
  28. puts(judge[6]);
  29. return 0;
  30. }
  31. char * s_gets(char * st, int n)
  32. {
  33. char * ret_val;
  34. char * find;
  35. ret_val = fgets(st, n, stdin);
  36. if (ret_val)
  37. {
  38. find = strchr(st, '\n');
  39. if (find)
  40. *find = '\0';
  41. else
  42. while (getchar() != '\n')
  43. continue;
  44. }
  45. return ret_val;
  46. }
  47. int get_score(char * st)
  48. {
  49. int len = strlen(st);
  50. int low_alpha = 0, up_alpha = 0;
  51. int num_count = 0;
  52. int other = 0;
  53. int score = 0;
  54. while (*st)
  55. {
  56. if (islower(*st))
  57. low_alpha++;
  58. else if (isupper(*st))
  59. up_alpha++;
  60. else if (isdigit(*st))
  61. num_count++;
  62. else
  63. other++;
  64. st++;
  65. }
  66. if (len <= 4 && len > 0)
  67. score += 5;
  68. else if (len <= 7)
  69. score += 10;
  70. else
  71. score += 25;
  72. if (low_alpha == 0 && up_alpha == 0)
  73. score += 0;
  74. else if (low_alpha > 0 && up_alpha > 0)
  75. score += 20;
  76. else
  77. score += 10;
  78. if (num_count == 0)
  79. score += 0;
  80. else if (num_count == 1)
  81. score += 10;
  82. else
  83. score += 20;
  84. if (other == 0)
  85. score += 0;
  86. else if (other == 1)
  87. score += 10;
  88. else
  89. score += 25;
  90. if (low_alpha && up_alpha && num_count && other)
  91. score += 5;
  92. else if ((low_alpha || up_alpha) && num_count && other)
  93. score += 3;
  94. else if ((low_alpha || up_alpha) && num_count)
  95. score += 2;
  96. return score;
  97. }

  • HJ88 扑克牌大小

  1. 输入:4 4 4 4-joker JOKER
  2. 输出:joker JOKER

示例代码:HJ88.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #define LEN 30
  6. /* 1:Singel、2: Pair、3: Tripel、5: Sequence、 4:Bomb、8:SupterBomb */
  7. int JudgeType(char * st);
  8. int main(void)
  9. {
  10. char puker[15][6] = { {"3"}, {"4"}, {"5"}, {"6"}, {"7"}, {"8"}, {"9"},
  11. {"10"}, {"J"}, {"Q"}, {"K"}, {"A"}, {"2"},
  12. {"joker"}, {"JOKER"} };
  13. static char puker1[LEN];
  14. static char puker1_b[LEN];
  15. static char puker2[LEN];
  16. static char puker2_b[LEN];
  17. int len1 = 0, len2 = 0;
  18. int type1, type2;
  19. int i_temp1, i_temp2;
  20. int i;
  21. char ch;
  22. while ((ch = getchar()) != '-')
  23. puker1[len1++] = ch;
  24. while((ch = getchar()) != '\n')
  25. puker2[len2++] = ch;
  26. strcpy(puker1_b, puker1);
  27. strcpy(puker2_b, puker2);
  28. /* puker1:Bomb、puker2:No Bomb */
  29. type1 = JudgeType(puker1);
  30. type2 = JudgeType(puker2);
  31. if (type1 % 4 == 0 && type2 % 4 != 0)
  32. puts(puker1);
  33. else if (type2 % 4 == 0 && type1 % 4 != 0)
  34. puts(puker2);
  35. else if (type1 == 8 && type2 == 4)
  36. puts(puker1);
  37. else if (type2 == 8 && type1 == 4)
  38. puts(puker2);
  39. else if (type1 == type2)
  40. {
  41. if (type1 != 1)
  42. {
  43. strtok(puker1, " ");
  44. strtok(puker2, " ");
  45. }
  46. for (i = 0; i < 15; i++)
  47. {
  48. if (strcmp(puker1, puker[i]) == 0)
  49. i_temp1 = i;
  50. if (strcmp(puker2, puker[i]) == 0)
  51. i_temp2 = i;
  52. }
  53. if (i_temp1 > i_temp2)
  54. puts(puker1_b);
  55. else if (i_temp1 < i_temp2)
  56. puts(puker2_b);
  57. }
  58. else
  59. puts("ERROR");
  60. return 0;
  61. }
  62. int JudgeType(char * st)
  63. {
  64. int type;
  65. int count = 0;
  66. int len = strlen(st);
  67. int i;
  68. for (i = 0; i < len; i++)
  69. {
  70. if (isspace(st[i]))
  71. count++;
  72. }
  73. if (count == 0)
  74. type = 1;
  75. if (count == 2)
  76. type = 3;
  77. if (count == 3)
  78. type = 4;
  79. if (count == 4)
  80. type = 5;
  81. if (count == 1)
  82. {
  83. if (strstr(st, "joker"))
  84. type = 8;
  85. else
  86. type = 2;
  87. }
  88. return type;
  89. }

  • HJ89 24点运算(基础数学,DFS)

  1. 输入:4 2 K A
  2. 输出:K-A*4/2
  3. 说明:A+K*2-4也是一种答案,输出任意一种即可

示例代码:HJ89.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #define LEN 20
  5. #define SIZE 4
  6. bool DFS(float * num, bool * used, char * symbol_stack, float * num_stack, int n_top, int s_top, float start);
  7. int main(void)
  8. {
  9. char puker[LEN];
  10. float num[SIZE];
  11. char symbol_stack[SIZE];
  12. float num_stack[SIZE];
  13. bool find;
  14. bool used[SIZE] = { false };
  15. int s_top, n_top;
  16. int i;
  17. while (fgets(puker, LEN, stdin) != NULL && *puker != '\0')
  18. {
  19. find = false;
  20. *(puker + (int) strlen(puker) - 1) = '\0';
  21. if (strstr(puker, "joker") || strstr(puker, "JOKER"))
  22. {
  23. puts("ERROR");
  24. break;
  25. }
  26. for (i = 0; i < 4; i++)
  27. {
  28. if (*(puker + 2 * i) == 'J')
  29. num[i] = 11;
  30. else if (*(puker + 2 * i) == 'Q')
  31. num[i] = 12;
  32. else if (*(puker + 2 * i) == 'K')
  33. num[i] = 13;
  34. else if (*(puker + 2 * i) == 'A')
  35. num[i] = 1;
  36. else
  37. num[i] = *(puker + 2 * i) - '0';
  38. }
  39. for (i = 0; i < SIZE; i++)
  40. {
  41. n_top = -1;
  42. s_top = -1;
  43. used[i] = true;
  44. num_stack[++n_top] = num[i];
  45. if (DFS(num, used, symbol_stack, num_stack, n_top, s_top, num[i]))
  46. {
  47. find = true;
  48. break;
  49. }
  50. used[i] = false;
  51. }
  52. if (!find)
  53. puts("NONE");
  54. }
  55. return 0;
  56. }
  57. bool DFS(float * num, bool * used, char * symbol_stack, float * num_stack, int n_top, int s_top, float start)
  58. {
  59. int i;
  60. if (start == 24 && s_top == 2)
  61. {
  62. for (i = 0; i < SIZE; i++)
  63. {
  64. if (num_stack[i] == 11)
  65. putchar('J');
  66. else if (num_stack[i] == 12)
  67. putchar('Q');
  68. else if (num_stack[i] == 13)
  69. putchar('K');
  70. else if (num_stack[i] == 1)
  71. putchar('A');
  72. else
  73. printf("%.0f", num_stack[i]);
  74. if (i < SIZE - 1)
  75. printf("%c", symbol_stack[i]);
  76. }
  77. return true;
  78. }
  79. for (i = 0; i < SIZE; i++)
  80. {
  81. if (!used[i])
  82. {
  83. used[i] = true;
  84. symbol_stack[++s_top] = '+';
  85. num_stack[++n_top] = num[i];
  86. if (DFS(num, used, symbol_stack, num_stack, n_top, s_top, start + num[i]))
  87. return true;
  88. s_top--;
  89. n_top--;
  90. symbol_stack[++s_top] = '-';
  91. num_stack[++n_top] = num[i];
  92. if (DFS(num, used, symbol_stack, num_stack, n_top, s_top, start - num[i]))
  93. return true;
  94. s_top--;
  95. n_top--;
  96. symbol_stack[++s_top] = '*';
  97. num_stack[++n_top] = num[i];
  98. if (DFS(num, used, symbol_stack, num_stack, n_top, s_top, start * num[i]))
  99. return true;
  100. s_top--;
  101. n_top--;
  102. symbol_stack[++s_top] = '/';
  103. num_stack[++n_top] = num[i];
  104. if (DFS(num, used, symbol_stack, num_stack, n_top, s_top, start / num[i]))
  105. return true;
  106. s_top--;
  107. n_top--;
  108. used[i] = false;
  109. }
  110. }
  111. return false;
  112. }

  • HJ90 合法IP(字符串)

  1. 输入:255.255.255.1000
  2. 输出:NO

示例代码:HJ90.c

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdbool.h>
  4. #define LEN 101
  5. bool is_legal(char * st);
  6. int main(void)
  7. {
  8. char str[LEN];
  9. while (scanf("%s", str) == 1)
  10. printf("%s\n", is_legal(str) ? "YES" : "NO");
  11. return 0;
  12. }
  13. bool is_legal(char * st)
  14. {
  15. char * st_b = st;
  16. int num_count = 0;
  17. int dot_count = 0;
  18. int num = 0;
  19. while (*st)
  20. {
  21. if ((st == st_b && !(*st >= '0' && *st <= '9')) || (*st == '.' && !(*(st + 1) >= '0' && *(st + 1) <= '9')))
  22. return false;
  23. if (st == st_b && *st == '0' && isdigit(*(st + 1)))
  24. return false;
  25. if (*st == '.' && *(st + 1) == '0' && isdigit(*(st + 2)))
  26. return false;
  27. if (isdigit(*st))
  28. {
  29. num = 10 * num + *st - '0';
  30. if (!isdigit(*(st + 1)))
  31. num_count++;
  32. }
  33. if (*st == '.')
  34. {
  35. dot_count++;
  36. if (!(num >= 0 && num < 256))
  37. return false;
  38. num = 0;
  39. }
  40. st++;
  41. }
  42. if (!(num_count == 4 && dot_count == 3) || !(num >= 0 && num < 256))
  43. return false;
  44. return true;
  45. }

  • HJ91 走方格的方案数(动态规划)

  1. 输入:2 2
  2. 输出:6

示例代码:HJ91.c

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. int m, n;
  5. if (scanf("%d%d", &m, &n) == 2)
  6. {
  7. int dp[m + 1][n + 1];
  8. for (int i = 0; i <= m; i++)
  9. {
  10. for (int j = 0; j <= n; j++)
  11. {
  12. /* 初始化初值,只有一条线,方案只有1种 */
  13. if (i == 0 || j == 0)
  14. dp[i][j] = 1;
  15. else
  16. /* 与上面一个格子,和左边一个格子的情况总和相同 */
  17. dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
  18. }
  19. }
  20. printf("%d\n", dp[m][n]);
  21. }
  22. return 0;
  23. }

  • HJ92 在字符串中找出连续最长的数字串(字符串)

  1. 输入:
  2. abcd12345ed125ss123058789
  3. a8a72a6a5yy98y65ee1r2
  4. 输出:
  5. 123058789,9
  6. 729865,2
  7. 说明:
  8. 样例一最长的数字子串为123058789,长度为9
  9. 样例二最长的数字子串有72,98,65,长度都为2

示例代码:HJ92.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #define LEN 201
  6. char * s_gets(char * st, int n);
  7. void get_num(char * st);
  8. void print(char * start, int n);
  9. int main(void)
  10. {
  11. char str[LEN];
  12. while (s_gets(str, LEN) != NULL && *str != '\0')
  13. get_num(str);
  14. return 0;
  15. }
  16. char * s_gets(char * st, int n)
  17. {
  18. char * ret_val;
  19. char * find;
  20. ret_val = fgets(st, n, stdin);
  21. if (ret_val)
  22. {
  23. find = strchr(st, '\n');
  24. if (find)
  25. *find = '\0';
  26. else
  27. while (getchar() != '\n')
  28. continue;
  29. }
  30. return ret_val;
  31. }
  32. void get_num(char * st)
  33. {
  34. int i;
  35. int mark[LEN][2];
  36. int max = 0;
  37. int count = 0;
  38. int lenth = 0;
  39. bool in_num = false;
  40. char * st_b = st;
  41. while (*st)
  42. {
  43. if (isdigit(*st) && in_num == false)
  44. {
  45. lenth = 1;
  46. if (lenth > max)
  47. max = lenth;
  48. in_num = true;
  49. }
  50. else if (isdigit(*st) && in_num == true)
  51. {
  52. lenth++;
  53. if (lenth > max)
  54. max = lenth;
  55. }
  56. if (!isdigit(*st) && in_num == true)
  57. {
  58. in_num = false;
  59. mark[count][0] = lenth;
  60. mark[count][1] = st - st_b;
  61. count++;
  62. }
  63. st++;
  64. }
  65. if (in_num == true)
  66. {
  67. mark[count][0] = lenth;
  68. mark[count][1] = st - st_b;
  69. count++;
  70. }
  71. for (i = 0; i < count; i++)
  72. {
  73. if (mark[i][0] == max)
  74. print(st_b + mark[i][1] - max, max);
  75. }
  76. printf(",%d", max);
  77. }
  78. void print(char * start, int n)
  79. {
  80. int i;
  81. for (i = 0; i < n; i++)
  82. putchar(*(start + i));
  83. }

  • HJ93 数组分组(数组,DFS)

  1. 输入:
  2. 4
  3. 1 5 -5 1
  4. 输出:
  5. true
  6. 说明:
  7. 第一组:5 -5 1
  8. 第二组:1

示例代码:HJ93.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define LEN 50
  5. bool DFS(int * ar_o, int count_o, bool * used, int start, int total);
  6. int main(void)
  7. {
  8. int n;
  9. int ar[LEN];
  10. int ar_o[LEN];
  11. int count_o;
  12. int total;
  13. int total_3;
  14. int total_5;
  15. int total_o;
  16. bool used[LEN] = { false };
  17. int i;
  18. while (scanf("%d", &n) == 1)
  19. {
  20. count_o = 0;
  21. total = total_3 = total_5 = total_o = 0;
  22. for (i = 0; i < n; i++)
  23. {
  24. scanf("%d", ar + i);
  25. if (ar[i] % 3 == 0)
  26. total_3 += ar[i];
  27. else if (ar[i] % 5 == 0)
  28. total_5 += ar[i];
  29. else
  30. {
  31. ar_o[count_o++] = ar[i];
  32. total_o += ar[i];
  33. }
  34. total += ar[i];
  35. }
  36. if (total % 2 != 0)
  37. {
  38. puts("false");
  39. continue;
  40. }
  41. if (total_3 == total_5 && total_o == 0)
  42. {
  43. puts("true");
  44. continue;
  45. }
  46. if (DFS(ar_o, count_o, used, total_3, total / 2))
  47. puts("true");
  48. else
  49. puts("false");
  50. }
  51. return 0;
  52. }
  53. bool DFS(int * ar_o, int count_o, bool * used, int start, int total)
  54. {
  55. int i;
  56. if (start == total)
  57. return true;
  58. for (i = 0; i < count_o; i++)
  59. {
  60. if (!used[i])
  61. {
  62. used[i] = true;
  63. if (DFS(ar_o, count_o, used, start + ar_o[i], total))
  64. return true;
  65. used[i] = false;
  66. }
  67. }
  68. return false;
  69. }

  • HJ94 记票统计(字符串)

  1. 输入:
  2. 4
  3. A B C D
  4. 8
  5. A D E CF A GG A B
  6. 输出:
  7. A : 3
  8. B : 1
  9. C : 0
  10. D : 1
  11. Invalid : 3
  12. 说明:
  13. E CF GG三张票是无效的,所以Invalid的数量是3.

示例代码:HJ94.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 21
  4. void get_tollet(int m, int n, char ar[][LEN], char br[][LEN]);
  5. int main(void)
  6. {
  7. int m, n;
  8. int i;
  9. scanf("%d", &m);
  10. char ar[m][LEN];
  11. for (i = 0; i < m; i++)
  12. scanf("%s", ar[i]);;
  13. scanf("%d", &n);
  14. char br[n][LEN];
  15. for (i = 0; i < n; i++)
  16. scanf("%s", br[i]);
  17. get_tollet(m, n, ar, br);
  18. return 0;
  19. }
  20. void get_tollet(int m, int n, char ar[][LEN], char br[][LEN])
  21. {
  22. int i, j;
  23. int stat[m];
  24. int use = 0;
  25. for (i = 0; i < m; i++)
  26. stat[i] = 0;
  27. for (i = 0; i < n; i++)
  28. {
  29. for (j = 0; j < m; j++)
  30. {
  31. if (!strcmp(ar[j], br[i]))
  32. {
  33. stat[j]++;
  34. break;
  35. }
  36. }
  37. }
  38. for (i = 0; i < m; i++)
  39. {
  40. printf("%s : %d\n", ar[i], stat[i]);
  41. use += stat[i];
  42. }
  43. printf("Invalid : %d\n", n - use);
  44. }

  • HJ95 人民币转换(字符串)

  1. 输入:151121.15
  2. 输出:人民币拾伍万壹仟壹佰贰拾壹元壹角伍分

示例代码:HJ95.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4. #define LEN 50
  5. void get_bits(double d_n, int * ar);
  6. void printInfo(int n, char number[][LEN]);
  7. int main(void)
  8. {
  9. double d_n;
  10. static int ar[5];
  11. bool flag = false;
  12. char number[13][LEN] = { {"零"}, {"壹"}, {"贰"}, {"叁"}, {"肆"}, {"伍"}, {"陆"}, {"柒"},
  13. {"捌"}, {"玖"}, {"拾"}, {"佰"}, {"仟"} };
  14. char unit[6][LEN] = { {"亿"}, {"万"}, {"元"}, {"角"}, {"分"}, {"整"} };
  15. while (scanf("%lf", &d_n) == 1)
  16. {
  17. get_bits(d_n, ar);
  18. if (ar[0] || ar[1] || ar[2] || ar[3] || ar[4])
  19. printf("人民币");
  20. if (ar[4])
  21. {
  22. printInfo(ar[4], number);
  23. printf("%s", unit[0]);
  24. flag = true;
  25. }
  26. if (ar[3])
  27. {
  28. if (flag && ar[4] % 10 == 0)
  29. printf("%s", number[0]);
  30. printInfo(ar[3], number);
  31. printf("%s", unit[1]);
  32. flag = true;
  33. }
  34. if (ar[2])
  35. {
  36. if (flag && ar[3] % 10 == 0)
  37. printf("%s", number[0]);
  38. printInfo(ar[2], number);
  39. flag = true;
  40. }
  41. if (flag)
  42. printf("%s", unit[2]);
  43. if (ar[1])
  44. printf("%s%s", number[ar[1]], unit[3]);
  45. if (ar[0])
  46. printf("%s%s", number[ar[0]], unit[4]);
  47. if (!ar[1] && !ar[0] && flag)
  48. printf("%s", unit[5]);
  49. }
  50. return 0;
  51. }
  52. void get_bits(double d_n, int * ar)
  53. {
  54. /* 分 */
  55. double temp = d_n * 100;
  56. ar[0] = (long long) round(temp) % 10;
  57. /* 角 */
  58. ar[1] = ((long long) round(temp) / 10) % 10;
  59. /* 元 */
  60. ar[2] = (long long) d_n % 10000;
  61. /* 万 */
  62. ar[3] = ((long long) d_n / 10000) % 10000;
  63. /* 亿 */
  64. ar[4] = (long long) d_n / 100000000;
  65. }
  66. void printInfo(int n, char number[][LEN])
  67. {
  68. bool flag = false;
  69. if (n >= 1000)
  70. {
  71. printf("%s%s", number[n / 1000], number[12]);
  72. n %= 1000;
  73. flag = true;
  74. }
  75. if (n < 100 && n >= 10 && flag)
  76. {
  77. printf("%s", number[0]);
  78. }
  79. else if (n >= 100)
  80. {
  81. printf("%s%s", number[n / 100], number[11]);
  82. n %= 100;
  83. flag = true;
  84. }
  85. if (n < 10 && n > 0 && flag)
  86. {
  87. printf("%s", number[0]);
  88. }
  89. else if (n >= 10)
  90. {
  91. if (n / 10 == 1)
  92. printf("%s", number[10]);
  93. else
  94. printf("%s%s", number[n / 10], number[10]);
  95. n %= 10;
  96. }
  97. if (n > 0)
  98. printf("%s", number[n]);
  99. }

  • HJ96 表示数字(字符串)

  1. 输入:Jkdi234klowe90a3
  2. 输出:Jkdi*234*klowe*90*a*3*

示例代码:HJ96.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <ctype.h>
  4. int main(void)
  5. {
  6. int ch;
  7. bool in_num = false;
  8. while ((ch = getchar()) != '\n')
  9. {
  10. if (isdigit(ch) && !in_num)
  11. {
  12. putchar('*');
  13. putchar(ch);
  14. in_num = true;
  15. }
  16. else if (!isdigit(ch) && in_num)
  17. {
  18. putchar('*');
  19. putchar(ch);
  20. in_num = false;
  21. }
  22. else
  23. putchar(ch);
  24. }
  25. if (in_num)
  26. putchar('*');
  27. return 0;
  28. }

  • HJ97 记负均正(数组)

  1. 输入:
  2. 11
  3. 1 2 3 4 5 6 7 8 9 0 -1
  4. 输出:
  5. 1 5.0

示例代码:HJ97.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int n;
  5. int num;
  6. int count_p = 0, count_n = 0;
  7. int total = 0;
  8. int i;
  9. float average;
  10. scanf("%d", &n);
  11. for (i = 0; i < n; i++)
  12. {
  13. scanf("%d", &num);
  14. if (num < 0)
  15. count_n++;
  16. else if (num > 0)
  17. {
  18. total += num;
  19. count_p++;
  20. }
  21. }
  22. if (total > 0)
  23. average = (float) total / count_p;
  24. else
  25. average = 0.0;
  26. printf("%d %.1f\n", count_n, average);
  27. return 0;
  28. }

  • HJ98 自动售货系统(字符串)

  1. 输入:
  2. r 22-18-21-21-7-20 3-23-10-6;c;q0;p 1;b A6;c;b A5;b A1;c;q1;p 5;
  3. 输出:
  4. S001:Initialization is successful
  5. E009:Work failure
  6. E010:Parameter error
  7. S002:Pay success,balance=1
  8. E008:Lack of balance
  9. 1 yuan coin number=1
  10. 2 yuan coin number=0
  11. 5 yuan coin number=0
  12. 10 yuan coin number=0
  13. E008:Lack of balance
  14. E008:Lack of balance
  15. E009:Work failure
  16. E010:Parameter error
  17. S002:Pay success,balance=5

示例代码:HJ98.c

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #define PRONUM 6
  6. char* s_gets(char *st, int n);
  7. int main(void)
  8. {
  9. int pro_price[PRONUM] = {2, 3, 4, 5, 8, 6};
  10. int pro_num[PRONUM] = {0};
  11. int money_box[4] = {0}; // 1, 2, 5, 10
  12. char str[1000];
  13. int tag_space = 0;
  14. int num, blance = 0; // blance余额
  15. bool sold_out;
  16. int i, j;
  17. while (s_gets(str, 1000) != NULL)
  18. {
  19. int len = strlen(str);
  20. for (i = 0; i < len; i++)
  21. {
  22. while (str[i] != ';' && str[i] != '\0')
  23. {
  24. if (str[i] == 'r') // 初始化钱盒,商品数量
  25. {
  26. i++;
  27. tag_space = 0; // 判断命令与参数之间是否有空格
  28. while (str[i] == ' ')
  29. {
  30. i++;
  31. tag_space = 1;
  32. }
  33. if (tag_space == 0)
  34. {
  35. printf("E010:Parameter error\n");
  36. while (str[i] != ';')
  37.     i++;
  38. break;
  39. }
  40. for (j = 0; j < PRONUM; j++) // 商品数量初始化
  41. {
  42. num = 0;
  43. while (str[i] != '-' && !isspace(str[i]))
  44. {
  45. num = num * 10 + (str[i] - '0');
  46. i++;
  47. }
  48. i++;
  49. pro_num[j] = num;
  50. }
  51. while (str[i] == ' ')
  52. i++;
  53. for (j = 0; j < 4; j++) // 钱盒初始化
  54. {
  55. num = 0;
  56. while (str[i] != '-' && str[i] != ';')
  57. {
  58. num = num * 10 + (str[i] - '0');
  59. i++;
  60. }
  61. money_box[j] = num;
  62. if (str[i] == ';')
  63. break;
  64. i++;
  65. }
  66. blance = 0;
  67. printf("S001:Initialization is successful\n");
  68. }
  69. else if (str[i] == 'p') // 投币
  70. {
  71. i++;
  72. tag_space = 0;
  73. while (str[i] == ' ')
  74. {
  75. i++;
  76. tag_space = 1;
  77. }
  78. if (tag_space == 0)
  79. {
  80. printf("E010:Parameter error\n");
  81. while (str[i] != ';')
  82. i++;
  83. break;
  84. }
  85. num = 0;
  86. while (str[i] != ';') // 投币面值
  87. {
  88. num = num * 10 + (str[i] - '0');
  89. i++;
  90. }
  91. sold_out = true;
  92. for (j = 0; j < PRONUM; j++)
  93. {
  94. if (pro_num[j])
  95. sold_out = false;
  96. }
  97. if (num != 1 && num != 2 && num != 5 && num != 10)
  98. printf("E002:Denomination error\n");
  99. else if (num > 2 && 1 * money_box[0] + 2 * money_box[1] < num)
  100. printf("E003:Change is not enough, pay fail\n");
  101. else if (sold_out)
  102. printf("E005:All the goods sold out\n");
  103. else if (num == 1)
  104. {
  105. money_box[0]++;
  106. blance += num;
  107. printf("S002:Pay success,balance=%d\n",blance);
  108. }
  109. else if (num == 2)
  110. {
  111. money_box[1]++;
  112. blance += num;
  113. printf("S002:Pay success,balance=%d\n",blance);
  114. }
  115. else if (num == 5)
  116. {
  117. money_box[2]++;
  118. blance += num;
  119. printf("S002:Pay success,balance=%d\n",blance);
  120. }
  121. else if (num == 10)
  122. {
  123. money_box[3]++;
  124. blance += num;
  125. printf("S002:Pay success,balance=%d\n",blance);
  126. }
  127. }
  128. else if (str[i] == 'b') // 购买商品
  129. {
  130. i++;
  131. tag_space = 0;
  132. while (str[i] == ' ')
  133. {
  134. i++;
  135. tag_space = 1;
  136. }
  137. if (tag_space == 0)
  138. {
  139. printf("E010:Parameter error\n");
  140. while (str[i] != ';')
  141. i++;
  142. break;
  143. }
  144. num = 0;
  145. i++; // 跳过字母A
  146. while (str[i] != ';')
  147. {
  148. num = num * 10 + (str[i] - '0');
  149. i++;
  150. }
  151. if (num > 6)
  152. printf("E006:Goods does not exist\n");
  153. else if (pro_num[num - 1] == 0)
  154. printf("E007:The goods sold out\n");
  155. else if (blance >= pro_price[num - 1])
  156. {
  157. pro_num[num - 1]--;
  158. blance -= pro_price[num -1];
  159. printf("S003:Buy success,balance=%d\n",blance);
  160. }
  161. else
  162. printf("E008:Lack of balance\n");
  163. }
  164. else if (str[i] == 'c') // 退币
  165. {
  166. i++;
  167. if (blance == 0)
  168. printf("E009:Work failure\n");
  169. else
  170. {
  171. int ref1 = 0, ref2 = 0, ref5 = 0, ref10 = 0;
  172. bool change;
  173. while (blance > 0)
  174. {
  175. change = false;
  176. if(money_box[0] == 0 && money_box[1] == 0 && money_box[2] == 0
  177. && money_box[3] == 0)
  178. {
  179. break;
  180. }
  181. else if (blance >= 10 && money_box[3] > 0)
  182. {
  183. money_box[3]--;
  184. blance -= 10;
  185. ref10++;
  186. change = true;
  187. }
  188. else if (blance >= 5 && money_box[2] > 0)
  189. {
  190. money_box[2]--;
  191. blance -= 5;
  192. ref5++;
  193. change = true;
  194. }
  195. else if (blance >= 2 && money_box[1] > 0)
  196. {
  197. money_box[1]--;
  198. blance -= 2;
  199. ref2++;
  200. change = true;
  201. }
  202. else if (blance >= 1 && money_box[0] > 0)
  203. {
  204. money_box[0]--;
  205. blance -= 1;
  206. ref1++;
  207. change = true;
  208. }
  209. if (!change) // 表明无钱可退
  210. break;
  211. }
  212. printf("1 yuan coin number=%d\n",ref1);
  213. printf("2 yuan coin number=%d\n",ref2);
  214. printf("5 yuan coin number=%d\n",ref5);
  215. printf("10 yuan coin number=%d\n",ref10);
  216. }
  217. }
  218. else if (str[i] == 'q') // 显示 0 商品,1 钱盒
  219. {
  220. i++;
  221. tag_space = 0;
  222. while (str[i] == ' ')
  223. {
  224. i++;
  225. tag_space = 1;
  226. }
  227. if (tag_space == 0)
  228. {
  229. printf("E010:Parameter error\n");
  230. while (str[i] != ';')
  231. i++;
  232. break;
  233. }
  234. num = 0;
  235. while (str[i] != ';')
  236. {
  237. num = num * 10 + (str[i] - '0');
  238. i++;
  239. }
  240. if (num == 0)
  241. {
  242. for(j = 0; j < PRONUM; j++)
  243. printf("A%d %d %d\n",j+1, pro_price[j], pro_num[j]);
  244. }
  245. else if (num == 1)
  246. {
  247. printf("1 yuan coin number=%d\n",money_box[0]);
  248. printf("2 yuan coin number=%d\n",money_box[1]);
  249. printf("5 yuan coin number=%d\n",money_box[2]);
  250. printf("10 yuan coin number=%d\n",money_box[3]);
  251. }
  252. else
  253. printf("E010:Parameter error\n");
  254. }
  255. i++;
  256. }
  257. }
  258. }
  259. return 0;
  260. }
  261. char * s_gets(char * st, int n)
  262. {
  263. char * ret_val, * find;
  264. ret_val = fgets(st, n, stdin);
  265. if (ret_val)
  266. {
  267. find = strchr(st, '\n');
  268. if (find)
  269. *find = '\0';
  270. else
  271. while (getchar() != '\n')
  272. continue;
  273. }
  274. return ret_val;
  275. }

  • HJ99 自守数(基础数学)

  1. 输入:6
  2. 输出:4
  3. 说明:有0156这四个自守数

示例代码:HJ99.c

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #define LEN 20
  5. bool judge(int num);
  6. void print(int n);
  7. int main(void)
  8. {
  9. int n;
  10. scanf("%d", &n);
  11. print(n);
  12. return 0;
  13. }
  14. bool judge(int num)
  15. {
  16. char str_o[LEN], str_s[LEN];
  17. int i, j;
  18. sprintf(str_o, "%d", num);
  19. sprintf(str_s, "%d", num * num);
  20. for (i = (int) strlen(str_o) - 1, j = (int) strlen(str_s)- 1; i > -1; i--, j--)
  21. {
  22. if (str_o[i] != str_s[j])
  23. return false;
  24. }
  25. return true;
  26. }
  27. void print(int n)
  28. {
  29. int i;
  30. int count = 0;
  31. for (i = 0; i <= n; i++)
  32. {
  33. if (judge(i))
  34. count++;
  35. }
  36. printf("%d", count);
  37. }

  • HJ100 等差数列(基础数学)

  1. 输入:2
  2. 输出:7
  3. 说明:2+5=7

示例代码:HJ100.c

  1. #include <stdio.h>
  2. #define START 2
  3. #define DIFF 3
  4. int get_sum(int n);
  5. int main(void)
  6. {
  7. int n;
  8. scanf("%d", &n);
  9. printf("%d", get_sum(n));
  10. return 0;
  11. }
  12. int get_sum(int n)
  13. {
  14. return (n * ( 2 * START + (n - 1) * DIFF)) / 2;
  15. }

  • HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序(排序)

  1. 输入:
  2. 8
  3. 1 2 4 9 3 55 64 25
  4. 0
  5. 输出:
  6. 1 2 3 4 9 25 55 64

示例代码 1:HJ101.c

  1. #include <stdio.h>
  2. void sort(int ar[], int n, int choose);
  3. int main(void)
  4. {
  5. int n;
  6. int i;
  7. int choose;
  8. scanf("%d", &n);
  9. int ar[n];
  10. for (i = 0; i < n; i++)
  11. scanf("%d", ar + i);
  12. scanf("%d", &choose);
  13. sort(ar, n, choose);
  14. return 0;
  15. }
  16. void sort(int ar[], int n, int choose)
  17. {
  18. int i, j;
  19. int temp;
  20. if (!choose)
  21. {
  22. for (i = 0; i < n - 1; i++)
  23. {
  24. for (j = i + 1; j < n; j++)
  25. {
  26. if (ar[i] > ar[j])
  27. {
  28. temp = ar[i];
  29. ar[i] = ar[j];
  30. ar[j] = temp;
  31. }
  32. }
  33. printf("%d ", ar[i]);
  34. }
  35. printf("%d ", ar[i]);
  36. }
  37. else
  38. {
  39. for (i = 0; i < n - 1; i++)
  40. {
  41. for (j = i + 1; j < n; j++)
  42. {
  43. if (ar[i] < ar[j])
  44. {
  45. temp = ar[i];
  46. ar[i] = ar[j];
  47. ar[j] = temp;
  48. }
  49. }
  50. printf("%d ", ar[i]);
  51. }
  52. printf("%d ", ar[i]);
  53. }
  54. }

示例代码 2:HJ101.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int SBCompare(const void * p1, const void * p2);
  4. int BSCompare(const void * p1, const void * p2);
  5. int main(void)
  6. {
  7. int n;
  8. int i;
  9. int choose;
  10. scanf("%d", &n);
  11. int ar[n];
  12. for (i = 0; i < n; i++)
  13. scanf("%d", ar + i);
  14. scanf("%d", &choose);
  15. if (choose == 0)
  16. qsort(ar, n, sizeof(int), SBCompare);
  17. if (choose == 1)
  18. qsort(ar, n, sizeof(int), BSCompare);
  19. for (i = 0; i < n; i++)
  20. printf("%d ", ar[i]);
  21. return 0;
  22. }
  23. int SBCompare(const void * p1, const void * p2)
  24. {
  25. return *((int *) p1) - *((int *) p2);
  26. }
  27. int BSCompare(const void * p1, const void * p2)
  28. {
  29. return *((int *) p2) - *((int *) p1);
  30. }

  • HJ102 字符统计(字符串)

  1. 输入:aaddccdc
  2. 输出:cda
  3. 说明:样例里,c和d出现3次,a出现2次,但c的ASCII码比d小,所以先输出c,再输出d,最后输出a.

示例代码:HJ102.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #define SIZE 256
  5. #define LEN 1001
  6. int main(void)
  7. {
  8. static int count[SIZE];
  9. char str[LEN];
  10. int max = 0;
  11. int i;
  12. scanf("%s", str);
  13. for (i = 0; i < (int) strlen(str); i++)
  14. {
  15. count[toascii(str[i])]++;
  16. if (count[toascii(str[i])] > max)
  17. max = count[toascii(str[i])];
  18. }
  19. while (max)
  20. {
  21. for (i = 0; i < SIZE; i++)
  22. {
  23. if (max == count[i])
  24. printf("%c",i);
  25. }
  26. max--;
  27. }
  28. return 0;
  29. }

  • HJ103 Redraiment的走法(动态规划,最长子序列问题)

  1. 输入:
  2. 6
  3. 2 5 1 5 4 5
  4. 输出:
  5. 3
  6. 说明:
  7. 6个点的高度各为 2 5 1 5 4 5
  8. 如从第1格开始走,最多为3步, 2 4 5 ,下标分别是 1 5 6
  9. 从第2格开始走,最多只有1步,5
  10. 而从第3格开始走最多有3步,1 4 5, 下标分别是 3 5 6
  11. 从第5格开始走最多有2步,4 5, 下标分别是 5 6
  12. 所以这个结果是3

示例代码:HJ103.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LEN 200
  4. int get_max(int n, int ar[]);
  5. int main(void)
  6. {
  7. int n;
  8. int i;
  9. int * ar;
  10. while (scanf("%d", &n) == 1)
  11. {
  12. ar = (int *) malloc(sizeof(int) * n);
  13. for (i = 0; i < n; i++)
  14. scanf("%d", ar + i);
  15. printf("%d", get_max(n, ar));
  16. free(ar);
  17. }
  18. return 0;
  19. }
  20. int get_max(int n, int ar[])
  21. {
  22. int i, j;
  23. int step = 1;
  24. int dp[n];
  25. for (i = 0; i < n; i++) // 初始化,如果本身就是最大值则不再更改该值
  26. dp[i] = 1;
  27. for (i = 1; i < n; i++) // 终点为 i
  28. {
  29. for (j = 0; j < i; j++)
  30. {
  31. if (ar[i] > ar[j])
  32. dp[i] = (dp[i] > dp[j] + 1) ? dp[i] : dp[j] + 1;
  33. }
  34. step = (step > dp[i]) ? step : dp[i];
  35. }
  36. return step;
  37. }

  • HJ105 记负均正II(数组)

  1. 输入:
  2. -13
  3. -4
  4. -7
  5. 输出:
  6. 3
  7. 0.0

示例代码:HJ105.c

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. int num;
  5. int count_n = 0;
  6. int count_o = 0;
  7. int sum_o = 0;
  8. float average = 0.0;
  9. while (scanf("%d", &num) == 1)
  10. {
  11. if (num < 0)
  12. count_n++;
  13. else
  14. {
  15. count_o++;
  16. sum_o += num;
  17. }
  18. }
  19. if (sum_o > 0)
  20. average = (float) sum_o / count_o;
  21. printf("%d\n%.1f\n", count_n, average);
  22. return 0;
  23. }

  • HJ106 字符逆序(字符串)

  1. 输入:I am a student
  2. 输出:tneduts a ma I

示例代码:HJ106.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define LEN 10001
  4. char * s_gets(char * st, int n);
  5. int main(void)
  6. {
  7. char str[LEN];
  8. int i;
  9. if (s_gets(str, LEN) != NULL && *str != '\0')
  10. {
  11. for (i = (int) strlen(str) - 1; i > -1; i--)
  12. putchar(str[i]);
  13. }
  14. return 0;
  15. }
  16. char * s_gets(char * st, int n)
  17. {
  18. char * ret_val;
  19. char * find;
  20. ret_val = fgets(st, n, stdin);
  21. if (ret_val)
  22. {
  23. find = strchr(st, '\n');
  24. if (find)
  25. *find = '\0';
  26. else
  27. while (getchar() != '\n')
  28. continue;
  29. }
  30. return ret_val;
  31. }

  • HJ107 求解立方根(二分查找)

  1. 输入:19.9
  2. 输出:2.7

示例代码:HJ107.c

  1. #include<stdio.h>
  2. int main()
  3. {
  4. double val;
  5. double l = -20.0;
  6. double r = 20.0;
  7. double mid;
  8. scanf("%lf", &val);
  9. while (val - l * l * l > 0.001) // 从左侧逼近,不用考虑正负号,不错的策略
  10. {
  11. mid = (l + r) / 2;
  12. if (mid * mid * mid < val)
  13. l = mid;
  14. else
  15. r = mid;
  16. }
  17. printf("%.1lf", l);
  18. return 0;
  19. }

  • HJ108 求最小公倍数(基础数学)

  1. 输入:5 7
  2. 输出:35

示例代码 1:HJ108.c

  1. #include <stdio.h>
  2. int get_com(int x, int y);
  3. int main(void)
  4. {
  5. int m, n;
  6. while (scanf("%d%d", &m, &n) == 2)
  7. printf("%d\n", get_com(m, n));
  8. return 0;
  9. }
  10. int get_com(int x, int y)
  11. {
  12. int i;
  13. int com;
  14. for (i = 1; i <= y; i++)
  15. {
  16. if ((x * i) % y == 0)
  17. {
  18. com = x * i;
  19. break;
  20. }
  21. }
  22. return com;
  23. }

示例代码 2:HJ108.c

  1. #include <stdio.h>
  2. int gcd(int m , int n);
  3. int main(void)
  4. {
  5. int m, n;
  6. while (scanf("%d%d", &m, &n) == 2)
  7. printf("%d", (m * n) / gcd(m, n));
  8. return 0;
  9. }
  10. int gcd(int m , int n)
  11. {
  12. int temp;
  13. while (n)
  14. {
  15. temp = m % n;
  16. m = n;
  17. n = temp;
  18. }
  19. return m;
  20. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/174164?site
推荐阅读
相关标签
  

闽ICP备14008679号