输入:studemt
显示:Wrong! The word is : student 【含有错误的源程序】
#include
int funtest(char *cp, int i) { int ix=0, iy=0, ik=0;
char ca[20]={0}, cb[20]={0}; strcpy(ca, cp); ik=strlen(ca); ix=rand( )%ik; iy=rand( )%ik;
while(iy==ix)iy=rand( )%ik; ca[ix]='*'; ca[iy]='*'; cout<<\
cout<<”Please input correct word:\ gets(cb);
if(cb==cp[i]) return 1; else return 0; }
void main()
{ char cs[5][20]=\ int i,ik=0; unsigned n;
cout<<”请输入一个随机数的种子” cin>>n; srand( ) i=rand( )%5;
ik=funtest(cs,i); if(ik)
cout<<\ else
cout<<\}
实验七
一、
编程题
16
N个人围成一圈,他们的序号依次为1~n,从第一个人开始顺序报数1、2、3……、m,报到m者退出圈子,并输出退出圈子的人的序号。用一个结点的环形链表模拟围成一圈的人。假定有10个人围城一圈,凡报到5者退出圈子,则 退出圈子的人的序号依次为5、10、6、2、9、8、1、4、7,最后留在圈中的人是3号。是单向环形链表的结构,其中head指向第一个人。
struct Node{ int x;
Node *next; }; 要求:
1. 函数Node * creat (int n)用来创建一个有n个节点的环形链表,他们
的序号依次为1~n,函数返回头结点;
2. 函数void del(Node *h , int m) 实现从第一个人开始顺序报数1、2、
3……、m,报到m者退出圈子,并输出退出圈子的人的序号。接着再顺序报数,直到圈子中留下一个人为止。
3. 主函数要求从键盘输入m,n的值,并调用函数creat函数完成建立环
形链表,和del函数依次输出退出圈子的人的序号,找出圈子中留下的最后一个人。 #include
node *next; };
node *creat(int n) {
node *p1,*p2,*head=0;
cout<<\建立环形链表:\ for(int i=1;i<=n;i++) { p1=new node; p1->x=i; if(head==0) {head=p1;p2=p1;} else {p2->next=p1;p2=p1;} }
if(head) p2->next=head; return(head); }
void print(const node *head) {
const node *p; p=head;
cout<<\输出链表上的各结点:\ for(int i=1;i<=10;i++) { cout<
17
}
cout<<'\\n'; }
void del(node *head,int m) {
node *h,*p;
int i=1,q=0,a=10; p=head;
while(a!=1) { h=p; p=p->next; i++; if(i%5==0) { q++;
cout<<\第\个退出圈子的是序号为:\的人\ h->next=p->next; delete p; p=h->next; i++; a--; } if(a==1) { cout<<\最后留在圈中的是序号为:\的人\ delete p; } } }
void main() {
int m,n; node *head;
cout<<\请输入游戏的人数:\ cin>>n;
head=creat(n); print(head);
cout<<\顺序报数退出队列的m数为:\ cin>>m; del(head,m); }
二、
编程题
用函数函数Node * creat (int n))建立两个有序的同结构的单向链表(结点包
18
含一个整型数和一个指向本结点的类型的指针);用函数 merge(Node * h1 , Node * h2 )合并这两个链表;用函数void prin(Node * h)输出结果,主函数如下。用下列数据测试程序的正确性。
链表1: 5,9,21,36,58,79,81
链表2:
3,4,8,16,34,78,90,100,101 void main( ){
list *h1, *h2, *h3; h1=creat( ); h2= creat( ); print(h1); print(h2);
h3=merge (h1,h2); print(h3);
del(h1); del(h2); del(h3); }
#include
Node * creat (int n);
Node *merge(Node * h1 , Node * h2 ); Node* insert(Node * h,Node *p); void print(Node *); void del(Node *h);
void main( ){ Node *h1, *h2, *h3; int n; cout<<\请输入第一条链表的结点数:\ cin>>n; h1=creat(n); cout<<\请输入第二条链表的结点数:\ cin>>n; h2= creat(n); print(h1);
19
print(h2); h3=merge (h1,h2); print(h3); del(h1); del(h2); del(h3); }
Node * creat (int n){ Node *h=0,*p1,*p2; cout<<\请输入:\\n\ while(n-->0){ p1=new Node;//建 cin>>p1->date;//录 if(h==0) h=p2=p1; else{ p2->next=p1;//链 p2=p1; //移 } } p2->next=0; return h; }
Node *merge(Node * h1 , Node * h2 ){ Node *h3=0,*p1,*p2,*p3; p1=h1,p2=h2; while(p1!=0 && p2!=0){ if(p1->date
20