• 第十二周作业


    2019第十二周作业

    2019年春季学期第十二周作业


    课程名称 c语言程序设计2
    作业要求 第十二周作业
    我的课程目标 了解指针与函数的关系,掌握指针作为函数返回值
    这个作业在哪个方面帮助我实现目标 了解了指针与函数的关系及指针与函数的关系
    参考文献 课本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
    
    

    (1)实验代码

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

    (2)设计思路

    流程图

    (3)实验遇到的问题及解决方案

    (4)运行结果截图

    基础作业二

    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
    

    (1)实验代码

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

    (2)设计思路

    流程图

    (3)实验遇到的问题及解决方案

    (4)运行结果截图

    基础作业三

    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 
    

    (1)实验代码

    struct ListNode *createlist()
    {
        struct ListNode *head,*tail,*p;
        int size=sizeof(struct ListNode);
        int n; 
        head=(struct ListNode*)malloc(size);
        head->next=NULL;
        tail=head;   
        while(p!=NULL)
        {
            p=(struct ListNode*)malloc(size);
            p->next=NULL;        
            scanf("%d",&n);
            if(n==-1)
                break;
            p->data=n;
            p->next=NULL;
            
            tail->next=p;
            tail=p;
        }
        return head;
    }
    struct ListNode *deleteeven( struct ListNode *head )
    {
        struct ListNode *pr1,*pr2;
        int f;   
        pr1=head;
        pr2=pr1->next;
        while(pr1->next)
        {
            f=0;
            if(pr2->data%2==0)
            {
                pr1->next=pr2->next;
                pr2=pr2->next; 
                f=1;
            }
            if(f==0)
            {
                pr1=pr1->next;
                pr2=pr1->next;
            }
        }  
        return head->next;
    }
    
    

    (2)设计思路

    流程图

    (3)实验遇到的问题及解决方案

    (4)运行结果截图

    挑战作业

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

    我的参考文献

    7-2 求迷宫最短通道 (20 分)

    递归求解迷宫最短通道的总步长。输入一个迷宫,求从入口通向出口的可行路径中最短的路径长度。为简化问题,迷宫用二维数组 int maze[10][10]来存储障碍物的分布,假设迷宫的横向和纵向尺寸的大小是一样的,并由程序运行读入, 若读入迷宫大小的值是n(3<n<=10),则该迷宫横向或纵向尺寸都是n,规定迷宫最外面的一圈是障碍物,迷宫的入口是maze[1][1],出口是maze[n-2][n-2], 若maze[i][j] = 1代表该位置是障碍物,若maze[i][j] = 0代表该位置是可以行走的空位(0<=i<=n-1, 0<=j<=n-1)。求从入口maze[1][1]到出口maze[n-2][n-2]可以走通的路径上经历的最短的总步长。要求迷宫中只允许在水平或上下四个方向的空位上行走,走过的位置不能重复走。
    
    输入格式:
    
    输入迷宫大小的整数n, 以及n行和n列的二维数组(数组元素1代表障碍物,0代表空位)
    
    输出格式:
    
    若有可行的通道则输出一个整数,代表求出的通道的最短步长;若没有通道则输出"No solution"
    
    输入样例:
    
    10
    1 1 1 1 1 1 1 1 1 1
    1 0 0 1 0 0 0 1 0 1
    1 0 0 1 0 0 0 1 0 1
    1 0 0 0 0 1 1 0 0 1
    1 0 1 1 1 0 0 0 0 1
    1 0 0 0 1 0 0 0 0 1
    1 0 1 0 0 0 1 0 0 1
    1 0 1 1 1 0 1 1 0 1
    1 1 0 0 0 0 0 0 0 1
    1 1 1 1 1 1 1 1 1 1
    上述输入代表的是如下这样一个迷宫:
    

    其中红色的小方块是障碍物,蓝色的小方块是空位,白色的小圆连起来是一条从入口到出口的通道,两个圆之间代表一个步长。
    
    输出样例:
    
    14
    

    我的参考文献

    预习作业

    1.项目名称:飞机大战。目标:发射激光。
    2项目主体功能:飞机能发射子弹并且击中目标,游戏能判断胜负,而且能判断音效。
    3线阶段准备工作:对飞机控制的预习。
    4成员:龙佳思,王俊杰,黄松
    
    

    学习感悟

     
    

    结对编程感想

      
    

    学习进度

    折线图

  • 相关阅读:
    KETTLE:mongdb与mysql互传数据
    6.azkban的监控
    5.azkaban权限管理
    3.azkaban3.0测试
    2.azkaban3.0安装
    1.编译azkaban
    十六:The YARN Service Registry
    十四:Using CGroups with YARN
    十二:NodeManager
    十 Writing YARN Applications
  • 原文地址:https://www.cnblogs.com/tuziwangjj/p/10884020.html
Copyright © 2020-2023  润新知