[关闭]
@sensitive-cs 2016-11-21T15:24:30.000000Z 字数 1523 阅读 900

2016||11||21

历程

今天学习了最长回文子串和高精度乘法,6174问题

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

6174:

  1. #include <stdio.h>
  2. #include <string.h>
  3. int ans[1005];
  4. int get_next(int x)
  5. {
  6. char a[10];
  7. sprintf(a,"%d",x);
  8. int i,j;
  9. int b,c;
  10. int m = strlen(a);
  11. for (i = 0;i < m;i++)
  12. {
  13. for (j = i + 1;j < m;j++)
  14. {
  15. if (a[i] > a[j])
  16. {
  17. char t = a[i];
  18. a[i] = a[j];
  19. a[j] = t;
  20. }
  21. }
  22. }
  23. sscanf(a,"%d",&b);
  24. for (i = 0;i < m / 2;i++)
  25. {
  26. char t = a[i];
  27. a[i] = a[m - 1 - i];
  28. a[m - 1 - i] = t;
  29. }
  30. sscanf(a,"%d",&c);
  31. return c - b;
  32. }
  33. int main(void)
  34. {
  35. int x;
  36. int lenth = 0;
  37. int flag = 1;
  38. scanf("%d",&x);
  39. ans[0] = x;
  40. while (flag)
  41. {
  42. int t = get_next(ans[lenth]);
  43. int j;
  44. for (j = 0;j <= lenth;j++)
  45. {
  46. if (ans[j] == t)
  47. {
  48. flag = 0;
  49. printf("%d -> %d\n",t,t);
  50. break;
  51. }
  52. }
  53. if (flag)
  54. {
  55. ans[++lenth] = t;
  56. printf("%d - >",ans[lenth-1]);
  57. }
  58. }
  59. return 0;
  60. }

这题刘汝佳用的是一个数组就解决问题,貌似我也是,233333,但是解法不一样。。。

想要说明的是对于最长回文字串的下标,采取手算,手算万岁。
Orz,刘汝佳

还有字母重排,算竞78页(远古)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. char a[100][20];
  5. void tosard(char *ch)
  6. {
  7. int i,j;
  8. for (i = 0;i < strlen(ch);i++)
  9. {
  10. for (j = i + 1;j < strlen(ch);j++)
  11. {
  12. if (ch[i] > ch[j])
  13. {
  14. char t = ch[i];
  15. ch[i] = ch[j];
  16. ch[j] = t;
  17. }
  18. }
  19. }
  20. }
  21. int main(void)
  22. {
  23. int i = 0,j,k;
  24. char b[20];
  25. while (scanf(" %s",a[i]) != EOF && a[i][0] != '*')
  26. i++;
  27. for (j = 0;j < i;j++)
  28. {
  29. for (k = j + 1;k < i;k++)
  30. {
  31. if (strcmp(a[j],a[k]) > 0)
  32. {
  33. char p[20];
  34. strcpy(p,a[j]);
  35. strcpy(a[j],a[k]);
  36. strcpy(a[k],p);
  37. }
  38. }
  39. }
  40. while (scanf(" %s",b) != EOF)
  41. {
  42. char c[20];
  43. strcpy(c,b);
  44. tosard(c);
  45. for (j = 0;j < i;j++)
  46. {
  47. char d[20];
  48. strcpy(d,a[j]);
  49. tosard(d);
  50. if (strcmp(c,d) == 0)
  51. printf("%s ",a[j]);
  52. }
  53. }
  54. return 0;
  55. }

略显繁琐,lrj的代码用到了qsort,值得学习。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注