卓越14U14716彭佳伟第十九周.docx

上传人:b****7 文档编号:10459123 上传时间:2023-02-11 格式:DOCX 页数:15 大小:17.20KB
下载 相关 举报
卓越14U14716彭佳伟第十九周.docx_第1页
第1页 / 共15页
卓越14U14716彭佳伟第十九周.docx_第2页
第2页 / 共15页
卓越14U14716彭佳伟第十九周.docx_第3页
第3页 / 共15页
卓越14U14716彭佳伟第十九周.docx_第4页
第4页 / 共15页
卓越14U14716彭佳伟第十九周.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

卓越14U14716彭佳伟第十九周.docx

《卓越14U14716彭佳伟第十九周.docx》由会员分享,可在线阅读,更多相关《卓越14U14716彭佳伟第十九周.docx(15页珍藏版)》请在冰豆网上搜索。

卓越14U14716彭佳伟第十九周.docx

卓越14U14716彭佳伟第十九周

12.5编写一个测试一个字符串是否为回文的递归函数,是回文,返回1;不是回文,返回0。

voidjudge(char*str,intn)

{

intstart=strlen(str)-n;

intend=n-1;

if(str[start]==str[end])

{

if(end-start>1)

{

judge(str,n-1);

}

else

{

printf("1\n");

}

}

else

{

printf("0\n");

}

}

13.1编写带哨兵的直接插入排序算法,将哨兵放在a[0]中,被排序的纪录放在a[1]~a[n-1]中。

#include

#ifndefMAX

#defineMAX8

#endif

intmain(intargc,charconst*argv[])

{

inta[MAX]={0,1,2,5,3,6,3,-2};

intj,i;

inttmp;

for(i=2;i

{

a[0]=a[i];

for(j=i-1;a[0]

{

a[j+1]=a[j];

}

a[j+1]=a[0];

}

for(i=1;i

{

printf("%d\n",a[i]);

}

return0;

}

13.6改写快速排序算法,要求采用三者(首元素、中间元素、尾元素)取中的方式选择分区元素;若当前被排序的去见长度小雨等于3,无需划分而是采用直接插入方式对其排序。

voidQuickSort(inta[],intleft,intright)

{

intsplit;

if(right-left>3)

{

split=Partition(a,left,right);

QuickSort(a,left,split-1);

QuickSort(a,split+1,right);

}

elseif(right-left<=3&&right-left>0)

{

InsertSort(a,left,right);

}

}

intPartition(inta[],intleft,intright)

{

intsplit=(left+right)/2;

swap(a,left,split);

inti=left;

intj=right+1;

while

(1)

{

while(a[++i]<=a[left]&&i<=right);

while(a[--j]>a[left]);

if(i>=j)

{

break;

}

swap(a,i,j);

}

swap(a,j,left);

returnj;

}

voidswap(inta[],inti,intj)

{

inttemp;

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

voidInsertSort(inta[],intleft,intright)

{

inti;

intj;

inttemp;

for(i=left+1;i<=right;i++)

{

temp=a[i];

for(j=i-1;j>=0&&a[j]>temp;j--)

{

a[j+1]=a[j];

}

a[j+1]=temp;

}

}

14.4输入一行长度无限制超长字符串,用一个先进先出且每个节点接受一个输入字符的单向链表接受这个字符串。

再完成下列任务:

(1)遍历输出链表中的所有字符;

(2)将该超长字符串无冗余地存放到一个通过动态存储分配创建地字符数组中,并且通过puts函数或printf函数输出该超长字符串。

#include

#include

typedefstructStringString;

structString

{

charch;

String*next;

};

voidSaveSring(String**head);

voidPrintString(String*head);

voidSaveToArray(String*head);

intmain(void)

{

String*head=NULL;

printf("Pleaseinputastring\n");

SaveSring(&head);

PrintString(head);

SaveToArray(head);

return0;

}

voidSaveSring(String**head)

{

String*current=NULL;

String*previous=NULL;

while

(1)

{

current=(String*)malloc(sizeof(String));

if(*head==NULL)

{

*head=current;

}

if(previous!

=NULL)

previous->next=current;

previous=current;

current->next=NULL;

scanf("%c",¤t->ch);

if(current->ch=='\n')

{

current->ch='\0';

current->next=NULL;

break;

}

}

}

voidPrintString(String*head)

{

while(head!

=NULL)

{

printf("%c",head->ch);

head=head->next;

}

printf("\n");

}

voidSaveToArray(String*head)

{

intcnt;

String*temp=head;

for(cnt=1;temp->next!

=NULL;cnt++)

{

temp=temp->next;

}

charstring[cnt];

for(inti=0;i

{

string[i]=head->ch;

head=head->next;

}

puts(string);

}

14.5用后进先出单向链表重做第14.3。

#include

#include

#include

#include

typedefstructStudentStudent;

structStudent

{

intnumber;

charname[10];

floatsubject1;

floatsubject2;

floatsubject3;

floataverage;

Student*next;

};

voidInput(Student**head);

voidOutput(Student*head);

voidChangeInfo(Student*head);

voidSort(Student*head);

intmain(void)

{

Student*head=NULL;

intn;

intflag=1;

while(flag==1)

{

printf("Pleaseinputthenumberofoptions:

\n");

printf("1:

InputInformation\t2:

ChangeInformation\n");

printf("3:

OutputInformation\t4:

SortInformation\n");

printf("5:

Quit\n");

scanf("%d",&n);

switch(n)

{

case1:

Input(&head);

break;

case2:

ChangeInfo(head);

break;

case3:

Output(head);

break;

case4:

Sort(head);

break;

case5:

flag=0;

break;

default:

printf("IllegalInput!

Pleaseinputagain.");

break;

}

printf("\n\n");

}

return0;

}

voidInput(Student**head)

{

//采用的是后进先出的单向链表

charanswer='y';

while(answer=='y')

{

Student*current=NULL;

current=(Student*)malloc(sizeof(Student));

current->next=*head;

*head=current;

printf("Pleaseinputthestudent'snumber:

");

scanf("%d",¤t->number);

printf("Pleaseinputthestudent'sname:

");

scanf("%s",current->name);

printf("Pleaseinputthescoreofsubject1:

");

scanf("%f",¤t->subject1);

printf("Pleaseinputthescoreofsubject2:

");

scanf("%f",¤t->subject2);

printf("Pleaseinputthescoreofsubject3:

");

scanf("%f",¤t->subject3);

current->average=(current->subject1+current->subject2+current->subject3)/3;

printf("Doyouwanttoinputtheinfomationofanotherstudent?

(Y/N)");

scanf("%c",&answer);

answer=tolower(answer);

}

}

voidOutput(Student*head)

{

if(head==NULL)

{

printf("Youhavenotinputanyinformation!

");

}

else

{

printf("\n\n\t\tTheInfomationOfStudents\n");

do{

printf("name:

%s\t\t",head->name);

printf("number:

%d\t\t",head->number);

printf("average:

%.2f\n",head->average);

printf("subject1:

%.2f\t",head->subject1);

printf("subject2:

%.2f\t",head->subject2);

printf("subject3:

%.2f\n\n",head->subject3);

head=head->next;

}while(head!

=NULL);

}

}

voidChangeInfo(Student*head)

{

if(head==NULL)

{

printf("Youhavenotinputanyinformation!

");

}

else

{

Student*current=head;

charname[10];

intflag=0;

intn;

printf("Pleaseinputthestudent'sname:

");

scanf("%s",name);

while(current->next!

=NULL)

{

if(strcmp(name,current->name)!

=0)

{

current=current->next;

}

else

{

flag=1;

printf("Pleaseinputthenumberofoptions:

\n");

printf("1:

subject1\t2:

subject2\t3:

subject3\t4:

name\t5:

number\n");

scanf("%d",&n);

switch(n)

{

case1:

printf("Pleaseinputthescoreofsubject1:

");

scanf("%f",¤t->subject1);

break;

case2:

printf("Pleaseinputthescoreofsubject2:

");

scanf("%f",¤t->subject2);

break;

case3:

printf("Pleaseinputthescoreofsubject3:

");

scanf("%f",¤t->subject3);

break;

case4:

printf("Pleaseinputthestudent'sname:

");

scanf("%s",current->name);

break;

case5:

printf("Pleaseinputthestudent'snumber:

");

scanf("%d",¤t->number);

default:

break;

}

}

if(n<=3)

current->average=(current->subject1+current->subject2+current->subject3)/3;

break;

}

}

}

voidSort(Student*head)

{

//采用的是冒泡排序

//交换的是数据域,不是指针

if(head==NULL)

{

printf("Youhavenotinputanyinformation!

");

}

else

{

Studenttemp;

while(head->next!

=NULL)

{

Student*current=head;

while(current->next!

=NULL)

{

if(current->averagenext->average)

{

temp=*current;

temp.next=current->next->next;

current->next->next=current->next;

*current=*current->next;

*current->next=temp;

}

current=current->next;

}

head=head->next;

}

}

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1