[关闭]
@Jayfeather 2018-05-27T14:18:31.000000Z 字数 2727 阅读 1023

几道算法题

算法题


emmmm……本周刷了几道算法题……也看了一下数据结构,但是数据结构的学习笔记还没写出来。所以说……还是把算法题发上来吧……

第一道题P1067 多项式输出

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node {
  4. int n;
  5. int a;
  6. struct node *p;
  7. };
  8. struct node* pscanf();
  9. struct node* firstprint(struct node*);
  10. struct node* middleprint(struct node*);
  11. int main()
  12. {
  13. struct node* p1;
  14. p1=pscanf();
  15. p1=firstprint(p1);
  16. middleprint(p1);
  17. return 0;
  18. }
  19. struct node* pscanf()
  20. {
  21. struct node* p1, *p2;
  22. int n;
  23. scanf("%d", &n);
  24. p1 = p2 = (struct node*)malloc(sizeof(struct node));
  25. scanf("%d", &(p2->a));
  26. p2->n = n;
  27. p2->p = NULL;
  28. n = n - 1;
  29. while (n >= 0)
  30. {
  31. p2=p2->p = (struct node*)malloc(sizeof(struct node));
  32. scanf("%d", &(p2->a));
  33. p2->n = n;
  34. p2->p = NULL;
  35. n--;
  36. }
  37. return p1;
  38. }
  39. struct node* firstprint(struct node *p1)
  40. {
  41. if (p1->a == 0 && p1->n != 0)
  42. {
  43. p1 = p1->p;
  44. p1= firstprint(p1);
  45. }
  46. else if (p1->a == -1 && p1->n != 0)
  47. printf("-x^%d", p1->n);
  48. else if (p1->a < -1 && p1->n != 0)
  49. printf("%dx^%d", p1->a, p1->n);
  50. else if (p1->a == 1 && p1->n != 0)
  51. printf("x^%d", p1->n);
  52. else if (p1->a > 1 && p1->n != 0)
  53. printf("%dx^%d", p1->a, p1->n);
  54. p1 = p1->p;
  55. return p1;
  56. }
  57. struct node* middleprint(struct node *p1)
  58. {
  59. if (p1->a == 0 && p1->n > 1);
  60. else if (p1->a == -1 && p1->n > 1)
  61. printf("-x^%d", p1->n);
  62. else if (p1->a < -1 && p1->n > 1)
  63. printf("%dx^%d", p1->a, p1->n);
  64. else if (p1->a == 1 && p1->n > 1)
  65. printf("+x^%d", p1->n);
  66. else if (p1->a > 1 && p1->n > 1)
  67. printf("+%dx^%d", p1->a, p1->n);
  68. if (p1->n == 1)
  69. {
  70. if (p1->a == 0 );
  71. else if (p1->a == -1)
  72. printf("-x", p1->n);
  73. else if (p1->a < -1)
  74. printf("%dx", p1->a, p1->n);
  75. else if (p1->a == 1)
  76. printf("+x", p1->n);
  77. else if (p1->a > 1)
  78. printf("+%dx", p1->a, p1->n);
  79. }
  80. if (p1->n == 0)
  81. {
  82. if (p1->a < 0)
  83. printf("%d", p1->a);
  84. else if (p1->a == 0);
  85. else printf("+%d", p1->a);
  86. }
  87. else
  88. {
  89. p1 = p1->p;
  90. middleprint(p1);
  91. }
  92. return p1;
  93. }

感觉自己写的还是太冗长了,代码不够精简
是用链表实现的,但是感觉没有必要。
其实有更精简的写法
还是贴上来吧
需要多学习~

  1. #include<cstdio>
  2. using namespace std;
  3. int n,a[105];
  4. int abs(int n)
  5. {
  6. return n>0?n:-n;
  7. }
  8. int main()
  9. {
  10. scanf("%d",&n);
  11. for(int i=n;i>=0;i--)
  12. scanf("%d",&a[i]);
  13. bool flag=1;//判断多项式是否为零
  14. for(int i=n;i>=0;i--)
  15. {
  16. if(a[i]==0)continue;
  17. if(flag==0&&a[i]>0)printf("+");
  18. if(a[i]<0)printf("-");
  19. if(abs(a[i])!=1||i==0)printf("%d",abs(a[i]));
  20. if(i==0)continue;
  21. printf("x");
  22. if(i==1)continue;
  23. printf("^%d",i);
  24. flag=0;
  25. }
  26. if(flag==1)printf("0");
  27. printf("\n");
  28. return 0;
  29. }

同样是代码
但是……为什么别人寥寥几行代码就搞定了……自己写的这么复杂……
需要学习

P1540 机器翻译

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int M, N,front=0,end=0,count1=0,count2=0,answer=0,temp;
  5. int m[100];
  6. while (count1 < 100)
  7. {
  8. m[count1] = -1;
  9. count1++;
  10. }
  11. count1 = 0;
  12. scanf("%d %d", &M, &N);
  13. while (count1 < N)
  14. {
  15. scanf("%d", &temp);
  16. while (count2 < M)
  17. {
  18. if (m[count2] == temp)
  19. end++;
  20. count2++;
  21. }
  22. count2 = 0;
  23. if (end != 0)
  24. end = 0;
  25. else
  26. {
  27. m[front] = temp;
  28. front++;
  29. if (front == M)
  30. front = 0;
  31. answer++;
  32. }
  33. count1++;
  34. }
  35. printf("%d", answer);
  36. return 0;
  37. }

这道题的代码还行。比上个题目好多了
但是,但是,但是
看了看别人的代码,发现自己写的是啥……

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. using namespace std;
  5. int n,m,x,ans,l,r,a[1005],b[1005];
  6. int main()
  7. {
  8. cin>>m>>n;
  9. l=0;r=0;//初始化两个指针
  10. for (int i=1;i<=n;i++)
  11. {
  12. scanf("%d",&x);//边读入边做
  13. if (a[x]==0)
  14. {
  15. ans++;
  16. r++;b[r]=x;a[x]=1;//因为每次遇到新单词都要做这些操作,不如搬到判断语句外做,这样程序更简洁
  17. if (r>m) {l++;a[b[l]]=0;}
  18. }
  19. }
  20. cout<<ans;
  21. return 0;//千万不能忘记打这句,不然在比赛中会出错
  22. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注