第十二周作业

这个作业属于哪个课程 C语言程序设计II
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/2824
我在该课程的目标是 预习相关的链表的知识,并明白链表的使用意义。同时了解java的创建对象,创建对象的作用及其意义
这个作业在那个具体方面帮助我实现目标 理解并灵活使用递归函数
参考文献 Java从入门到精通,C primer plus,c语言程序设计
任务一.试题一:
6-1 计算最长的字符串长度 (15 分)

本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。
函数接口定义:

int max_len( char *s[], int n );

其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。
裁判测试程序样例:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 10
#define MAXS 20

int max_len( char *s[], int n );

int main()
{
    int i, n;
    char *string[MAXN] = {NULL};

    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        string[i] = (char *)malloc(sizeof(char)*MAXS);
        scanf("%s", string[i]);
    }
    printf("%d\n", max_len(string, n));

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

4
blue
yellow
red
green

输出样例:

6

2.分析:本题比较简单,一次AC,实际上我们只需码上题中给出的处理函数即可!所以放开自我,放心大胆地动手吧!
3.执行代码:

 int max_len( char *s[], int n )
{
	int max=0;
	for(int i=0;i<n;i++){
		strlen(s[0])==max;
		if(strlen(s[i])>max){
			max=strlen(s[i]);
		}
	}
	return max;
} 

4.运行正确截图

5.流程图:
6.本题总结:该题难度不大,只要能动手,基本能做对,实为水题!
任务二:试题二.6-2 统计专业人数 (15 分)

本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。链表结点定义如下:

struct ListNode {
    char code[8];
    struct ListNode *next;
};

这里学生的学号共7位数字,其中第2、3位是专业编号。计算机专业的编号为02。
函数接口定义:

int countcs( struct ListNode *head );

其中head是用户传入的学生学号链表的头指针;函数countcs统计并返回head链表中专业为计算机的学生人数。
裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct ListNode {
    char code[8];
    struct ListNode *next;
};

struct ListNode *createlist(); /*裁判实现,细节不表*/
int countcs( struct ListNode *head );

int main()
{
    struct ListNode  *head;

    head = createlist();
    printf("%d\n", countcs(head));
	
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

1021202
2022310
8102134
1030912
3110203
4021205
#

输出样例:

3

2.分析:本题同上,难度并不大,输入函数不需要我们去写出,所以难度降低了,直接写即可,但需要注意的是:我们不能只对字符串进行一次操作就完了,指完一次字符串则需要进行下一组字符串的*head指向操作!
3.执行代码:

 int countcs( struct ListNode *head )
{
	int jishu=0;
	for(int j=0;head!=NULL;j++){
		if(head->code[1]=='0'&&head->code[2]=='2')
		jishu++;
		head=head->next;
	}
	return jishu;
}

4.运行正确截图:
5.流程图:
6.总结:本题实为水,但是我还是做了二三十分钟才做出来,不过感觉还好,难度不算的太大!
任务三:试题三.6-3 删除单链表偶数节点 (20 分)

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:

struct ListNode {
    int data;
    struct ListNode *next;
};

函数接口定义:

struct ListNode *createlist();
struct ListNode *deleteeven( struct ListNode *head );

函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针。
裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *createlist();
struct ListNode *deleteeven( struct ListNode *head );
void printlist( struct ListNode *head )
{
     struct ListNode *p = head;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    struct ListNode *head;

    head = createlist();
    head = deleteeven(head);
    printlist(head);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

1 2 2 3 4 5 6 7 -1

输出样例:

1 3 5 7 

2.分析:需要输入码上处理函数以及相关的输入函数,首先需要将

struct ListNode *createlist()

链表初始化即可!
3.执行代码:

struct ListNode *createlist()
{
    int x;
    struct ListNode *head,*tail,*p;
    head=(struct ListNode*)malloc(sizeof(struct ListNode));
    head->next=NULL;
    tail=head;
    while(1)
    {
        p=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->next=NULL;
        scanf("%d",&x);
        if(x==-1)
            break;
        p->data=x;
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return head;
}
struct ListNode *deleteeven( struct ListNode *head )
{
    struct ListNode *p1,*p2;
    int flag;
    p1=head;
    p2=p1->next;
    while(p1->next)
    {
        flag=0;
        if(p2->data%2==0)
        {
            p1->next=p2->next;
            p2=p2->next;
            flag=1;
        }
        if(flag==0)
        {
            p1=p1->next;
            p2=p1->next;
        }
    }
    return head->next;
}

4.总结:本题需要码上输入函数,处理函数,所以我们需要写两函数,注意遍历*head时,我们需要注意到偶数链表的删除!
5.运行结果截图:
6.流程图:

任务四:估值一亿的AI核心代码 (20 分)
本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
无论用户说什么,首先把对方说的话在一行中原样打印出来;
消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
把原文中所有大写英文字母变成小写,除了 I;
把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
把原文中所有独立的 I 和 me 换成 you;
把原文中所有的问号 ? 换成惊叹号 !;
在一行中输出替换后的句子作为 AI 的回答。
输入样例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

输出样例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know

2.分析:本题难度过大,我对此没有什么解决措施,所以我只能附上其链接

http://www.pianshen.com/article/5255311811/

3.本题询问了一下向淇同学,该题的基本算法使用了维克托数组法,此处附上其伪码,后续我在学习算法的时侯再去深究该算法:

void solve(char * s,char * bj)
{
	cout<<"AI:";
    char strxxx[MAXN][MAXN]={'\0'};
   int x = 0,a=0,b=0;
  strlwr(s);
  int m =strlen(s);
  vector<char> str;
  for(int i = 0 ; i < m ; i++)
  {
    str.push_back(s[i]);	
  }
  
  for(int i = 0 ; i < m ; i++)
  {
  	if((s[i]==' '&&s[i+1]==' ')||(s[i]==' '&&bj[i+1]==1)||(s[i]==' '&&s[i+1]=='!')||(s[i]==' '&&s[i+1]=='?')||(s[i]==' '&&s[i+1]=='.')){
	  str.erase(str.begin()+(i-x));
	  x++;} 
	if(s[i]=='?')
	{
		str.erase(str.begin()+(i-x));
	    str.push_back('!');
	}
  }
 // for(int i = 0 ; i < m-x ; i++)
  //cout<<str[i];
  //cout<<endl;
  for(int i = 0 ; i < m-x ; i++){
  if(str[i]==' '){a++;b=0;}
  else strxxx[a][b++]=str[i];
  }
  
  //for(int i = 0 ; i < m-x ; i++)
  //cout<<strxxx[i];

  	if(strcmp(strxxx[0],"could")==0&&strcmp(strxxx[1],"you")==0)
	{
    cout<<"I could";
    for(int i = 2 ; i < m-x ; i++)
    {
    	if(strcmp(strxxx[i],"me")==0||strcmp(strxxx[i],"I")==0)cout<<" you";
        else cout<<" "<<strxxx[i];
    }
    }
    
    else if(strcmp(strxxx[0],"can")==0&&strcmp(strxxx[1],"you")==0)
    {
    cout<<"I can";
    for(int i = 2 ; i < m-x ; i++)
    {
    	if(strcmp(strxxx[i],"me")==0||strcmp(strxxx[i],"I")==0)cout<<" you"; 	
        else cout<<" "<<strxxx[i];
    
    }
	}
	else
	{
		for(int i = 0 ; i < m-x ; i++)
		{
			if(strcmp(strxxx[i],"me")==0||strcmp(strxxx[i],"I")==0||strcmp(strxxx[i],"I")==0)cout<<" you"; 	
			else cout<<" "<<strxxx[i];
		}

	}
    cout<<endl;
    
}

任务五:***八皇后问题 (20 分)

在国际象棋中,皇后是最厉害的棋子,可以横走、直走,还可以斜走。棋手马克斯·贝瑟尔 1848 年提出著名的八皇后问题:即在 8 × 8 的棋盘上摆放八个皇后,使其不能互相攻击 —— 即任意两个皇后都不能处于同一行、同一列或同一条斜线上。
现在我们把棋盘扩展到 n × n 的棋盘上摆放 n 个皇后,请问该怎么摆?请编写程序,输入正整数 n,输出全部摆法(棋盘格子空白处显示句点“.”,皇后处显示字母“Q”,每两格之间空一格)。
输入格式

正整数 n (0 < n ≤ 12)

输出格式

若问题有解,则输出全部摆法(两种摆法之间空一行),否则输出 None。

要求:试探的顺序逐行从左往右的顺序进行,请参看输出样例2。
输入样例1

3

输出样例1

None

输入样例2

6

输出样例2

. Q . . . .
. . . Q . .
. . . . . Q
Q . . . . .
. . Q . . .
. . . . Q .

. . Q . . .
. . . . . Q
. Q . . . .
. . . . Q .
Q . . . . .
. . . Q . .

. . . Q . .
Q . . . . .
. . . . Q .
. Q . . . .
. . . . . Q
. . Q . . .

. . . . Q .
. . Q . . .
Q . . . . .
. . . . . Q
. . . Q . .
. Q . . . .

2.本题从网上资料检索到:使用剪枝法的解题效率还是挺高的,避免了多数无用的搜索,所以代码附在这里了!

 #include<stdio.h>
int X[100],n,cnt=0;
void shuchu()
{
	int i,j;
	printf("\n",++cnt);
	for(i=1;i<=n;i++)
	{
	   for(j=1;j<X[i];j++) printf(".");
	   printf("Q");
	   for(j=X[i]+1;j<=n;j++) printf(".");
	   printf("\n");
	}
}
int jianzhi(int k,int i)
{
	int j;
	for(j=1;j<k;j++)
	   if(X[j]==i || X[j]-i==j-k || X[j]-i==k-j)     //第一个条件判断是否有同列的 
	             return 0;
	             //第二个条件判断是否有对角的 
	return 1;
}
void f(int k)
{   int i;
	if(k-1==n) shuchu();
	else for(i=1;i<=n;i++)
	      if(jianzhi(k,i))
	{
		X[k]=i;
		f(k+1);
	}
}
int main()
{
	scanf("%d",&n);
	f(1);
	return 0;
}

2.运行结果截图:

ps:PTA上被报格式错误,但是在c++上可以完美运行,后期再慢慢查找
任务六:学习总结--本周我简单学习了相关的c语言链表知识,发现其还是以指针为基础,只有扎实的指针基础才能把链表熟练掌握,话不多说,下一周要开始学习游戏设计项目开发了,先把最基础的知识掌握好,同时还要多去拓展更多的算法知识,我发现一个好的算法,能让程序的运行效率提高数倍!
学习表格:
学习统计图:
任务七:预习作业.从第十三周开始,将进入课程设计阶段,请在本次作业中给出:
1.所在小组想要开发的项目的名称和目标;

《能跳动并会变色的小球》

2.项目主体功能的描述;

小球的上下跳动,除了能对其速度控制之外,同时也要每弹跳一次并会变色一次

3.现阶段已做的准备工作;

对c语言的游戏设计教本第一章开始预习,并且合理调用windows以及c++库函数,例如:#include<windows.h>&&windows<color.h>

4.小组成员名单和进度安排。(课程设计阶段:13-17周)

黄桢淇,王雅琼,周旭,吴昊
进度:分工合作,本组已经暂时完成了小球的跳动步骤,在本周内定将小球的变色运动程序编译出来!同时王雅琼,周旭同学负责代码问题的排查,黄桢淇,吴昊同学负责代码的编写以及相关思路的规划。
posted @ 2019-05-13 22:42  千倍于太阳的光辉  阅读(282)  评论(0编辑  收藏  举报