第十二周总结

问题 答案
这个作业属于哪个课程 C语言程序与设计2
这个作业要求在哪里 https://pintia.cn/problem-sets?tab=1
我在这个课程的目标是什么 单链表的创立,插入,删除,遍历等一些重要内容
这个作业在哪个具体方面帮助我实现目标 让我了解它们的是怎样利用的与我该如何运用及理解
参考文献 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
", max_len(string, n));

    return 0;
}

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

输入样例:

4
blue
yellow
red
green

输出样例:

6

一:实验代码

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

二:设计思路

三:遇到的问题与解决
(1)在起初不知道怎样计算长度,(但借鉴上几周的代码中计算行数的代码的读入,找到应要写的strlen计算代码长度的“函数”吧)
四:运行截图

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
", countcs(head));
	
    return 0;
}

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

输入样例:

1021202
2022310
8102134
1030912
3110203
4021205
#

输出样例:

3

一:实验代码

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

二:设计思路

三:遇到的问题与解决
)(1)在其中遇到的问题也就是指向关系的错误我不知道指向哪里(通过继续对题目的深读,知道要指向那一段中第几个第几个数字在进行对count++)
四:运行截图

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("
");
}

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 

一:实验代码

typedef struct ListNode *List;
struct ListNode *createlist()
{
    List head=NULL, tail=NULL;    //tail相当于你输入的尾部,head是你输入的开头,开头与尾部相同
    int t;
    scanf("%d",&t);   //输入的数字
    while(t != -1){        //判断输入的条件
        List pnew = (List)malloc(sizeof(struct ListNode));
        pnew->data = t;     
        pnew->next = NULL;
        if(tail==NULL)
            head = tail =pnew;
        else{
            tail->next = pnew;
            tail = pnew;
        }
        scanf("%d",&t);
    }
    return head;
}
struct ListNode *deleteeven( struct ListNode *head )
{
    List p=head,q;
    while(head && head->data%2==0){ 
        p = head;
        head = head->next;
    }
        while(p->next && p->next->data%2==0){
            q = p->next;
            p->next = q->next;
            free(p);
        }
        p = p->next;
    }
    return head;
}

二:设计思路

三:遇到的问题与解决
(1)这个题目出现很多很多的错误,起初我用了两个free(P)来释放p,结果导致答案部分错误。(如今也没有没有找到正确答案)
(2)在创建新链中没有到typedef哪一行也导致编译错误(也无法找到正确答案)
(3)在指向关系中也没有正确理解。还在继续挣扎。
四:运行截图

挑战题

7-1 ***八皇后问题 (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 . . . .

一:实验代码

#include<stdio.h>
#include<math.h>
int n;
int a[20]={0};
int cnt=0;
void showtime()
{
	for(int i=1;i<n;i++){
		for(int j=1;j<=n;j++){
			if(a[i]==j)
			{
				printf("Q");
			}
			else
			{
				printf(".");
			}
			if(j!=n)
			{
				printf(" ");
			}
		}
		printf("
");
	}
	printf("
");
}
int judge(int x,int y)
{
	int j=1;
	while(x>j)
	{
		if(a[j]==y||(abs(x-j)==abs(a[j]-y)))
		{
			return 0;
		}
		j++;
	}
	return 1;
}

int start(int x);
{
	if(x>n)
	{
		showtime();
		cnt=1;
	}
	else
	{
		for(int y =1;y<=n;y++)
		{
			if(judge(x,y))
			{
				a[x]=y;
				strat(x+1);
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	start(1);
	if(cnt==0)
	{
		printf("None");
	}
	return 0;
}

二:设计思路
(1)此代码是通过观看助教版教程一遍一遍记忆中打出来的,也在慢慢理解他其中的思路,至少看了少于十遍,我就是相信功夫不负有心人.
还在慢慢理解思路中,等我有思路了马上写上。1=
(2)知道代码一样但是我相信理解之后会改代码,然后进行写输出自己的代码,我就不相信写不出自己的代码,还有时间,花时间可以解决很多很多问题,时间总会给人一个好的答案的!


为什么总是要虐我?

预习作业

1.所在小组想要开发的项目的名称和目标
大鱼吃小鱼
利用C语言编程进行对我小时候玩的游戏,体验自己做的小游戏
2.项目主体功能的描述;
针对对自己的鱼由小变大的过程,只能吃比自己的小的鱼类,里面也有一些障碍物影响我们游戏的进程,相应游戏成功后会有相应的奖励并通关。游戏结束,进行下一局更难的挑战。
3.现阶段已做的准备工作;
主要是对C语言游戏设计开发相应的教程进行研究。
4.小组成员名单和进度安排
成员:文加宁,段建红,陈新
进度安排:我们一起通过对书本的精读,然后各自安排各自的任务,文加宁与段建红负责对代码的调试,陈新负责对代码的编程,也要一起进行对代码的研究。

学习感悟

感觉自己越来越没有思绪,导致总在想我要编程,我要学代码
俺就是打不出,对它又热爱但努力总没有想的那样顺利打出
看书还是能理解但就是在实际的运用中无法成功运用,很无赖.
相信在最后剩下的学习中多去练吧,多看看别人的代码编写过程。

原文地址:https://www.cnblogs.com/chenxinxin/p/10868397.html