本文主要是介绍【王道数据结构】【chapter2线性表】【P19t10~t14】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2010年统考
#include <iostream>void r(int *a,int l,int r)
{while(l<r){int tmp=a[l];a[l]=a[r];a[r]=tmp;l++,r--;}
}
void reverse(int *a,int length,int position)
{r(a,0,position-1),r(a,position,length),r(a,0,length);
}
int main() {int a[10]={1,2,3,4,5,6,7,8,9,10};reverse(a,9,5);for(int i=0;i<10;i++){printf("%3d",a[i]);}return 0;
}
2011年统考
#include <iostream>int mid_position(int *a,int*b,int length)
{int s1,d1,s2,d2;s1=0,s2=0,d1=length-1,d2=length-1;while(s1!=d1||s2!=d2){int m1=s1+d1>>1,m2=s2+d2>>1;if(a[m1]==b[m2]) return a[m1];if(a[m1]>b[m2]){if((s1+d1)%2==0){d1=m1;s2=m2;}else{d1=m1;s2=m2+1;}}else{if((s1+d1)%2==0){s1=m1;d2=m2;}else{s1=m1+1;d2=m2;}}}return std::min(a[s1],b[s2]);
}
int main() {int a[5]={11,13,15,17,19};int b[5]={2,4,6,8,20};printf("%3d\n",mid_position(a,b,5));return 0;
}
2013年统考
#include <iostream>int find_main(int*a,int length)
{int count=0,record=-1;for(int i=0;i<length;i++){if(count==0)count++,record=a[i];else{if(a[i]==record) count++;else{count--;}}}count=0;for(int i=0;i<length;i++){if(a[i]==record) count++;}if(count>=length/2) return record;else return -1;
}
int main() {int a[8]={0,5,5,3,5,7,5,5};printf("%d\n",find_main(a,8));return 0;
}
2018年统考
#include <iostream>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include<memory.h>
int find_min(int*a,int length,int n)
{int *b= (int*)malloc(n*sizeof (int));memset(b,0,sizeof (int)*n);for(int i=0;i<length;i++){if(a[i]>0&&a[i]<n) b[a[i]-1]=1;}for(int i=0;i<n;i++){if(b[i]==0) return i+1;}return 12;
}
int main() {srand(time(nullptr));int *a=(int*) calloc(10,sizeof (int));for(int i=0;i<10;i++){a[i]=rand()%12;printf("%3d",a[i]);}puts("");printf("%3d\n",find_min(a,10,12));return 0;
}
2020年统考
#include <iostream>int get_distance(int a,int b,int c)
{return abs(a-b)+abs(b-c)+abs(c-a);
}int find_closest(int* a,int l1,int * b,int l2,int * c,int l3)
{int dis=0x7fffffff;int i=0,j=0,k=0;dis= get_distance(a[i],b[j],c[k]);while(i<l1&&j<l2&&k<l3){int c1=(a[i]>b[j]?b[j]:a[i]);int tmp=c1>c[k]?c[k]:c1;if(a[i]==tmp) i++;if(b[j]==tmp) j++;if(c[k]==tmp) k++;int d= get_distance(a[i],b[j],c[k]);dis=dis> d?d:dis;}return dis;
}
int main() {int a[3]={-1,0,9};int b[4]={-25,-10,10,11};int c[5]={2,9,17,30,41};printf("%3d\n",find_closest(a,3,b,4,c,9));return 0;
}
这篇关于【王道数据结构】【chapter2线性表】【P19t10~t14】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!