好文档 - 专业文书写作范文服务资料分享网站

应聘华为软件的上机试题及部分答案

天下 分享 时间: 加入收藏 我要投稿 点赞

{head=p->next;delete p;} else

{s->next=p->next;delete p;} } else

cout<<\未找到要删除的节点\<

node *insert(node *head,int num)//要插入和删除的时候要考虑判断特殊位置点 { node *p,*s,*t; p=head;

t=new node[1]; t->data=num;

while(p->datanext!=NULL) {s=p;p=p->next;} if(num<=p->data){ if(p==head)

{t->next=p;head=s;} else

{t->next=p;s->next=t;} }

else //把插入点定位在了链尾 {p->next=t;t->next=NULL;} return head; }

node *sort(node* head) { node *p; int temp;

int n=sizeof(head)/sizeof(node); if(head==NULL&&head->next==NULL) return head; p=head;

for(int i=0;i

for(int j=0;j

if(p->data>p->next->data) { temp=p->data;

p->data=p->next->data; p->next->data=temp; }}} return head; }

node *sort(node *head) {

node *p;

int temp; int n;

n=length(head);

if(head==NULL&&head->next==NULL) return head; for(int i=0;i

p=head;

for(int j=0;j

if(p->next!=NULL) {

if(p->data>p->next->data) {

temp=p->data;

p->data=p->next->data; p->next->data=temp; }

p=p->next; } } }

cout<<\ return head; }

int main() {

node *p1; p1=creat(); sort(p1); print(p1); cout<

16、栈的基本操作:

struct list{ int data;

struct node *next; }*node;

void initstack(node &top) {

top=NULL; }

void push(node &top,int num) { node *p; p=new node; p->data=num; p->next=top; top=p; }

void pop(node &top) { node *p=top; int n=top->data; top=top->next; cout<

int Isempty(node &top) {

return top==NULL; }

17、改进后的冒泡排序:

oid maopao(int *s,int n) { int i=1,j,b=1; int temp; while(b){ b=0;

for(j=n-i-1;j>i;j--) if(s[j]

b=1;

temp=s[j]; s[j]=s[j-1]; s[j-1]=temp; }

for(j=i;js[j+1]) { b=1;

temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } i++; } }

18、扫描数组,求出最大的增量长度:

int main()

{ int a[]={7,2,3,4,5,6,8}; int n=7;

int max=1,len=1; for(int i=1;i

if(a[i]>=a[i-1]) { len++;

continue; //跳出本次循环,继续以下的部分

}

if(max

cout<

19、扫描整数序列,求出最大长度子序列:

思路:定义两个游标i j分别在数组的左右,从两边收缩,计算i j之间数组的总和,然后比较,求出最大的记录,并将此时的i j 记录下来。 int main()

{ int s[]={1,20,-3,49,59,10,30,20,-41,90,-2,-30,60.-29}; int n=sizeof(s)/sizeof(int); int i,k,j,a,b,num,max=0; for( i=0;i

for( j=n-1;j>=i;j--) { num=0;

for( k=i;k<=j;k++) num=num+s[k]; if(max

max=num; a=i; b=j; } }

for(int m=a;m<=b;m++) cout<

20、直接插入排序:

void insertsort(int a[],int n) { int x,j;

for(int i=1;i

for( j=i-1;j>=0&&x>a[j];j--) a[j+1]=a[j]; a[j+1]=x; }

21、提取字符串中的数字:

在字符串\中提取的字符是0,123,456,253,5。 void fun( char str[], int outArray[]) { char *p=str;

int count=0,value=0; while(*p!='\\0')

{

if(*p>='0'&&*p<='9')

{

value=0;

while(*p>='0'&&*p<='9')

{

value=value*10+(*p-'0'); p++;

}

outArray[count++]=value; } p++;

} }

22、数字转换成字符串:

int main()

{ char str[20],temp[20]; int i=0,j=0; int num=; while(num>0){

str[i]=num+'0'; num=num/10; i++; } i--;

while(i>=0){

temp[j++]=str[i--]; }

temp[j]='\\0'; cout<

430zv2lg3m2i4cx3q5al1oirv327pb00po7
领取福利

微信扫码领取福利

微信扫码分享