二级C的30分填空题中,第3部分(最后)是完善程序题,共4小题,12分
14. 以下程序中函数float fun(int m)的功能是计算s=s1+s2+s3+s4的值,其中s1、s2、s3、s4用下列公式计算。
请完善程序以达到要求的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> float fun(int m) { int i,n; float s=0,t; for(n=1; n<=m,n++) { t=___19_____; for(i=1; i<5; i++) { t=_____20____; s+=t; } } return s; } void main() { printf("%f",fun(50)); } |
15. 以下程序将同时出现在数组a和b中的数据复制到数组c中。例如,如果数组a中的数据是{8,5,7,1,6,4,9},数组b中的数据是{2,9,3,7,4,5,10},则数组c中的结果数据为{5,7,4,9},以下程序中,in函数判断x中存储的整数是否在a指向的数组前n个元素中出现,如果出现则函数返回1,否则返回0。请完善程序以达到要求达到功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> int in(int a[],int n,int x) { int i; for(i=0; i<n; i++) if(___________) return 1; return 0; } void main() { int a[7]= {8,5,7,1,6,4,9},b[7]= {2,9,3,7,4,5,10},c[7]= {0},i,j=0; for(i=0; i<7; i++) if(_________) c[j++]=a[i]; for(i=0; ________; i++) printf("%d\t",c[i]); } |
16.以下程序根据输入的年份及该年中的第几天,计算并输出这一天的“年/月/日”。例如若输入“2010,100”则出处“2010/4/10(表示2010年得第100天是4月10日)”。试完善程序以达到要求的功能。
提示:能被4整除并且不能被100整除的年份是闰年,能被400整除的年份也是闰年,其他年份是平年。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<stdio.h> int leap(int year) { int y; y=year%4==0&&year%100!=0||year%400==0; return y; } void month_day(int year,int yearday,int *month,int *day) { int k,p; int tab[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; p=leap(year); for(k=1; yearday>_________; k++) yearday=yearday-tab[p][k]; *month=k; *day=____________; } void main() { int year,days,month,day; printf("Input year and days!\n"); scanf("%d,%d",&year,&days); if(leap(year)==1 && days<=366 || leap(year)==0 && days<=365) { month_day(year,days,__________); printf("\n%d%d%d",year,month,day); } else puts("days is wrong!"); } |
17.本题所列Bind函数的功能是实现两个多项式的合并操作,试完善Bind函数以实现要求的功能。
算法提示:Bind函数所在的程序采用单向链表存储多项式,多项式的每一项分别存储在链表的一个结点中。结点的数据结构如下:
1 2 3 4 5 | typedef struct node { int a; int n; struct node *next; } NODE; |
函数NODE *Bind(NODE *h1,NODE *h2)的形式参数h1和h2粉笔是两个按“指数”值降序排列的有序单向链表的头指针(简称这两个链表为h1链表和h2链表),h3是一个新建的按“指数”值降序排列的有序单向链表头指针(简称h3链表)。Bind函数完成h1链表与h2链表中分别存储的两个多项式的合并操作,合并后的多项式保存到h3链表中。函数返回h3链表头指针的值。
算法提示:若h1指向结点与h2指向结点的“指数”值相同,则将这两个结点的“系数”值相加并取两结点中任一个结点的指数值,结果保存到一个新申请的结点中;如果新结点的“系数”值不为零则将新结点添加到h3链表的尾部,否则放弃新结点;分别修改h1和h2使其指向所在链表下一个结点。若h1指向结点与h2指向结点的“指数”值不相同,则将其中“指数”值较大结点的“系数”和“指数”值复制到一个新申请的结点中,将新结点添加到h3链表尾部,修改被复制结点的链表头指针使其指向下一个结点。重复以上操作直到处理完h1链表和h2链表中全部结点为止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | NODE *Bind(NODE *h1,NODE *h2) { NODE *h3=NULL,*p1,*p2; while(___________) { p1=(NODE *)malloc(sizeof(NODE)); if(__________&&h1&&h2) { p1->a=h1->a+h2->a; p1->n=h1->n; if(p1->a==0) { free(p1); p1=0; } h1=h1->next; h2=h2->next; } else { if(h1->n>h2->n||h1&&h2==0) { p1->a=h1->a; p1->n=h1->n; h1=h1->next; } if(h1->n<h2->n||h2&&h1==0) { p1->a=h2->a; p1->n=h2->n; h2=h2->next; } } if(p1) if(h3==NULL) h3=p2=p1; else { ________=p1; p2=p1; } } if(_________) p2->next=NULL; return h3; } |