數據結構13,15,16,18,19

13
#include <stdio.h>
int s[]={10,15,31,9,20,22,34,37,6,15,42};
int r2[20];
void merge(int low ,int m ,int high,int r[])
{
  int i=low,j=m+1,p=0;
 // int r2[high-low+1];
  while(i<=m && j<=high)
  {
    if(r[i]<=r[j])
    {
      r2[p]=r[i];
      i++;
    }
    else
    {
      r2[p]=r[j];
      j++;
    }
    ++p;
  }
  while(i<=m)
    r2[p++]=r[i++];
  while(j<=high)
    r2[p++]=r[j++];
  for(int t=0;t<p;t++)
  {
    printf("%d ",r2[t]);
  }
}
main()
{
  int temp[20];
  merge(0,2,4,s);
  for(int k1=0;k1<5;k1++)
    temp[k1]=r2[k1];
  merge(5,7,10,s);
  for(int k2=5;k2<11;k2++)
    temp[k2]=r2[k2-5];
  printf("/nThe result is:");
  merge(0,4,10,temp);
}

15#include <stdio.h>
#define stacksize 100
typedef struct{
  int data[stacksize];
  int top;
}Seqstack;
void push(Seqstack *s,int x)
{
  if(s->top==stacksize-1)
  {
    printf("The stack is full");
    return ;
    }
  s->data[++s->top]=x;
}
int pop(Seqstack *s)
{
  return s->data[s->top--];
}
void MultiBaseOutput(int n,int b)
{
  int i;
  Seqstack *s;
  s->top=-1;
  if(n==0)
    printf("0");
  while(n)
  {
     push(s,n%b);
     n=n/b;
   }
   while(s->top!=-1)
   {
     i=pop(s);
     printf("%d",i);
   }
}
main()
{
  MultiBaseOutput(10,2);
}

16
#include <stdio.h>
main()
{
  int A[1000],B[1000],C[1000],lena,lenb,k,temp,tempc[1000],count,temp1;
  printf("Input A:");
  for(int i=0;i<1000;i++)
  {
    scanf("%d",&A[i]);
    if(A[i]==-9999)
    {
      lena=i;
      break;
    }
  }
  printf("Input B:");
  for(int j=0;j<1000;j++)
  {
    scanf("%d",&B[j]);
    if(B[j]==-9999)
    {
      lenb=j;
      break;
     }
   }
  /*--------get A-B ,included same numbers-----------------*/
  k=0;
  for(int t=0;t<lena;t++)
    for(int m=0;m<lenb;m++)
    {
      if(A[t]==B[m])
      {
    tempc[k]=A[t];
     //    printf("%d ",tempc[k]);
    k++;
      }
    }
       // printf("k=%d",k);
   /*------------delete same numbers ------------------------*/
   count=0;
   temp=k;
   for(int x=0;x<temp-1-count;x++)
   {
     for(int y=x+1;y<temp-count;y++)
       if(tempc[x]==tempc[y])
       {
     count++;
     for(int z=y;z<temp-count;z++)
     {
       tempc[z]=tempc[z+1];
     }
     y--;
       }
   }
   temp1=temp-count;
/*----------to sort--------------------*/
  for(int r=0;r<temp1-1;r++)
  for(int c=r+1;c<temp1;c++)
  {
    if(tempc[r]>tempc[c])
    {
      temp=tempc[r];
      tempc[r]=tempc[c];
      tempc[c]=temp;
    }
  }
  /*----------to show-------------------*/
   printf("The results are:/n");
   for(int f=0;f<temp1;f++)
   {
     printf("%d ",tempc[f]);
   }
}

18
#include <stdio.h>
main()
{
  int num[2000],k,temp,times=0;
  for(int i=1;i<2000;i++)
  {
    scanf("%d",&num[i]);
    if(num[i]==-9999)
    {
      k=i;
   //   printf("i=%d",i);
      break;
     }
   }
   for(int j=2;j<k;j++)
   {
    // num[0]=num[j];
    if(num[j]<num[j-1])
    {
      num[0]=num[j];
      temp=j-1;
      do{
    times++;
    num[temp+1]=num[temp];
    temp--;
    }while(num[0]<num[temp]);
      num[temp+1]=num[0];
     }
     else
       times++;
   }
   printf("/n The ordre is:");
   for(int e=1;e<k;e++)
     printf("%d ",num[e]);
   printf("/nThe compaired times are:%d",times);
}


19
#include <stdio.h>
main()
{
  int A[1000],B[1000],C1[1000],C[1000],numa,numb,q,f,temp,temp1,count;
  printf("Please input A(end with -9999):");
  for(int i=0;i<1000;i++)
  {
    scanf("%d",&A[i]);
    if(A[i]==-9999)
    {
      numa=i;
      break;
     }
   }
   printf("Please input B(end with -9999):");
   for(int j=0;j<1000;j++)
   {
     scanf("%d",&B[j]);
     if(B[j]==-9999)
     {
       numb=j;
       break;
     }
   }
   /*find out A-B ,included same nums*/
   q=0;
   for(int r=0;r<numa;r++)
   {
     temp=A[r];
     f=0;
     for(int t=0;t<numb;t++)
     {
       if(temp!=B[t])
     f++;
     }
     if(f==numb)
     {
       C[q]=temp;
 //      printf("%d ",temp);
       q++;
     }
   }
   /*find out different nums in A-B */
   printf("The result of A-B are:");
   temp=q;
   count=0;
   for(int x=0;x<temp-1-count;x++)
   {
     for(int y=x+1;y<temp-count;y++)
       if(C[x]==C[y])
       {
     count++;
     for(int z=y;z<temp-count;z++)
     {
       C[z]=C[z+1];
     }
     y--;
       }
   }
   temp1=temp-count;
   /*---------sort C ------------*/
   for(int d=0;d<temp1;d++)
    for(int m=d+1;m<temp1;m++)
    {
      if(C[d]>C[m])
      {
    temp=C[d];
    C[d]=C[m];
    C[m]=temp;
      }
    }
    for(int u=0;u<temp1;u++)
      printf("%d ",C[u]);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章