@Jayfeather
2018-05-27T14:18:31.000000Z
字数 2727
阅读 1023
算法题
emmmm……本周刷了几道算法题……也看了一下数据结构,但是数据结构的学习笔记还没写出来。所以说……还是把算法题发上来吧……
第一道题P1067 多项式输出
#include<stdio.h>#include<stdlib.h>struct node {int n;int a;struct node *p;};struct node* pscanf();struct node* firstprint(struct node*);struct node* middleprint(struct node*);int main(){struct node* p1;p1=pscanf();p1=firstprint(p1);middleprint(p1);return 0;}struct node* pscanf(){struct node* p1, *p2;int n;scanf("%d", &n);p1 = p2 = (struct node*)malloc(sizeof(struct node));scanf("%d", &(p2->a));p2->n = n;p2->p = NULL;n = n - 1;while (n >= 0){p2=p2->p = (struct node*)malloc(sizeof(struct node));scanf("%d", &(p2->a));p2->n = n;p2->p = NULL;n--;}return p1;}struct node* firstprint(struct node *p1){if (p1->a == 0 && p1->n != 0){p1 = p1->p;p1= firstprint(p1);}else if (p1->a == -1 && p1->n != 0)printf("-x^%d", p1->n);else if (p1->a < -1 && p1->n != 0)printf("%dx^%d", p1->a, p1->n);else if (p1->a == 1 && p1->n != 0)printf("x^%d", p1->n);else if (p1->a > 1 && p1->n != 0)printf("%dx^%d", p1->a, p1->n);p1 = p1->p;return p1;}struct node* middleprint(struct node *p1){if (p1->a == 0 && p1->n > 1);else if (p1->a == -1 && p1->n > 1)printf("-x^%d", p1->n);else if (p1->a < -1 && p1->n > 1)printf("%dx^%d", p1->a, p1->n);else if (p1->a == 1 && p1->n > 1)printf("+x^%d", p1->n);else if (p1->a > 1 && p1->n > 1)printf("+%dx^%d", p1->a, p1->n);if (p1->n == 1){if (p1->a == 0 );else if (p1->a == -1)printf("-x", p1->n);else if (p1->a < -1)printf("%dx", p1->a, p1->n);else if (p1->a == 1)printf("+x", p1->n);else if (p1->a > 1)printf("+%dx", p1->a, p1->n);}if (p1->n == 0){if (p1->a < 0)printf("%d", p1->a);else if (p1->a == 0);else printf("+%d", p1->a);}else{p1 = p1->p;middleprint(p1);}return p1;}
感觉自己写的还是太冗长了,代码不够精简
是用链表实现的,但是感觉没有必要。
其实有更精简的写法
还是贴上来吧
需要多学习~
#include<cstdio>using namespace std;int n,a[105];int abs(int n){return n>0?n:-n;}int main(){scanf("%d",&n);for(int i=n;i>=0;i--)scanf("%d",&a[i]);bool flag=1;//判断多项式是否为零for(int i=n;i>=0;i--){if(a[i]==0)continue;if(flag==0&&a[i]>0)printf("+");if(a[i]<0)printf("-");if(abs(a[i])!=1||i==0)printf("%d",abs(a[i]));if(i==0)continue;printf("x");if(i==1)continue;printf("^%d",i);flag=0;}if(flag==1)printf("0");printf("\n");return 0;}
同样是代码
但是……为什么别人寥寥几行代码就搞定了……自己写的这么复杂……
需要学习
P1540 机器翻译
#include<stdio.h>int main(){int M, N,front=0,end=0,count1=0,count2=0,answer=0,temp;int m[100];while (count1 < 100){m[count1] = -1;count1++;}count1 = 0;scanf("%d %d", &M, &N);while (count1 < N){scanf("%d", &temp);while (count2 < M){if (m[count2] == temp)end++;count2++;}count2 = 0;if (end != 0)end = 0;else{m[front] = temp;front++;if (front == M)front = 0;answer++;}count1++;}printf("%d", answer);return 0;}
这道题的代码还行。比上个题目好多了
但是,但是,但是
看了看别人的代码,发现自己写的是啥……
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;int n,m,x,ans,l,r,a[1005],b[1005];int main(){cin>>m>>n;l=0;r=0;//初始化两个指针for (int i=1;i<=n;i++){scanf("%d",&x);//边读入边做if (a[x]==0){ans++;r++;b[r]=x;a[x]=1;//因为每次遇到新单词都要做这些操作,不如搬到判断语句外做,这样程序更简洁if (r>m) {l++;a[b[l]]=0;}}}cout<<ans;return 0;//千万不能忘记打这句,不然在比赛中会出错}