C语言答案参考.docx
《C语言答案参考.docx》由会员分享,可在线阅读,更多相关《C语言答案参考.docx(15页珍藏版)》请在冰豆网上搜索。
C语言答案参考
1、杨辉三角形的每一项数据正好是组合(即n!
/m!
/(n-m)!
)的值,其中n是行数(从0行开始);m是列数(从0列开始)。
请使用上述算法得到杨辉三角形每一个位置的值并按下图打印。
要求用函数f计算一个正整数的阶乘(用递归函数来实现),通过主函数调用f完成计算。
2.编写一个函数,要求对n个学生的成绩进行排序,要求用数组名作函数参数。
在数组a中存放了10个学生某门课程的成绩,调用上述函数,实现对10个学生的成绩排序。
三、程序运行结果示例:
二、实验内容:
按题目要求完成程序的改错、调试、填空和编写。
1、以下程序中,main函数通过调用fun()函数统计整数序列中的负数的个数以及平均值。
本题约定平均值由函数返回,负数的个数由参数返回。
程序有若干错误,请先阅读程序,找出其中的错误行,并写出出错的原因,最后上机调试该程序验证自己的预测。
#1doubleaver(inta[],intn,int*p)
#2{inti,sum=0;
#3*p=0;
#4for(i=0;i#5{sum=sum+a[i];
#6if(a[i]<0)*p++;
#7}
#8returnsum/n;
#9}
#10#include"stdio.h"
#11main()
#12{intcount,x[]={0,12,33,-9,-5,27,80,0,54,63};
#13doubleav;
#14av=aver(x,10,count);
#15printf("count:
%d\naverage:
%.2f\n",count,av);
#16}
2、设有如下结构定义,且structlink为链表的结点类型,由该结构类型创建的链表中的a结点已被指针p指向(见下图),请完成下面的操作:
structlink
{intscore;
strucklink*next;
}*p,*q;
(1)写出删除b结点(包括释放其存储空间)的语句序列(允许借助于q指针),但要求链表的连续结构不能破坏,不能移动p指针。
答案:
(2)阅读下面程序说明,按注释提示,在划线处补充细节,使程序达到预期功能。
[程序说明]
以下程序中,函数create的功能是创建一个结点类型为structlink的学生成绩链表,main函数中,首先调用create函数创建一个包含N个结点的成绩链表,然后调用问题
(1)的算法,将链表的第2个结点删除掉,要求输出结点删除前、后链表的内容,以验证问题
(1)算法的正确性。
#include
#defineN5
#defineLsizeof(structlink)
structlink
{intscore;/*成绩*/
(1);/*定义结点的指针域next*/
};
structlink*create(void)
{structlink*head,*p;
inti;
head=NULL;
printf("Input%drecords\n",N);
for(i=1;i<=N;i++)
{p=
(2);/*创建一个动态结点*/
scanf("%d",&p->score);
(3);/*新结点进栈*/
head=p;
}
return(4);/*返回所创建链表的头指针*/
}
voidprintlist(structlink*head)/*输出链表*/
{structlink*p;
p=head;
while(p!
=NULL)
{printf("%d,",p->score);
(5);/*使p后移一个结点*/
}
printf("\n");
}
main()
{structlink*base,*new;
(6);/*调用create函数,创建链表*/
printlist(base);/*输出原始链表*/
/*借助于new指针删除第2个结点*/
(8);/*输出结点删除以后的链表内容*/
printf("ok!
\n");
}
跪求那位电脑高手来帮我下!
!
!
我有附件,请一定帮帮忙!
谢了!
问题补充:
跪求!
!
!
!
!
搜啦,后面的还可以,但是第一个死活搜不到
2010-6-2321:
38
最佳答案
下面是我给的答案,有问题的联系我,再讨论。
从12点多一直做到现在,别忘了给我选成推荐答案哈。
累傻了我了都
1、杨辉三角形的每一项数据正好是组合(即s(n!
/m!
/(n-m)!
)的值,其中n是行数(从0行开始);m是列数(从0列开始)。
请使用上述算法得到杨辉三角形每一个位置的值并按下图打印。
要求用函数f计算一个正整数的阶乘(用递归函数来实现),通过主函数调用f完成计算。
答:
下面是我的源代码,程序输出的部分在最后的注释里面
/*
*yanghui.cc
*
*Createdon:
2010-6-14
*Author:
LiuFeng
*Email:
sohu2000000@
*/
#include
#include
usingnamespacestd;
intfac(intbase){
if(base==1){
return1;
}
returnfac(base-1)*base;
}
inttriDisplay(int**a,introw,intcol){
if(row!
=col){
perror("must:
row=col");
return(-1);
}
intsz=row;
printf("\n");
for(inti=0;i*((int*)a+i*sz+0)=1;
*((int*)a+i*sz+i)=1;
}
for(intn=2;nfor(intm=1;m*((int*)a+n*sz+m)=fac(n)/fac(m)/fac(n-m);
}
for(intn=0;nfor(intm=0;m<=n;++m){
printf("%5d",*((int*)a+n*sz+m));
}
printf("\n");
}
return0;
}
int
main(void){
intdata[10][10];
intsize=10;
if(triDisplay((int**)data,size,size)<0){
perror("badmtria");
exit(-2);
}
return0;
}
/*
[Administrator@/<7>06/14]$g++-g-O3-otriyanghui.cc
[Administrator@/<7>06/14]$./tri.exe
1
11
121
1331
14641
15101051
1615201561
172135352171
18285670562881
193684126126843691
*/
2.编写一个函数,要求对n个学生的成绩进行排序,要求用数组名作函数参数。
在数组a中存放了10个学生某门课程的成绩,调用上述函数,实现对10个学生的成绩排序。
答:
程序的源代码如下,程序的输出在最后面的注释里面
/*
*DcSort.cc
*
*Createdon:
2010-6-14
*Author:
LiuFeng
*Email:
sohu2000000@
*
*g++-g-O3DcSort.cc-Wall-odsort
*/
#include
#include
#include
typedefstruct
{
char_sname[30];
double_sscore;
}stu;
int
Partition(stu*stus,intl,inth)
{
stupivot=stus[l];
while(lwhile(l=(pivot._sscore)))h--;
if(lwhile(lif(l}
stus[l]=pivot;
returnl;
}
voidDcSort(stu*stus,intlow,inthigh){
intpivotpos;
if(lowpivotpos=Partition(stus,low,high);
DcSort(stus,low,pivotpos-1);
DcSort(stus,pivotpos+1,high);
}
}
intmain(void){
stus[10]={
{"zhao",60},
{"qian",40},
{"sun",80},
{"li",90},
{"zhou",70},
{"wu",50},
{"zheng",80},
{"jiang",90},
{"shen",100},
{"han",80},
};
for(inti=0;i<10;++i){
printf("student:
name:
%s,score:
%f\n",s[i]._sname,s[i]._sscore);
}
DcSort(s,0,10);
printf("\n===================aftersorted==================\n");
for(inti=0;i<10;++i){
printf("student:
name:
%s,score:
%f\n",s[i]._sname,s[i]._sscore);
}
return0;
}
/*
输出结果:
[Administrator@~/<1>preInterView/SortAndFind]$g++-g-O3DcSort.cc-Wall-odsort
[Administrator@~/<1>preInterView/SortAndFind]$clear
[Administrator@~/<1>preInterView/SortAndFind]$./dsort.exe
student:
name:
zhao,score:
60.000000
student:
name:
qian,score:
40.000000
student:
name:
sun,score:
80.000000
student:
name:
li,score:
90.000000
student:
name:
zhou,score:
70.000000
student:
name:
wu,score:
50.000000
student:
name:
zheng,score:
80.000000
student:
name:
jiang,score:
90.000000
student:
name:
shen,score:
100.000000
student:
name:
han,score:
80.000000
===================aftersorted==================
student:
name:
qian,score:
40.000000
student:
name:
wu,score:
50.000000
student:
name:
zhao,score:
60.000000
student:
name:
zhou,score:
70.000000
student:
name:
han,score:
80.000000
student:
name:
sun,score:
80.000000
student:
name:
zheng,score:
80.000000
student:
name:
jiang,score:
90.000000
student:
name:
li,score:
90.000000
student:
name:
shen,score:
100.000000
[Administrator@~/<1>preInterView/SortAndFind]$
*/
三、程序运行结果示例:
第一题:
/*
[Administrator@/<7>06/14]$g++-g-O3-otriyanghui.cc
[Administrator@/<7>06/14]$./tri.exe
1
11
121
1331
14641
15101051
1615201561
172135352171
18285670562881
193684126126843691
*/
第二题:
/*
输出结果:
[Administrator@~/<1>preInterView/SortAndFind]$g++-g-O3DcSort.cc-Wall-odsort
[Administrator@~/<1>preInterView/SortAndFind]$clear
[Administrator@~/<1>preInterView/SortAndFind]$./dsort.exe
student:
name:
zhao,score:
60.000000
student:
name:
qian,score:
40.000000
student:
name:
sun,score:
80.000000
student:
name:
li,score:
90.000000
student:
name:
zhou,score:
70.000000
student:
name:
wu,score:
50.000000
student:
name:
zheng,score:
80.000000
student:
name:
jiang,score:
90.000000
student:
name:
shen,score:
100.000000
student:
name:
han,score:
80.000000
===================aftersorted==================
student:
name:
qian,score:
40.000000
student:
name:
wu,score:
50.000000
student:
name:
zhao,score:
60.000000
student:
name:
zhou,score:
70.000000
student:
name:
han,score:
80.000000
student:
name:
sun,score:
80.000000
student:
name:
zheng,score:
80.000000
student:
name:
jiang,score:
90.000000
student:
name:
li,score:
90.000000
student:
name:
shen,score:
100.000000
[Administrator@~/<1>preInterView/SortAndFind]$
*/
二、实验内容:
按题目要求完成程序的改错、调试、填空和编写。
1、以下程序中,main函数通过调用fun()函数统计整数序列中的负数的个数以及平均值。
本题约定平均值由函数返回,负数的个数由参数返回。
程序有若干错误,请先阅读程序,找出其中的错误行,
并写出出错的原因,最后上机调试该程序验证自己的预测。
#1doubleaver(inta[],intn,int*p)//p所谓返回的参数之一,应该使用二级指针,或者指针的引用;
#2{inti,sum=0;//这里的*p指向了函数内部的临时变量,且该变量在函数结束时,
//会同时被系统从栈上释放掉,那么你在main函数中读取p指向的地址,得到的
//就是一个不可以预期的值了,也就是“野指针”
//使用二级指针或者指针的引用,是可以将内存在函数体内外带入带出的,但是
//注意要在main中先申请好
#3*p=0;
#4for(i=0;i#5{sum=sum+a[i];
#6if(a[i]<0)*p++;
#7}
#8returnsum/n;
#9}
#10#include"stdio.h"
#11main()
#12{intcount,x[]={0,12,33,-9,-5,27,80,0,54,63};
#13doubleav;
#14av=aver(x,10,count);
#15printf("count:
%d\naverage:
%.2f\n",count,av);
#16}
2、设有如下结构定义,且structlink为链表的结点类型,由该结构类型创建的链表中的a结点已被指针p指向(见下图),请完成下面的操作:
structlink
{intscore;
structlink*next;
}*p,*q;
(1)写出删除b结点(包括释放其存储空间)的语句序列(允许借助于q指针),但要求链表的连续结构不能破坏,不能移动p指针。
答:
void
deleteNode(link*p,link*q,intb)
{
q=p;
intlen=0;
while(q->next)++len;
if(len
q=p;
while(--b){//movetob
q=q->next;
}
while(q->next){//overwritetheprenode,onebyone
q->score=q->>next->score;
}
free(q)//qpointtothelastnode;
}
(2)阅读下面程序说明,按注释提示,在划线处补充细节,使程序达到预期功能。
[程序说明]以下程序中,函数create的功能是创建一个结点类型为structlink的学生成绩链表,main函数中,首先调用create函数创建一个包含N个结点的成绩链表,然后调用问题
(1)的算法,将链表的第2个结点删除掉,要求输出结点删除前、后链表的内容,以验证问题
(1)算法的正确性。
#include
#defineN5
#defineLsizeof(structlink)
structlink
{intscore;/*成绩*/
(1);/*定义结点的指针域next*/
};
structlink*create(void)
{
structlink*head,*p;
inti;
head=NULL;
printf("Input%drecords\n",N);
for(i=1;i<=N;i++)
{
p=
(2);/*创建一个动态结点*/
scanf("%d",&p->score);
(3);/*新结点进栈*/
head=p;
}
return(4);/*返回所创建链表的头指针*/
}
voidprintlist(structlink*head)/*输出链表*/
{structlink*p;
p=head;
while(p!
=NULL)
{printf("%d,",p->score);
(5);/*使p后移一个结点*/
}
printf("\n");
}
int
main(void)
{structlink*base,*new;
(6);/*调用create函数,创建链表*/
printlist(base);/*输出原始链表*/
/*借助于new指针删除第2个结点*/
(8);/*输出结点删除以后的链表内容*/
printf("ok!
\n");
}
(2)答:
【
(1)link*next;】
【
(2)p=(link*)malloc(sizeof(structlink));】
【(3)p->next=head】
【(4)head】
【(5)p=p->next】
【(6)base=create();】
【(7)deleteNode(base,new,intb);//利用我们上一问中,写好的函数】
【(8)printlist(base);】