码迷,mamicode.com
首页 > 其他好文 > 详细

2015数据结构上机考试题解

时间:2016-06-22 23:42:23      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
#include<cstdio>

#define inf 0x3f3f3f3f

const int maxn=10000;

using namespace std;

int n,m,q;

int a[maxn+10];

int bs(int x){
   int l=0,r=n-1;
   while(l<=r){
        int mid=(l+r)>>1;
        if(a[mid]==x) return 1;
        else if(a[mid]>x) r=mid-1;
        else l=mid+1;
   }
   return 0;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    scanf("%d",&m);
    while(m--){
        scanf("%d",&q);
        if(bs(q)) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
1001
技术分享
#include<cstdio>

#define inf 0x3f3f3f3f

const int maxn=10000;

using namespace std;

int n,a[maxn+10];

void QuickSort(int a[],int l,int r){
     if(l<r){
        int t=a[l];
        int low=l,high=r;
        while(low<high){
            while(low<high&&a[high]>=t) high--;
            a[low]=a[high];
            while(low<high&&a[low]<=t) low++;
            a[high]=a[low];
        }
        a[low]=t;
        QuickSort(a,l,low-1);
        QuickSort(a,low+1,r);
     }
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    QuickSort(a,1,n);
    for(int i=1;i<=n;i++) printf("%d\n",a[i]);
    return 0;
}
1003
技术分享
#include <cstdio>
#include <string.h>
#include <stdlib.h>
#define inf 0x3f3f3f3f

const int maxn=1000;

using namespace std;

typedef struct node{
  int data;
  struct node *lch,*rch;
}bstnode,*bstree;

int n,v,cnt,x;

bool searchbst(bstree &t,int v,bstree f,bstree &p){
        if(!t){
         p=f;
         return 0;
        } else {
            if(t->data==v) {
                p=t;
                return 1;
            } else if(t->data>v) return searchbst(t->lch,v,t,p);
            else return searchbst(t->rch,v,t,p);
        }
}

void bstinsert(bstree &t,int v){
     bstree s,p;
     if(!searchbst(t,v,NULL,p)){
        s=(bstnode*)malloc(sizeof(bstnode));
        s->lch=s->rch=NULL;
        s->data=v;
        if(!p) {
                t=s;
        } else if(s->data>p->data) p->rch=s;
        else p->lch=s;
     }
}

void pre_order(bstree t){
   if(t){
        if(t->data>=x)
        {printf("%d\n",t->data);cnt++;}
        pre_order(t->lch);
        pre_order(t->rch);
   }
}

int main()
{
    bstree t=NULL;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&v);
        bstinsert(t,v);
    }
    scanf("%d",&x);
    pre_order(t);
    printf("%d\n",cnt);
    return 0;
}
1015
技术分享
#include<cstdio>

#define inf 0x3f3f3f3f

const int maxn=1000;

using namespace std;

int n;

int flag[maxn+10];

int cost[maxn+10][maxn+10];

int d[maxn+10];

void dijkstra(int s){
     for(int i=1;i<=n;i++) d[i]=10000;
     d[s]=0;
     //flag[s]=1;
     while(1){
        int v=-1;
        for(int u=1;u<=n;u++) if(!flag[u]&&(v==-1||d[u]<d[v])) v=u;
        if(v==-1) break;
        flag[v]=1;
        for(int u=1;u<=n;u++){
                if(!flag[u]&&d[u]>d[v]+cost[v][u]){
                        d[u]=d[v]+cost[v][u];
        }
        }
     }
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        scanf("%d",&cost[i][j]);
    dijkstra(1);
    for(int i=1;i<=n;i++)
        if(i==1) printf("%d",d[i]);
    else printf(" %d",d[i]);
    printf("\n");
    return 0;
}
1440
技术分享
#include <cstdio>
#include <string.h>
#define inf 0x3f3f3f3f

const int maxn=10000;

using namespace std;

int f;

char a[maxn+10];

int main()
{
    scanf("%s",a);
    for(int i=0,j=strlen(a)-1;i<j;i++,j--){
        if(a[i]!=a[j]){
                f=1;
                printf("No\n");
        }
    }
    if(!f) printf("Yes\n");
    return 0;
}
1480
技术分享
#include <iostream>
#include <cstdio>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f

const int maxn=1000;

using namespace std;

typedef struct node{
   char a;
   int dep;
   int code;
   int v;
   struct node *lch,*rch;
   friend bool operator<(struct node a,struct node b){
        return a.v>b.v;
   }
}hnode,*htree;

htree root;

int inde,count_num;

hnode Node[maxn+10];

char a[maxn+10],b[maxn+10];

int deep[maxn+10],cod[maxn+10];

priority_queue<hnode> pq;

void haffman(){
    for(int i=0;i<=inde;i++)
        pq.push(Node[i]);
    while(pq.size()>1){
        //printf("1\n");
        htree h1=(hnode*)malloc(sizeof(hnode));
        *h1=pq.top();pq.pop();
        htree h2=(hnode*)malloc(sizeof(hnode));
        *h2=pq.top();pq.pop();
        hnode h3;
        h3.a=0;
        h3.lch=h1,h3.rch=h2;
        h3.v=h1->v+h2->v;
        pq.push(h3);
    }
    *root=pq.top();
    pq.pop();
    root->code=0;
    root->dep=0;
    queue<node> q;
    q.push(*root);
    while(!q.empty()){
        //printf("1\n");
        node h=q.front();
        //printf("%c\n",h.a);
        q.pop();
        if(h.a!=0) {
             //printf("%c %d\n",h.a,h.code);
             deep[h.a-a]=h.dep;
             cod[h.a-a]=h.code;
        }
        if(h.lch!=NULL){
              //printf("100\n");
              h.lch->dep=h.dep+1;
              h.lch->code=h.code*10;
              //printf("%c\n",h.lch->a);
              q.push(*h.lch);
        }
        if(h.rch!=NULL){
                //printf("100\n");
                h.rch->dep=h.dep+1;
                h.rch->code=h.code*10+1;
                //printf("%d\n",h.code*10+1);
                q.push(*h.rch);
        }
    }
}

int main()
{
    gets(a);
    root=(hnode*)malloc(sizeof(htree));
    inde=0,count_num=1;
    sort(a,a+strlen(a));
    for(int i=1;i<strlen(a);i++){
        if(a[i]!=a[i-1]){
               Node[inde].v=count_num;
               Node[inde++].a=a[i-1];
               count_num=1;
        } else count_num++;
    }
    Node[inde].a=a[int(strlen(a))-1];
    Node[inde].v=count_num;
   // printf("%d\n",inde);
    //printf("%c\n",a[strlen(a)-1]);
    haffman();
    gets(b);
    for(int i=0;i<strlen(b);i++){
        printf("%0*d",deep[b[i]-a],cod[b[i]-a]);
        //printf("%c %d %d\n",b[i],deep[b[i]-‘a‘],cod[b[i]-‘a‘]);
    }
    printf("\n");
    cout<<b<<endl;
    return 0;
}
1520

 

2015数据结构上机考试题解

标签:

原文地址:http://www.cnblogs.com/GeniusYang/p/5608801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!