zoukankan      html  css  js  c++  java
  • 第3次作业

    作业要求一

    1)C高级第三次PTA作业(1)




    2)C高级第三次PTA作业(2)

    作业要求二

    1)C高级第三次PTA作业(1)

    6-1 输出月份英文名

    1.设计思路

    1.定义十二个月份
    2.判断n是否超出范围,若没有输出那个所指的月份

    2.实验代码

    'char *getmonth( int n )
    {
      char *month[13]={"January","February","March","April","May","June","July","August","September","October","November","December"};
      if(n>=13||n<=0){
        return NULL;
      }else{
        return *(month+n-1);
        }
    }'
    

    3.本题调试过程碰到问题及解决办法

    错误信息1:PTA显示答案错误
    改正方法:结果找到发现是我忘记了n需要减去1才是所指月份

    6-2 查找星期

    1.设计思路

    1.定义七个星期
    2.利用循环先用判断判断是否为0—6
    3.如果不是返回-1,如果是则返回星期

    2.实验代码

    'int getindex( char *s ) {
      char day[7][MAXS]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; 
      int i; 
      for(i=0;i<7;i++) {
        if(strcmp(*(day+i),s)==0) 
        return (i); 
      } 
      if(i==7) 
      return (-1);
    }'
    

    6-3 计算最长的字符串长度

    1.设计思路

    1.定义最大值和总和
    2.利用循环求出所有字符串的长度
    3.如果有字符串长度比前一个长,就取代前一个长度

    2.实验代码

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

    6-4 指定位置输出字符串

    1.设计思路

    1.定义指针p
    2.利用循环判断第一个字符
    3.再利用循环输出之间的每个字符
    4.找出最后的字符,输出

    流程图

    2.实验代码

    'char *match( char *s, char ch1, char ch2 )
    {
      int i,n;
      char *p=NULL;
      for(i=0;*(s+i)!='';i++)
      {
        if(*(s+i)==ch1){
          p=&s[i];
          for(n=i;*(s+n)!='';n++){
            if(*(s+n)!=ch2){
              printf("%c",*(s+n));
            }if(*(s+n)==ch2){
              printf("%c
    ",*(s+n));
              return p; 
            }
          }printf("
    ");  
          return p;  
        }
      }if (*(s+i)=='')
        {
          p=&s[i];
        }
      printf("
    ");  
      return p;
    }'
    

    2)一道编程题

    本题在同学帮助下完成

    实验代码

    、#include<stdio.h>
    int main()
    {
    int m=0,n=0,i=0,j=0,flag=0;
    scanf("%d %d",&m,&n);
    flag=m*n;
    int *p = (int *)malloc((m*n) *sizeof(int));
    int *q = (int *)malloc((m*n) *sizeof(int));
    for(i=0;i<flag;i++) 
    {
    p[i] = i+1;
    }
    for(i=0;i<flag;i++) {
    for(j = i+1;j<=flag;j++) {
        if(p[i] !=1&&p[j] != 1) {
            if(p[j]%p[i] ==0) {
                p[j] = 1;
            }
        }
    }
    }
    j=0;
    for(i=0;i<flag;i++) {
    if(p[i] != 1) {
        printf(" %d",p[i]);
        j++;
    } 
    if(j == 5) {
        printf("
    ");
        j=0;
    }
    }
    }`
    

    3)C高级第三次PTA作业(2)

    6-1 奇数值结点链表

    1.设计思路

    第一:利用while循环在当前链表存下date
    第二:判断是否为空并链接下一链表
    第三:用一个while语句遍历链表
    第四:判断是否为单数如果是p=n,或p=m

    2.实验代码

    'struct ListNode *readlist()
        {
    	    int data;
    	    struct ListNode *head=NULL,*p=NULL,*tail=NULL;
    	    scanf("%d",&data);
    	    while(data != -1){
    		    p = (struct ListNode *)malloc(sizeof(struct ListNode));
    		    p->data = data;
    		    p->next = NULL;
    		    if(head == NULL){
    			    head = p;
        		    tail = p;
        	    }else{
        		    tail->next = p;
        		    tail = p;
        	    }
    		    scanf("%d",&data);
        }
    	    return head; 
    
        } 
    
    struct ListNode *getodd( struct ListNode **L )
    {
         struct ListNode *p = *L,*m=NULL,*n=NULL,*head1=NULL,*head2=NULL;
         head1=(struct ListNode*)malloc(sizeof(struct ListNode));
         head2=(struct ListNode*)malloc(sizeof(struct ListNode));
        head1->next=NULL;
        head2->next=NULL;
        m=head1;
        n=head2;
         while (p) {
               if((p->data)%2 == 1){
           		    n->next=p;
    		    	n=p;
    		     }else{
    		 	    m->next=p;
    			    m=p;
    			     }
                   p = p->next;
         }
         m->next=NULL;
        n->next=NULL;
        *L = head1->next;	
        return head2->next;
    }'
    

    6-2 学生成绩链表处理

    1.设计思路

    第一:利用while函数对链表进行数据的输入
    第二:利用while函数进行链表删除,找出前一位与后一位
    第三:设立一个新函数除了那个值重新写入

    2.实验代码

    'struct stud_node *createlist()
    {
        struct stud_node *tail=NULL,*head=NULL,*p=NULL;
        int num=0,score=0;
        char name[20];
        scanf("%d",&num);
        while(num!=0)
        {   
    	    p=(struct stud_node*)malloc(sizeof(struct stud_node));
    	    p->num=num; 
    	    scanf("%s %d",p->name,&p->score);
    	    if(head==NULL)
    	    {
    		    head=p;
    	    }else
    	    {
    		    tail->next=p;
    	    }
    	    tail=p;
    	    scanf("%d",&num);
    	    p->next=NULL;
        }
        return head;
    }
    struct stud_node *deletelist( struct stud_node *head, int min_score )
    {
        struct stud_node *ptr1=NULL,*ptr2=NULL;
        for(;head!=NULL;head=head->next)
        {
    	    if(head->score>=min_score)
    	    {
    		    if(ptr1==NULL)
    		    {
    		    	    ptr1=head;
    		    }else
    		    {
    			    ptr2->next=head;
    		    }
    		    ptr2=head;
    	    }
        }
        if(ptr1==NULL)
        {
    	    return NULL;
        }else
        {
    	    ptr2->next=NULL;
        }
        return ptr1;
    }'
    

    3.本题调试过程碰到问题及解决办法

    6-3 链表拼接

    1.设计思路

    第一:利用while函数对链表进行数据的输入
    第二:利用while函数进行链表删除,找出前一位与后一位
    第三:写入新的数据

    2.实验代码

    'struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
    {
        int list[100],i=0,j=0,swap=0,count=0;
        while(list1!=NULL)
        {
    	    list[i]=list1->data;
    	    i++;
    	    list1=list1->next;
    	    count++;
        }
        while(list2!=NULL)
        {
    	    list[i]=list2->data;
    	    i++;
    	    list2=list2->next;
    	    count++;
        }
        for(i=0;i<count;i++)
        {
    	    for(j=i+1;j<count;j++)
    	    {
    		    if(list[i]>list[j])
    		    {
    			    swap=list[i];list[i]=list[j];list[j]=swap;
    		    }
    	    }
        }
        struct ListNode *p=NULL,*head=NULL,*tail=NULL;
        for(i=0;i<count;i++)
        {
    	    p=(struct ListNode*)malloc(sizeof(struct ListNode));
    	    p->data=list[i];
    	    p->next=NULL;
    	    if(head==NULL)
    	    {
    		    head=p;
    	    }else
    	    {
    		    tail->next=p;
    	    }
    	    tail=p;
        }
        return head;
    }'
    

    要求三、学习总结和进度

    1、总结两周里所学的知识点,回答下列问题

    (1)如何理解指针数组,它与指针、数组有何关系?为何可以用二级指针对指针数组进行操作?

    指针数组就是数组之内的数据用指针表示,通过二级指针去访问二维数组需要先给二级指针分配等同于二维数组行数的一维数组指针。

    (2)将C高级第三次PTA作业(1)任何一个题目改为使用二级指针对指针数组进行操作。

    `int getindex( char *s ) {
      char day[7][MAXS]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; 
      int i; 
      for(i=0;i<7;i++) {
        if(strcmp(day+i,s)==0) 
        return (i); 
      } 
      if(i==7) 
      return (-1);
    }`
    

    (3)用指针数组处理多个字符串有何优势?可以直接输入多个字符串给未初始化的指针数组吗?为什么?

    指针数组可以节省内存。不可以。没有分配内存空间。

    2、将PTA作业的源代码使用git提交到托管平台上,要求给出上传成功截图和你的git地址。

    git地址:https://git.coding.net/wuyaole/Cgaojidisancizuoye.git

    3、点评3个同学的本周作业

    丰大为:http://www.cnblogs.com/DavidPark/p/8778993.html
    顾家玮:http://www.cnblogs.com/qq807443119/p/8909434.html
    高立彬:http://www.cnblogs.com/gao628526/p/8781936.html

    4、表格和折线图呈现本周


  • 相关阅读:
    win10 uwp 装机必备应用 含源代码
    win10 uwp 装机必备应用 含源代码
    win10 uwp clone
    win10 uwp clone
    win10 输入法禁用IME
    win10 输入法禁用IME
    win10 输入法禁用IME
    查看SQL运行时间
    [学习笔记]三元环
    数据库范式
  • 原文地址:https://www.cnblogs.com/wsbqz/p/8906819.html
Copyright © 2011-2022 走看看