习题二和上机答案
习题二和上机答案

习题二⒉1描述以下四个概念的区别:头指针变量,头指针,头结点,首结点(第一个结点)。
解:头指针变量和头指针是指向链表中第一个结点(头结点或首结点)的指针;在首结点之前附设一个结点称为头结点;首结点是指链表中存储线性表中第一个数据元素的结点。
若单链表中附设头结点,则不管线性表是否为空,头指针均不为空,否则表示空表的链表的头指针为空。
2.2简述线性表的两种存储结构有哪些主要优缺点及各自使用的场合。
解:顺序存储是按索引直接存储数据元素,方便灵活,效率高,但插入、删除操作将引起元素移动,降低了效率;而链式存储的元素存储采用动态分配,利用率高,但须增设表示结点之间有序关系的指针域,存取数据元素不如顺序存储方便,但结点的插入和删除十分简单。
顺序存储适用于线性表中元素数量基本稳定,且很少进行插入和删除,但要求以最快的速度存取线性表中的元素的情况;而链式存储适用于频繁进行元素动态插入或删除操作的场合。
2.3 在头结点为h的单链表中,把值为b的结点s插入到值为a的结点之前,若不存在a,就把结点s插入到表尾。
Void insert(Lnode *h,int a,int b){Lnode *p,*q,*s;s=(Lnode*)malloc(sizeof(Lnode));s->data=b;p=h->next;while(p->data!=a&&p->next!=NULL){q=p;p=p->next;}if (p->data==a){q->next=s;s->next=p;}else{p->next=s;s->next=NULL;}}2.4 设计一个算法将一个带头结点的单链表A分解成两个带头结点的单链表A和B,使A中含有原链表中序号为奇数的元素,而B中含有原链表中序号为偶数的元素,并且保持元素原有的相对顺序。
Lnode *cf(Lnode *ha){Lnode *p,*q,*s,*hb;int t;p=ha->next;q=ha;t=0;hb=(Lnode*)malloc(sizeof(Lnode));s=hb;while(p->next!=NULL){if (t==0){q=p;p=p->next;t=1;}else{q->next=p->next;p->next=s->next; s->next=p; s=p;p=p->next; t=0;}}s->next=NULL;return (hb);}2.5设线性表中的数据元素是按值非递减有序排列的,试以不同的存储结构,编写一算法,将x插入到线性表的适当位置上,以保持线性表的有序性。
C语言上机练习题及答案

C语言上机练习题及答案1输入三个整数x,y,z,把这三个数由小到大输出。
# includevoid main(){int x,y,z,a;scanf ("%d,%d,%d",&x,&y,&z);if(x>y) {a=x; x=y;y=a;}if(x>z) { a=x; x=z; z=a;}if(y>z) {a=y; y=z; z=a;}printf("%d,%d,%d\n",x,y,z);}2输入圆的半径,输出圆的周长和面积。
# include# define PI 3.1415926# define S ==PI*r*r# define V (4.0/3)*PI*r*r*rvoid main(){double r;printf("please input r:");scanf("%lf",&r);printf("area is %.4lf\n",S);printf("volume is %.4lf\n",V);}输入正方形的边长,输出正方形的周长和面积。
# includevoid main(){float c,zc,mj;printf("输入你的正方形");scanf("%f\n",&c);zc=4*c;mj=c*c;printf("周长%f,面积%f,边长%f",zc,mj,c);}3用格式输入函数输入3个字符,并用输出函数反向输出3个字符和他们的ASCII 码。
# includeint main(){char-ch1,ch2,ch3;printf("please input three characters:\n");scanf("%C%C%c",&ch1,&ch2,&ch3);printf("%c\n%d\n",ch3,ch3);printf("%c\n%d\n",ch2,ch2);printf("%c\n%d\n",ch1,ch1);}输入一个摄氏温度,要求输出华氏温度。
C语言程序设计 上机实验指导与习题 参考答案(第四版)

C 语言程序设计上机实验指导与习题参考答案(第四版)(学生改编)实验 1:C语言程序初步一、实验目的(1)了解所用的计算机系统的基本操作方法,学会独立使用该系统。
(2)了解在该系统上如何编辑、编译、连接和运行一个 C程序。
(3)通过运行简单的 C 程序,初步了解 C程序的特点。
(4)在教师的指导下,学会使用在线评判系统。
二、实验内容1.运行第一个 C 程序题目:ThefirstCProgram将下列程序输入visualc ,编译、连接和运行该程序。
includequotstdio.hquotmain printfquotThefirstCProgramnquot具体操作步骤(1)在编辑窗口中输入程序。
(2)保存程序,取名为a1.c。
(3)按照第一章中介绍的方法,编译、连接和运行程序。
,则该题完成。
4按照第三章介绍的方法,将代码提交到在线评判系统,系统返回“通过”2.在在线评判系统中提交实现了计算 ab 功能的程序题目 1001:计算ab由键盘输入两个整数,计算并输出两个整数的和。
实现该功能的程序如下,inclu dequotstdio.hquotmain int ab scanfquotddquotampaampbprintfquotdquotab(1)在程序编辑窗口中输入程序。
(2)保存程序,取名为a2.c。
(3)按照前二章中介绍的方法,编译、连接和运行程序。
(4)在程序运行过程中,输入 15 30↙↙表示输入回车符(5)如果看到如下输出结果,则表明1530 的结果正确,如果得不到如下结果,则需检查并更正程序。
45(6)按照第三章中介绍的方法进入在线评判系统。
(7)显示题目列表,点击题号为 1001,题名为“计算ab”的题目。
(8)查看完题目要求后,点击页面下端的“sumbit” ,参照第二章提交程序的方法提交程序a2.c。
(9)查看评判结果,如果得到“accepted”则该题通过,否则返回第一步检查程序是否正确。
软件测试上机练习题1 - 答案

软件测试技术基本要求
一、考试内容
1、功能(黑盒)测试用例设计编程实现
(1)等价类划分法
(2)边界值分析法
(3)因果图法
(4)决策表法
2、结构(白盒)测试用例设计编程实现
(1)语句覆盖
(2)判定覆盖
(3)条件覆盖
(4)组合覆盖
(5)路径覆盖
(6)独立路径测试
二、考生注意事项
1、平时训练与考试
(1)思想重视
明确考试目的,端正考试态度,认真做好上机考试的准备工作。
(2)知识准备
平时认真学习,消化课程内容,熟悉编程环境和工具,认真做好课程实验。
(3)平时训练
应针对上机考试题型做好平时训练。
练习题(一)
1、假设某保险公司的人寿保险的保费计算方式为:投保额×保险费率。
其中,保险费率依
点数不同而有别,具体规则见下表。
根据问题的规格说明,从输入条件划分等价类,并设计测试用例。
输入等价类
测试用例
练习题(二)
1、假设某单位内部电话号码由三部分组成,分别是:分机号+前缀+后缀。
其中,分机号为
空白或一位数字;前缀为非‘0’开头的二位数字;后缀为非全0的3位数字。
假定被测程序能接受一切符合上述规定的电话号码,拒绝所有不符合规定的电话号码。
根据问题的规格说明,从输入条件划分等价类,并设计测试用例。
《C语言程序设计上机指导与习题》(张健)599-0答案 第二部分 习题答案

第1章C语言概述一、选择题1.C 2.D 3.C 4.C 5.B6.C 7.C 8.D 9.C 10.A二、填空题1.主函数main 2.分号3./* */ //4.源程序.c 目标程序.obj 可执行程序.exe三、编程题1.参考代码#include <stdio.h>int main(){printf("欢迎来到C语言的世界!\n");return 0;}2.参考代码#include <stdio.h>int main(){int a,b,c;a=2;b=3;c=a+b;printf("c=%d\n",c);return 0;C 语言程序设计上机指导与习题 }第2章 算 法一、选择题1.D2.A3.A4.C5.B二、填空题1.算法2.顺序结构、选择结构(分支结构)、循环结构三、编程题1.算法的流程图如图2-1所示,算法的N-S 流程图如图2-2所示。
a>ba>cb>cmax=amax=c max=b max=c真真真假假假开始输入a ,b ,c 的值输出max 的值结束图2-1 流程图第二部分 习题答案输入a ,b ,c 的值判断a>b是否输出max 的值判断b>c判断a>c 是是否否max=amax=cmax=b max=c图2-2 N-S 流程图2.算法的流程图如图2-3所示,算法的N-S 流程图如图2-4所示。
开始num1=0,num2=0,num3=0输出num1,num2和num3的值结束No Yes 输入c 的值是否是小写字母num1++是否是大写字母num2++是否是数字num3++判断c 是否为#Yes No NoYes YesNo图2-3 流程图C 语言程序设计上机指导与习题判断是否是小写字母是否是否是大写字母是num1++否是否是数字num1=0,num2=0,num3=0输入c 的值是否num2++num3++当输入的字符c 不为#时输出num1,num2和num3的值图2-4 N-S 流程图第3章 C 程序设计基础一、选择题1.C 2.C 3.C 4.D 5.A 6.B 7.D 8.B 9.A 10.A 11.A 12.A 13.B 14.D 15.A 16.A17.B18.A19.A20.D二、填空题1.字母 数字 下划线 字母 下划线2.23.04.2第二部分习题答案5.1个2个6.(a/100) % 107.'f' 8.6 9.710.6.700000 2.000000 11.10,1812.d=13,e=13.200000三、编程题1.参考代码#include<stdio.h>int main(){int num,a,b,c;printf("Please input the number:");scanf("%d",&num);a=num/100;b=(num-100*a)/10;c=num-100*a-10*b;a+=b*10+c*100;printf("The result is%d\n",a);return 0;}2.参考代码#include <stdio.h>int main(){float c,f;f=120.3;c=5.0/9*(f-32);printf("c=%f\n",c);return 0;}3.参考代码#include <stdio.h>int main( ){C语言程序设计上机指导与习题int a,b,c;printf("请输入a的值\n");scanf("%d",&a);printf("请输入b的值\n");scanf("%d",&b);c=(b%10)*1000+(a%10)*100+(b/10)*10+a/10;printf("c=%d\n",c);return 0;}第4章顺序结构程序设计一、选择题1.A 2.D 3.B 4.A 5.C 6.D 7.B 8.B二、填空题1.空格制表符回车2.printf("%.2f\n",n);3.A4.0.33%5.n1=%04d\nn2=%04d6.①putchar(a); ②putchar(b);7.①b=a+32; ②putchar(b);8.n1=1,n2=129.aabb cc abc A B10.x=3.600000,i=3三、编程题1.参考代码第二部分习题答案#include <stdio.h>int main(){int i;int s1,s2,s3,s4;int sum;double average;sum=average=0;scanf("%d,%d,%d,%d",&s1,&s2,&s3,&s4);sum=s1+s2+s3+s4;average = (double)sum/4;printf("sum=%d\n",sum);printf("average=%f\n",average);return 0;}2.参考代码#include <stdio.h>int main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1=c1+5;c2=c2+5;c3=c3+5;c4=c4+5;c5=c5+5;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);return 0;}3.参考代码#include <stdio.h>int main(){int a=560;int h,m;h=a/60;C语言程序设计上机指导与习题m=a%60;printf("560分钟=%d小时%d分钟\n",h,m);return 0;}第5章选择结构程序设计一、选择题1.C 2.B 3.C 4.C 5.D 6.B 7.C 8.C 9.C 10.D 11.B 12.C 13.C 14.A 15.B 16.A 17.D 18.B 19.B 20.D二、填空题1.算术运算符赋值运算符2.a>1&&a<43.if(a%3)4.x>z||y>z5.11,19,30,16.67.08.①getchar(); ②ch<='Z'-6 ③ch=ch+6;④'Z'-6<ch&&ch<='Z' ⑤ch=ch+5-'Z'+'A';三、编程题1.参考代码#include<stdio.h>int main(){char num='0';第二部分习题答案scanf("%c",&num);if (num>='a'&&num<='z')printf("小写字母\n");else if (num>='A'&&num<='Z')printf("大写字母\n");elseprintf("不是字母\n");return 0;}2.参考代码#include<stdio.h>int main(){int grade;printf("input grade(0-100):");scanf("%d",&grade);if(grade>100)printf("Input error!\n");else if(grade>=90)printf("Very Good!\n");else if(grade>=80)printf("Good!\n");else if(grade>=70)printf("Middle!\n");else if(grade>=60)printf("Pass!\n");elseprintf("No pass!\n");return 0;}3.参考代码#include <stdio.h>int main(){int month;C语言程序设计上机指导与习题printf("input month(1-12):");scanf("%d",&month);if(month>=3&&month<=5)printf("spring");if(month>=6&&month<=8)printf("summer");if(month>=9&&month<=11)printf("autumn");if(month==12||month==1||month==2)printf("winter");if(month>12||month<1)printf("error month!");return 0;}4.参考代码#include <stdio.h>int main(){char c;printf("请输入一个大写字母:\n");scanf("%c",&c);if('B'<=c&&c<='Y')printf("前一个字母为%c,后一个字母为%c\n",c-1,c+1);else if(c=='A')printf("没有前面的字母,后一个字母为%c\n",c+1);elseprintf("前一个字母为%c,没有后面的字母",c-1);return 0;}5.参考代码#include <stdio.h>int main(){int a,b,c,t,x;printf("input 3 integers:");scanf("%d,%d,%d",&a,&b,&c);if(a>b){t=a;a=b;b=t;}if(b<c)x=b;else if(a<c)x=c;elsex=a;printf("the middle number is:%d",x);return 0;}第6章循环结构程序设计一、选择题1.B 2.B 3.B 4.A 5.B6.C 7.A 8.C 9.C 10.B11.B12.A13.C 14.B 15.A16.A 17.C 18.A 19.C 20.B二、填空题1.while do-while for 不管循环条件是否成立,do-while循环都先执行一次循环体,所以do-while循环的循环体至少执行一次,而while循环的循环体可能一次都不执行。
浙江大学C语言上机练习参考答案汇总

全国计算机等级考试二级教程--C语言程序设计课后习题答案

《全国计算机等级考试二级教程--C语言程序设计》课后习题答案第一章1.1 EXE1.2 C OBJ EXE1.3 顺序选择循环第二章一. 选择题2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A二. 填空题2.14 11 122.15 4.2 4.22.16 { } 定义执行语句2.17 关键字用户标识符2.18 int float double2.19 float a1=1; float a2=1;2.20 存储单元2.213.52.22 (a*b)/c a*b/c a/c*b2.23 把常量10赋给变量s2.24 位1或0三. 上机改错题2.28#include "stdio.h"; 删除行尾的";"main(); / * main function * / 删除")"后的";",注释中的*要紧靠“/”,即应为“/*”和“*/”函数开始处遗失了一个“{”float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注释符号不可嵌套使用r = 5.0 ;s = 3.14159 * r * r ;printf("%f\n",s) 行尾遗失了“;”函数结束处遗失了一个“}”2.29#include "stdio.h"main /* main function */ main后遗失了“()”{float a,b,c,v; /*a,b,c are sides, v is volume of cube */a=2.0; b=3.0; c=4.0 行尾遗失了“;”v=a*b*c;printf("%f\n", v) 行尾遗失了“;”}第三章一.选择题3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D 3.9 A 3.10 B3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 无答案3.19 C 3.20 B二. 填空题3.21 (1)-2002500(2)i=-200,j=2500(3)i=-200j=25003.22 12 0 03.23 一条语句;3.24 ;3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.89234 3.26 x=127,x= 127,x= 177,x= 7f,x= 1273.27 x=127,x=127 ,x=$127 ,x=$000127,x=%06d3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500三. 编程题和改错题3.29 修改后的程序如下:main(){double a,b,c,s,v;printf("input a,b,c:");scanf("%lf%lf%lf",&a,&b,&c);s =a*b;v=a*b*c;printf("a=%f,b=%f,c=%f\n", a,b,c);printf("s=%f,v=%f\n",s,v);}3.30#includemain(){int a=560,b=60;printf("560 minute is %d hour and %d minute.\n",a/b,a%b);}3.31#includemain(){int a,b;a=1500;b=350;printf("a div b is : %d\n",a/b);printf("a mod b is : %d\n",a%b);}3.32#includemain(){double a,b,c,ave;printf ("input 3 double number : \n");scanf ("%lf%lf%lf",&a,&b,&c);printf ("%.1f\n",(a+b+c)/3);}3.33#includevoid main(){int a,b,c,t;printf("请依次输入整数a,b,c:");scanf("%d%d%d",&a,&b,&c);printf("\n你输入的值是: a=%d,b=%d,c=%d\n",a,b,c);t=b;b=a;a=c;c=t;printf("交换之后的值是:a=%d,b=%d,c=%d\n",a,b,c);}第四章一. 选择题4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C二. 填空题4.11 非0 04.12 < > >= <=同级== !=同级4.13 ! && ||4.15 !4.16 a == b || a < c x > 4 || x < -44.17 14.18 x <= 0 1 > 04.19 3 2 24.20 *#三. 编程题4.21 略4.22#include/* 检查日期的合法性*/int checkdate(int year, int month, int day){if(year < 1900 || year > 2005){printf("输入的年份无效!\n");return 0;}else if(month < 0 && month > 12){printf("输入的月份无效!\n");return 0;}else if(day <= 0 && day > 31){printf("输入的日期无效!\n");return 0;}else{switch(month){case 4:case 6:case 9:case 11:if(day > 30){printf("输入的日期无效!\n");return 0;}break;case 2:if((year%4 == 0 && year%100 != 0) || year%400 == 0){if(day > 29){printf("输入的日期无效!\n");return 0;}}else{if(day > 28){printf("输入的出生日期无效!\n");return 0;}}break;}/* end of switch(m0)*/}return 1;}void main(){int y0, m0, d0; /* 生日*/int y1, m1, d1; /* 当前日期*/int years, months, days; /* 实足年龄*/printf("请输入学生的生日:"); scanf("%d%d%d", &y0,&m0,&d0);if(checkdate(y0, m0, d0)){printf("请输入当前日期:");scanf("%d%d%d", &y1,&m1,&d1);/*当前日期合法性检查*/if(!checkdate(y1, m1, d1)){return;}else if(y0 > y1){printf("出生年份比当前年份晚!\n"); return;}else if(y0 == y1){if(m0 > m1){printf("出生年月比当前年月晚!\n"); return;}else if(m0 == m1){if(d0 > d1){printf("出生年月日比当前年月日晚!\n"); return;}}}}/* 计算实足年龄*/years = y1 - y0;months = m1 - m0;days = d1 - d0;/* 修正实足年龄天数*/if(days < 0){months--;switch(m1){case 1:case 5:case 7:case 10:case 12:days += 30;break;case 2:case 4:case 6:case 8:case 9:case 11:days += 31;break;case 3:if((y1%4 == 0 && y1%100 != 0) || y1%400 == 0){days += 29;}else{days += 28;}break;}/* end of switch(m1) */}/* end of if(days < 0) *//* 修正实足年龄月数*/if(months < 0){months += 12;years--;}/* end of if(months < 0) */printf("出生日期: %d年%d月%d日\n", y0, m0, d0);printf("当前日期: %d年%d月%d日\n", y1, m1, d1); printf("实足年龄: %d年%d月%d日\n", years, months, days);return;}4.23#includevoid main()int a;printf ("请输入一个整数:");scanf ("%d",&a);if (a%2==0){printf ("%d 是偶数\n", a);}else{printf ("%d 是奇数\n", a);}}4.24#includevoid main(){int a,b,c,temp,max;printf ("请输入三个整数:");scanf ("%d %d %d",&a,&b,&c);temp=(a>b)? a:b;max=(temp>c)? temp:c;printf ("\n");printf ("你输入的数中最大的是%d.\n",max); }4.25(1)不嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if ( x>-5 && x<0 ){printf("y is %d\n",y=x);}if ( x==0 ){printf("y is %d\n",y=x-1);if ( x>0 && x<10 ){printf("y is %d\n",y=x+1); }if ( x>=10 || x<=-5){printf("error\n");}}(2)嵌套的if语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);printf("\n");if(x < 0){if(x > -5){printf("y is %d.\n",y=x); }else{printf("error!\n");}}if(0 == x){printf("y is %d.\n",y=x-1); }if(x > 0){if(x < 10){printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(3)if_else语句#includevoid main(){int x,y;printf("input x :");scanf("%d",&x);if( x>-5 && x<0 ){printf("y is %d.\n",y=x); }else if( x==0 ){printf("y is %d.\n",y=x-1); }else if( x>0 && x<10 ) {printf("y is %d.\n",y=x+1); }else{printf("error!\n");}}(4)switch语句#includevoid main(){int x,y;printf("input x : ");scanf("%d",&x);switch (x){case -4:case -3:case -2:case -1:printf("y is %d.\n",y=x); break;case 0:printf("y is %d.\n",y=x-1);break;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:printf("y is %d.\n",y=x+1);break;default:printf("error!\n");}}第五章一. 选择题5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D二. 填空题5.11 5 4 65.12 死循环5.13 -15.14 115.15 d=1.0 k++ k<=n5.16 x>=0 x三. 编程题5.17#includevoid main(){int i;int sig = 1;int sum = 0;for(i=1; i<=101; i++,i++){sum += sig*i;sig *= -1;}printf("sum=%d\n", sum);}5.18(1)#includevoid main(){int i;double m=1.0;double e = 1.0;for(i=1; i<50; i++){m *= i;e += 1/m;}printf("e=%f\n",e);}(2)#includevoid main(){int i=1;double m=1.0;double e = 1.0;while(1/m >= 0.0004){m *= i;e += 1/m;i++;}printf("e=%f\n",e);}5.19#includevoid main(){int year;int col = 0;for(year=1600; year<=2000; year++){if((year%4 == 0 && year%100 != 0) || year%400 == 0)printf("%d\t", year);col++;if(col%5 == 0){printf("\n");}}}printf("\n");}5.20#include#define N 7void main(){int i;int j;int m;int k = N/2;for(i=0; i {m = i-k;if(m < 0){m *= -1;}for(j=0; j {printf(" ");}for(j=0; j<2*(k-m)+1; j++){printf("*");}printf("\n");}}第六章一. 选择题6.1 B 6.2 D 6.3 A 6.4 A 6.5 B 6.6 D 6.7 D 6.8 B 6.9 A 6.10 A 6.11 C二. 填空题6.12 -16.13 16.14 ctype.h6.15 16.16 10A 20B 30C 40D6.177.29 101.298AB6.18 A7.29B101.2986.19 A B C (每个字符后有三个空格)三. 编程题6.20#include#define N 80void main(){char str[N];int iLoop = 0;gets(str);while(str[iLoop]){printf("%c-%d\t", str[iLoop],str[iLoop]); iLoop++;if(iLoop%3 == 0){printf("\n");}}printf("\n");}6.21#include#define N 80void main(){char str[N];int num = 0;int iLoop = 0;gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9') {num = 10*num + (str[iLoop] - '0');iLoop++;}printf("%d\n",num);}6.22#include#include#define N 80void main(){char str[N];int num = -1;do{gets(str);num++;}while(strcmp(str, "EOF"));printf("您输入了%d行字符!\n",num);}6.23#include#define N 80void main(){char str[N];int iLoop = 0;int num = 0;gets(str);while(str[iLoop] && iLoop < N){if(str[iLoop] >= 'a' && str[iLoop] <= 'z'){num++;}iLoop++;}printf("您输入了字符中有%d个小写字母!\n",num); }#includevoid main(){int line;int iLoop1;int iLoop2;printf("请输入图案的行数(不大于26):");scanf("%d", &line);for(iLoop1 = 0; iLoop1 < line; iLoop1++){for(iLoop2 = 0; iLoop2 < line - iLoop1; iLoop2++) {printf(" ");}for(iLoop2 = 0; iLoop2 < 2*iLoop1+1; iLoop2++) {printf("%c",iLoop1 + 'A');}printf("\n");}}第七章一. 选择题7.1 C 7.2 C 7.3 B 7.4 C 7.5 A 7.6 D 7.7 A二. 填空题7.8 127.9 9.0000007.10 47.11 n=1 s7.12 <=y z*x7.13 1 s*i 0 f(k)三. 程序调试和编程题7.14fun(int n){ int k,yes;for(k=2; k<=n/2; k++){if(n%k == 0) { yes = 0; break;}}return yes;}7.15int mymod(int a, int b){return a%b;}7.16double fun(int n){double sum = 0;int iLoop;int sig = -1;for(iLoop=1; iLoop<=n; iLoop++) {sig *= -1;sum += sig*1.0/iLoop;}return sum;}7.17double fun(int n){double t = 1.0;int iLoop;long tmp;for(iLoop=2; iLoop<=n; iLoop++) {tmp = iLoop*iLoop;t -= 1.0/tmp;}return t;}7.18#include#includedouble fun(double x){return x*x + 5*x + 4;}{int x = 2;printf("y1=%f\n", fun(x));printf("y2=%f\n", fun(x+15));printf("y3=%f\n", fun(sin(x)));}第八章一. 选择题8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C二. 填空题8.13 1108.14 7 18.15 (1)char *p=&ch; (2) p=&ch; (3)scanf("%c",p); (4)*p='A'; (5)printf("%c",*p);8.16 (1)s=p+3; (2)s=s-2 (3)50 (4)*(s+1) (5)2 (6)10 20 30 40 50三. 编程题8.17void fun(double x, double y, double *sum, double *div){*sum = x + y;*div = x - y;return;}8.18void fun(double x, double y, double z, double *max, double *min){*max = x;*min = x;if(*max < y){*max = y;}if(*max < z){*max = z;}if(*min > y){*min = y;}if(*min > z){}return;}第九章一. 选择题9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C 9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C二. 填空题9.19 9 09.20 69.21 129.22 39.23 27219.24 -850,2,09.25 k=p k9.26 (c=getchar()) c-'A'三. 编程题9.27#include#define N 81int main(){int counter[10] = {0};int iLoop = 0;char str[N];gets(str);while(str[iLoop]){if(str[iLoop] >= '0' && str[iLoop] <= '9'){counter[str[iLoop] - '0']++;}iLoop++;}for(iLoop=0; iLoop < 10; iLoop++){printf("%d - %d\n", iLoop, counter[iLoop]);}return 0;}void fun(int array[], int arraysize, int start){int iLoop;if(start < arraysize-1){if(start <=0){start = 1;}for(iLoop = start; iLoop < arraysize; iLoop++){array[iLoop-1] = array[iLoop];}}for(iLoop = 0; iLoop < arraysize; iLoop++){printf("No.%d = %d\n", iLoop, array[iLoop]);}}9.29int fun(int arry1[], int arry2[], int arrysize){int iLoop;int counter = 0;for(iLoop = 0; iLoop < arrysize; iLoop++){if(arry1[iLoop] % 2){arry2[counter++] = arry1[iLoop];}}return counter;}9.30void fun(char array[], int arraysize){int iLoop1;int iLoop2;char temp;/* 冒泡排序*/for(iLoop1 = 0; iLoop1 < arraysize - 1; iLoop1++){for(iLoop2 = 0; iLoop2 < arraysize - 1 - iLoop1; iLoop2++)if(array[iLoop2] < array[iLoop2 + 1]){temp = array[iLoop2];array[iLoop2] = array[iLoop2 + 1];array[iLoop2 + 1] = temp;}}}}9.31#includevoid fun(int array[], int arraysize, int inertNumber) {int iLoop;int iLoop2;if(array[0] < array[arraysize-1]){for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] > inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2];}array[iLoop] = inertNumber;break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}else{for(iLoop = 0; iLoop< arraysize; iLoop++){if(array[iLoop] < inertNumber){for(iLoop2 = arraysize - 1; iLoop2 >= iLoop; iLoop2--) {array[iLoop2 + 1] = array[iLoop2]; }array[iLoop] = inertNumber; break;}}if(iLoop >= arraysize){array[arraysize] = inertNumber;}}}int main(){int iLoop;int a[20] = {7,6,5,3,2,1};for(iLoop = 0; iLoop < 6; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");fun(a, 6, 0);for(iLoop = 0; iLoop < 7; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");fun(a, 7, 4);for(iLoop = 0; iLoop < 8; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");fun(a, 8, 8);for(iLoop = 0; iLoop < 9; iLoop++) {printf("%d ", a[iLoop]);}printf("\n");return 0;}9.32int fun(int number, int array[]){int iLoop = 0;int iLoop2;int binLen;int midNumber;int div;int remain;midNumber = number;do{div = midNumber/2;remain = midNumber%2;midNumber = div;array[iLoop++] = remain;}while(midNumber);binLen = iLoop;for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 < iLoop; iLoop2++, iLoop--) {midNumber = array[iLoop2];array[iLoop2] = array[iLoop];array[iLoop] = midNumber;}return binLen;}9.33#include#include#define N 15void fun(int array[], int arraysize){int x;int iLoop;int iLoop2;for(iLoop = 0; iLoop < arraysize; iLoop++){iLoop2 = 0;x = rand()%20;do{if(x == array[iLoop2] && iLoop > 0){x = rand()%20;iLoop2 = 0;}iLoop2++;}while(iLoop2 < iLoop);array[iLoop] = x;}}int main(){int a[N];int iLoop;fun(a, N);for(iLoop = 0; iLoop < N; iLoop++){printf("%d\n", a[iLoop]);}return 0;}第十章一. 选择题10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C二. 填空题10.11 GFEDCB10.12 XYZ10.13 SO10.14 1010.15 Itis10.16 strlen(str)-1 j--10.17 310.18 goodgood!三. 编程题10.19char* mygets(char *str){int iLoop = 0;char ch;while((ch=getchar()) != '\n')str[iLoop++] = ch;}str[iLoop] = '\0';return str;}char * myputs(char *str){int iLoop = 0;while(str[iLoop]){putchar(str[iLoop++]);}putchar('\n');return str;}10.20#include#includeint fun(char *str){int len;int iLoop1;int iLoop2;int result = 1;len = strlen(str);for(iLoop1 = 0, iLoop2 = len - 1; iLoop1 < iLoop2; iLoop1++, iLoop2--) {if(str[iLoop1] != str[iLoop2]){result = 0;break;}}return result;}int main(){char a[20] = "ABCDCBA";char b[20] = "ABCDEBA";printf("%d\n", fun(a));printf("%d\n", fun(b));return 0;}10.21char fun(char *str, int pos){int len;int iLoop;char ch;len = strlen(str);if(pos > len){return NULL;}ch = str[pos];for(iLoop = pos; iLoop < len - 1; iLoop++) {str[iLoop] = str[iLoop + 1];}str[len-1] = '\0';return ch;}第十一章一. 选择题11.1 D 11.2 B 11.3 A 11.4 C二. 填空题11.5 IJKLEFGHABCD11.6 711.7 811.8 *(s+j) i+1 i11.9 1711.10 (*fun)() (*fun)(a+i*h)/h mypoly三. 编程题11.11#include#include#define N 81int main(int argc, char **argv)char sig;int dig;int pos;char str[N] = {'\0'};char outStr[N] = {'\0'};if(argc < 2){sig = '-';dig = 10;}else{sig = argv[1][0];dig = argv[1][1] - '0';}printf("请输入一个字符串:"); gets(str);if(sig == '-'){pos = strlen(str) - dig;if(pos <= 0){pos = 0;}strcpy(outStr, str + pos);}else if(sig == '+'){strcpy(outStr, str);pos = strlen(outStr);if(pos > dig){pos = dig;}outStr[pos] = '\0';}printf("处理后的字串为:"); printf("%s\n", outStr);}11.12#include#includevoid movebin(char *bin){int len;int iLoop;len = strlen(bin);for(iLoop = len; iLoop > 0; iLoop--) {bin[iLoop] = bin[iLoop - 1];}return;}void fun(int n, char *bin){int pos;pos = strlen(bin);if(n == 0){return;}if(n == 1){movebin(bin);bin[0] = '1';return;}movebin(bin);bin[0] = (n%2) + '0';n /= 2;fun(n, bin);return;}int main(){int a = 4;char bin[50] = {""};printf("%s\n", bin);return 0;}11.13#includelong fun(int n){if(n == 1){return n;}else{return fun(n-1) + n;}}int main(){int num;int sum;printf("请输入一个自然数:"); scanf("%d", &num);sum = fun(num);printf("结果是:%d\n", sum);return 0;}11.14#includeint fun(int n){if(n == 0 || n == 1){return 1;}else{return fun(n-1) + fun(n-2);}}int main(){int num;int result;printf("请输入一个自然数:");scanf("%d", &num);result = fun(num);printf("斐波拉契级数为:%d\n", result);return 0;}第十二章一. 选择题12.1 B 12.2 B 12.3 A 12.4 C 12.5 D 12.6 B 12.7 A 12.8 A二. 填空题12.9 2,5,1,2,3,-212.10 2468第十三章一. 选择题13.1 A 13.2 C 13.3 B 13.4 C 13.5 D 13.6 D 13.7 D二. 填空题13.8 ar=9 ar=9 ar=1113.9 int* s *b三. 编程题13.10#define MYALPHA(C) ((C>='A' && C<='Z') || (C>='a' && C<='z')) ? 1 : 0 13.11#define SWAP(t,x,y) {t tmp; tmp=x; x=y; y=tmp;}13.12#include#includeint main(){int *p;int tmp;int iLoop;int iLoop2;p = (int *)malloc(sizeof(int)*3);scanf("%d%d%d", p,p+1,p+2);for(iLoop = 0; iLoop < 2; iLoop++){for(iLoop2 = 0; iLoop2 < 2 - iLoop; iLoop2++){if(*(p + iLoop2) > *(p + iLoop2 + 1)){tmp = *(p + iLoop2);*(p + iLoop2) = *(p + iLoop2 + 1);*(p + iLoop2 + 1) = tmp;}}}printf("%d %d %d\n", *p, *(p+1), *(p+2));free(p);p = NULL;return 0;}第十四章一. 选择题14.1 D 14.2 D 14.3 D 14.4 A 14.5 C 14.6 C 14.7 C 14.8 B二. 填空题14.9 struct link *next14.10 p->next m>p->data14.11 (struct list*) struct list struct list* struct list return h三. 编程题14.12#include#define N 3struct stud{char num[5], name[10];int s[4];double ave;};void readrec(struct stud array[], int size){int iLoop;for(iLoop=0; iLoop {scanf("%s%s%d%d%d%d", array[iLoop].num, array[iLoop].name, &array[iLoop].s[0], &array[iLoop].s[1],&array[iLoop].s[2], &array[iLoop].s[3]);array[iLoop].ave = (array[iLoop].s[0] + array[iLoop].s[1] +array[iLoop].s[2] + array[iLoop].s[3])/4.0;}return;}void writerec(struct stud array[], int size){int iLoop;for(iLoop=0; iLoop{printf("%s\t%s\t%d\t%d\t%d\t%d\t%f\n",array[iLoop].num,array[iLoop].name,array[iLoop].s[0],array[iLoop].s[1],array[iLoop].s[2],array[iLoop].s[3],array[iLoop].ave);}return;}int main(){struct stud stu[N];readrec(stu, N);writerec(stu, N);return 0;}14.13#include#include#define N 100struct node{int data;struct node* next;};int seekMaxValue(struct node *pNode){int max;struct node* pMove;pMove = pNode;max = pMove->data;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;}pMove = pMove->next;}return max;}struct node* seekMaxValueAddress(struct node *pNode) {int max;struct node* maxAddress;struct node* pMove;pMove = pNode;max = pMove->data;maxAddress = pMove;pMove = pMove->next;while(pMove){if(max < pMove->data){max = pMove->data;maxAddress = pMove;}pMove = pMove->next;}return maxAddress;}int main(){struct node* head;struct node* pNode;int iLoop;head = (struct node*)malloc(sizeof(struct node)); pNode = head;pNode->next = NULL;for(iLoop=0; iLoop {pNode->next = (struct node*)malloc(sizeof(struct node)); pNode = pNode->next;pNode->next = NULL;pNode->data = iLoop;}printf("%d\n", seekMaxValue(head->next));printf("%d\n", seekMaxValueAddress(head->next));return 0;}第十五章一.选择题15.1 D 15.2 A 15.3 B 15.4 A二. 填空题15.5 1111000015.6 a^a15.7 a|0xffff15.8 x|0xff0015.9 a=(012500>>2)15.10 ch|0x20第十六章一. 选择题16.1 B 16.2 C二. 填空题16.3 3 !feof(f1) f2 fclose(f1) fclose(f2)16.4 fopen(fname,"w") ch16.5 "r" !feof(fp) fgetc(fp)16.6 CCCCBBBBAAAA三. 编程题#include#define N 10#define LEN 81int main(){char *str[N] = {"AAAAAAAAA", "BBBBBBBBB", "CCCCCCCCC", "DDDDDDDDD", "EEEEEEEEE", "FFFFFFFFF", "GGGGGGGGG", "HHHHHHHHH","IIIIIIIII","JJJJJJJJJ"};char str2[N][LEN];FILE *fp;int iLoop;fp = fopen("str.txt", "w");if(fp == NULL){printf("创建文件失败!\n"); return 1;}else{for(iLoop = 0; iLoop < N; iLoop++) {fputs(str[iLoop], fp);fputs("\n",fp);}}fclose(fp);fp = fopen("str.txt", "r");if(fp == NULL){printf("打开文件失败!\n"); return 1;}elsefor(iLoop = 0; iLoop < N; iLoop++){fgets(str2[iLoop], LEN - 1, fp);}}fclose(fp);for(iLoop = 0; iLoop < N; iLoop++){printf("%s", str2[iLoop]);}return 0;}16.8#include#define N 10int main(){float num;int iLoop;FILE *fp;fp = fopen("num.bin", "wb+");if(fp == NULL){printf("创建文件失败!\n");return 1;}/* 从键盘读入10个数并写文件*/printf("请输入%d个数:", N); for(iLoop = 0; iLoop < N; iLoop++){scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);}/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++){fread(&num, sizeof(num), 1, fp);printf("%f\n", num);/* 移文件指针到第四个数开始处*/fseek(fp, 3L*sizeof(num), SEEK_SET);/* 读入一个新数据*/printf("请输入一个新数据:"); scanf("%f", &num);fwrite(&num, sizeof(num), 1, fp);/* 文件指针回到开始处*/rewind(fp);/* 从文件读出10个数并显示*/for(iLoop = 0; iLoop < N; iLoop++){fread(&num, sizeof(num), 1, fp);printf("%f\n", num);} /* 关闭文件*/fclose(fp);return 0;}。
人大(王燕)时间序列课后习题答案)2-5(含上机的)

第二章P34 1、(1)因为序列具有明显的趋势,所以序列非平稳。
(2)样本自相关系数:∑∑=-=+---≅=nt tkn t k t tk x xx x x xk 121)())(()0()(ˆγγρ5.10)2021(20111=+++==∑=Λn t t x n x =-=∑=2201)(201)0(x x t t γ35 =--=+=∑))((191)1(1191x x x x t t t γ29.75=--=+=∑))((181)2(2181x x x x t t t γ25.9167=--=+=∑))((171)3(3171x x x x t t t γ21.75γ(4)=17.25 γ(5)=12.4167 γ(6)=7.251ρ=0.85(0.85) 2ρ=0.7405(0.702) 3ρ=0.6214(0.556) 4ρ=0.4929(0.415) 5ρ=0.3548(0.280) 6ρ=0.2071(0.153) 注:括号内的结果为近似公式所计算。
(3)样本自相关图:Autocorrelation Partial Correlation AC PACProb . |*******| . |*******| 1 0.850 0.850 16.732 0.000 . |***** | . *| . | 2 0.702 -0.07628.7610.000 . |**** | . *| . | 3 0.556 -0.07636.7620.000 . |*** | . *| . | 4 0.415 -0.07741.5000.000 . |**. | . *| . | 5 0.280 -0.07743.8000.000 . |* . | . *| . | 6 0.153 -0.07844.5330.000 . | . | . *| . | 7 0.034 -0.07744.5720.000 . *| . |. *| . |8 -0.074 -0.07744.7710.000. *| . | . *| . |9 -0.17-0.07545.9210.000 .**| . | . *| . | 10 -0.252-0.07248.7130.000 .**| . | . *| . | 11 -0.319-0.06753.6930.000 ***| . |. *| . | 12 -0.37-0.0661.2200.000该图的自相关系数衰减为0的速度缓慢,可认为非平稳。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
习题二⒉1描述以下四个概念的区别:头指针变量,头指针,头结点,首结点(第一个结点)。
解:头指针变量和头指针是指向链表中第一个结点(头结点或首结点)的指针;在首结点之前附设一个结点称为头结点;首结点是指链表中存储线性表中第一个数据元素的结点。
若单链表中附设头结点,则不管线性表是否为空,头指针均不为空,否则表示空表的链表的头指针为空。
2.2简述线性表的两种存储结构有哪些主要优缺点及各自使用的场合。
解:顺序存储是按索引直接存储数据元素,方便灵活,效率高,但插入、删除操作将引起元素移动,降低了效率;而链式存储的元素存储采用动态分配,利用率高,但须增设表示结点之间有序关系的指针域,存取数据元素不如顺序存储方便,但结点的插入和删除十分简单。
顺序存储适用于线性表中元素数量基本稳定,且很少进行插入和删除,但要求以最快的速度存取线性表中的元素的情况;而链式存储适用于频繁进行元素动态插入或删除操作的场合。
2.3 在头结点为h的单链表中,把值为b的结点s插入到值为a的结点之前,若不存在a,就把结点s插入到表尾。
Void insert(Lnode *h,int a,int b){Lnode *p,*q,*s;s=(Lnode*)malloc(sizeof(Lnode));s->data=b;p=h->next;while(p->data!=a&&p->next!=NULL){q=p;p=p->next;}if (p->data==a){q->next=s;s->next=p;}else{p->next=s;s->next=NULL;}}2.4 设计一个算法将一个带头结点的单链表A分解成两个带头结点的单链表A和B,使A中含有原链表中序号为奇数的元素,而B中含有原链表中序号为偶数的元素,并且保持元素原有的相对顺序。
Lnode *cf(Lnode *ha){Lnode *p,*q,*s,*hb;int t;p=ha->next;q=ha;t=0;hb=(Lnode*)malloc(sizeof(Lnode));s=hb;while(p->next!=NULL){if (t==0){q=p;p=p->next;t=1;}else{q->next=p->next;p->next=s->next; s->next=p; s=p;p=p->next; t=0;}}s->next=NULL;return (hb);}2.5设线性表中的数据元素是按值非递减有序排列的,试以不同的存储结构,编写一算法,将x插入到线性表的适当位置上,以保持线性表的有序性。
⑴顺序表;解:本题的算法思想是:先找到适当的位置,然后后移元素空出一个位置,再将 x 插入,并返回向量的新长度。
实现本题功能的函数如下:int insert(vector A,int n,ElemType x) /*向量 A 的长度为 n*/{ int i,j;if (x>=A[n-1]) A[n]=x /*若 x 大于最后的元素,则将其插入到最后*/else{ i=0;while (x>=A[i]) i++; /*查找插入位置 i*/for (j=n-1;j>=i;j--) A[j+1]=A[j]; /*移出插入 x 的位置*/A[i]=x;n++; /*将 x 插入,向量长度增 1*/}return n;}⑵单链表。
解:本题算法的思想是先建立一个待插入的结点,然后依次与链表中的各结点的数据域比较大小,找到插入该结点的位置,最后插入该结点。
实现本题功能的函数如下:node *insertorder(head,x)node *head; ElemType x;{node *s,*p,*q;s=(node *)malloc(sizeof(node)); /*建立一个待插入的结点*/s->data=x;s->next=NULL;if (head==NULL || x<head->data) /*若单链表为空或 x 小于第一个结点的date 域*/{s->next=head; /*则把 s 结点插入到表头后面*/head=s;}else{ q=head; /*为 s 结点寻找插入位置,p 指向待比较的结点,q 指向 p 的前驱结点*/p=q->next;while (p!=NULL && x>p->data) /*若 x 小于 p 所指结点的 data 域值*/if (x>p->data) /*则退出 while 循环*/ {q=p;p=p->next;}s->next=p; /*将 s 结点插入到 q 和 p 之间*/q->next=s;}return(head);}2.6假设有A和B分别表示两个递增有序排列的线性表集合(即同一表中元素值各不相同),求A和B的交集C,表C中也依值递增有序排列。
试以不同的存储结构编写求得C的算法。
⑴顺序表;void SqList_Intersect_True(SqList &A,SqList B)//求元素递增排列的线性表A和B的元素的交集并存回A中{i=1;j=1;k=0;while(A.elem[i]&&B.elem[j]){if(A.elem[i]<B.elem[j]) i++;else if(A.elem[i]>B.elem[j]) j++;else if(A.elem[i]!=A.elem[k]){A.elem[++k]=A.elem[i]; //当发现了一个在A,B中都存在的元素i++;j++; //且C中没有,就添加到C中}}//whilewhile(A.elem[k]) A.elem[k++]=0;}//SqList_Intersect_True⑵单链表。
单链表chnode *or(chnode *head1,chnode *head2){ chnode *p1,*p2,*q2,*h,*p;h=p=malloc(sizeof(chnode));p->next=NULL;p1=head1->next;while(p1){ p2=head2;q2=p2->next;while((q2->data!=p1->data)&&q2){ p2=q2;q2=q2->next;}if(p1->data==q2->data)p2->next=q2->next;if(q2){ while(p->next)p=p->next;p->next=q2;p=q2;q2->next=NULL;}p1=p1->next;}return(h);}2.7设计一个算法求两个递增有序排列的线性表A和B 的差集。
(每个单链表中不存在重复的元素)提示:即在A中而不在B中的结点的集合。
typedef int elemtype;typedef struct linknode{elemtype data;struct linknode *next;} nodetype;nodetype *subs(nodetype *heada, nodetype *headb){nodetype *p, *q, *r, *s;s=(nodetype *)malloc(sizeof(nodetype));s->next=heada;heada=s;p=heada->next;r=heada;r->next=NULL;while (p!=NULL){q=headb;while (q!=NULL && q->data!=p->data) q=q->next;if (q!=NULL){s=p->next;free(p);p=s;}else{r->next=p;s=p->next;r=p;r->next=NULL;p=s;}}s=heada;heada=heada->next;free(s);return heada;}2.8设有线性表A=(a1 ,a2 ,...,a m ),B=(b1 ,b2 ,...,b n )。
试写一合并A、B为线性表C的算法,使得(a1 ,b1 ,...,a m ,b m ,b m+1 ,...,b n ) 当m≤n时C={(a1 ,b1 ,...,a n ,b n ,a n+1 ,...,a m ) 当m>n时A、B和C均以单链表作存储结构,且C表利用A和B中结点空间。
解:假设 A,B 和 C 链表分别具有头结点的指针 a,b 和 c。
实现本题功能的函数如下:node *link(a,b)node *a,*b;{node *r,*s,*p,*q,*c;c=(node *)malloc(sizeof(node)); /*建立一个头结点*/r=c;p=a;q=b;while (p!=NULL || q!=NULL){if (p!=NULL) /*如果 A 链表还存在可取的结点,则复制一个同样的结点链接到 C 中*/{s=(node *)malloc(sizeof(node));s->data=p->data;r->next=s;r=s;p=p->next;}if (q!=NULL) /*如果 B 链表还存在可取的结点,则复制一个同样的结点链接到 C 中*/{s=(node *)malloc(sizeof(node));s->data=q->data;r->next=s;r=s;q=q->next;}}r->next=NULL;s=c;c=c->next; /*删除头结点*/free(s);return(c);}2.9试用两种线性表的存储结构来解决约瑟夫问题。
设有n个人围坐在圆桌周围,现从第s 个人开始报数,数到第m个人出列,然后从出列的下一个人重新开始报数,数到第m个人又出列,…,如此重复直到所有的人全部出列为止。
例如当n=8,m=4,s=1,得到的新序列为:4,8,5,2,1,3,7,6。
写出相应的求解算法。
解:先构造一个循环链表nodetype *crea(int n){ nodetype *s,*r,*h;int I;for (i=1;i<=n;i++){ s=(nodetype *)malloc(sizeof (nodetype));s->data=I;s->next=NULL;if(i==1) h=s;else r->next=s;r=s;}r->next=h;return h;}void jese (nodetype *h,int m){ nodetype *p=h,*q;int I;while (p->next!=p){for (i=1;i<m-1;i++)p=p->next;if (p->next!=p){ q=p->next;printf(“%d”,q->data);p->next=q->next;free(q);}p=p->next;}printf(“%d”,p->data);}2.10已知单链表中的数据元素含有三类字符(即:字母字符、数字字符和其它字符),试编写算法构造三个环形链表,使每个环形链表中只含同一类的字符,且利用原表中的结点空间作为这三个表的结点空间,头结点可另辟空间。