第三次作业

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

2)一道编程题

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

6-1 输出月份英文名
//设计思路

1)文字描述

1.读入n;
2.用switch对n选择返回值;
3.返回月份字符串;

2)调试错误及修改

//代码部分

char *getmonth( int n )
{
    switch(n)
    {
    case 1:return "January";
    case 2:return "February";
    case 3:return "March";
    case 4:return "April";
    case 5:return "May";
    case 6:return "June";
    case 7:return "July";
    case 8:return "August";
    case 9:return "September";
    case 10:return "October";
    case 11:return "November";
    case 12:return "December";
    default:return NULL;
   }
}

6-2 查找星期
//设计思路
*
1)文字部分

1.将星期的英文赋给二维数组;
2.遍历一维数组,比较数组中每行元素与输入的字符串是否相同,若相同则跳出循环;
3.返回值i;

2)调试错误及修改

3)流程图
主函数

getindex

//代码部分

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

#define MAXS 80

int getindex( char *s );

int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);

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

6-3 计算最长的字符串长度
//设计思路
1)文字部分

1.读入s;
2.用for进行历遍,并在for中另用一for进行计数;
3.把s中各元素的长度2存到数组a中;
4.进行比较,记录最大值下标max;
5.返回a[max];

2)调试错误及修改

//代码部分

#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;
}
int max_len( char *s[], int n )
{
  int a[n],i,k,max=0;
  for(i=0;i<n;i++)
  {
  	char *b=s[i];
    for(k=0;;k++)
    {
      if(b[k]=='\0')
      {
      a[i]=k;
      break;
      }
    }
    if(a[max]<a[i])
    max=i;
  }
  return a[max];
}

6-4 指定位置输出字符串
//设计思路
1)文字部分

1.读入s,ch1,ch2;
2.用for循环确定ch1在s中的位置,若查找不到则跳出;
3.再次用for循环,输出单个字符,并确定ch2位置,若不存在则跳出;
4.返回s;

2)调试错误及修改

错误:测试点1 sample2, ch1找不到, ch2找到,输出和例二不同,少一个空行输出,在DEV环境输出结果无错误;
解决方法:向同学询问后认识到缺少空行,加入了输出空行;

//代码部分

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

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

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

int main()
{
    struct ListNode *L, *Odd;
    L = readlist();
    Odd = getodd(&L);
    printlist(Odd);
    printlist(L);

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

一道编程题
//代码部分

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int a=201,b=3943,f,*n;
	n=(int *)malloc((a*b)*sizeof(int));
	int i,j;
	for(i=2;i<=a*b;i++)
	{
	n[i-2]=i;
	}
	n[i-1]=0;
	for(i=0;n[i]!=0;i++)
	{
		if(n[i]!=1)
		{
		for(j=i+1;n[j]!=0;j++)
		{
			if(n[j]%n[i]==0)
			{
			n[j]=1;
			}
		}
		}
	}
	for(i=2;i<(a*b-2);i++)
	{
	if(n[i]!=1)
	{
	printf("%d ",n[i]);
	f++;
	if(f==5)
	{
	printf("\n");
	f=0;
    }
	}
	}
	free(n);
}
//该代码需要执行80秒左右出结果,可能为堆栈之间区别问题;
/*同时我看了董雅洁同学本题代码,与我同样a,b的情况下执行非常慢,我的DEV对董雅洁同学的代码在与我同样a,b情况下执行了近5分钟没有得出结果,希望老师能讲解一下*/

//运行结果

可以看到程序执行使用了81s;

6-1 奇数值结点链表
//设计思路

1)文字部分

1.在readlist中,要求用输入的值建立链表,用p->data==-1判链表结束;
2.定义head作为链表头文件,并且定义链表n放置输入元素;
3.在getodd中,要求将建立的链表拆分为两个链表,将单数和双数分配;
4.定义h1,h2作为两新链表头文件;
5.历遍链表,将单数放置入j链表,指向h1,将双数放置入k链表,指向h2;
6.将k链表赋给原链表,并返回h1链表;

2)调试中的错误及修改

3)流程图
主函数

readlist

getodd

//代码部分

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

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

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

int main()
{
    struct ListNode *L, *Odd;
    L = readlist();
    Odd = getodd(&L);
    printlist(Odd);
    printlist(L);

    return 0;
}
struct ListNode *readlist()
{
  struct ListNode *p,*head,*n;
  head=(struct ListNode*)malloc(sizeof(struct ListNode));
  n=head;
  while(1)
  {
  	p=(struct ListNode*)malloc(sizeof(struct ListNode));
  	scanf("%d",&p->data);
    if(p->data==(-1))
    break;
    n->next=p;
    n=p;
  }
  n->next=NULL;
  return head->next;
}
struct ListNode *getodd( struct ListNode **L )
{
  struct ListNode *i,*j,*k,*h1,*h2;
  j=h1=(struct ListNode*)malloc(sizeof(struct ListNode));
  h1->next=NULL;
  k=h2=(struct ListNode*)malloc(sizeof(struct ListNode));
  h2->next=NULL; 
  i=*L;
  int n=0;
  for(;i;i=i->next)
  {
    if((i->data)%2==1)
    {
    j->next=i;
	j=i;
    }
    else
    {
    k->next=i;
	k=i;
    }
  }
  j->next=NULL;
  k->next=NULL;
  *L=h2->next;
  return h1->next;
}

6-2 学生成绩链表处理
//设计思路

1)文字部分

1.在createlist中,将输入的数建立一个新链表,并将其返回;
2.定义head作为链表头文件,并定义链表p作储存用,当输入0时终结链表;
3.返回链表头文件;
4.在deletelist中,将读入的链表删除低于读入数字int min_score的链表;
5.对链表进行历遍,当p->score小于min_score时将链表向前推进,挤掉要删除项;
6.返回链表头文件

2)调试中的错误及修改

//代码部分

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

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};

struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;

    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}
struct stud_node *createlist()
{
  struct stud_node *p,*head,*n;
  head=(struct stud_node*)malloc(sizeof(struct stud_node));
  n=head;
  while(1)
  {
  	p=(struct stud_node*)malloc(sizeof(struct stud_node));
  	scanf("%d ",&p->num);
  	if(p->num==0)
    break;
    scanf("%s %d",&p->name,&p->score);
    n->next=p;
    n=p;
  }
  n->next=NULL;
  return head->next;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
	struct stud_node *p,*k;
	p=(struct stud_node*)malloc(sizeof(struct stud_node));
	k=p;
	for(;head;head=head->next)
	{
		if((head->score)>=min_score)
		{
			k->next=head;
			k=head;
		}
	}
	k->next=NULL;
	return p->next;
}

6-3 链表拼接
//设计思路

1)文字部分

1.在mergelists中,要求将读入的链表list1,list2拼接为一个链表;
2.定义h作为新链表头文件,并定义p用于储存新链表数值;
3.使用while结构用于排列当两链表同时没结束时,可利用链表原为升序链表这一特点进行排序
4.同第3点逻辑可实现对其中一链表未结束而另一链表以结束的两种情况的循环;
5.返回链表p;

2)调试中的错误及修改

错误:除去最后一个判断点,其它判断点都为段错误,实际为未考虑当一链表结束而另一链表未结束时的情况;
解决方法:新增两循环分别用于判断一链表结束而另一未结束情况;

//代码部分

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

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

struct ListNode *createlist(); /*裁判实现,细节不表*/
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);
void printlist( struct ListNode *head )
{
     struct ListNode *p = head;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    struct ListNode  *list1, *list2;

    list1 = createlist();
    list2 = createlist();
    list1 = mergelists(list1, list2);
    printlist(list1);
	
    return 0;
}
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
	struct ListNode *h,*p,*i,*k;
	h=(struct ListNode*)malloc(sizeof(struct ListNode));
	p=h;
	i=list1;
	k=list2;
	while(i!=NULL&&k!=NULL)
	{
		if(i->data<k->data)
		{
			p->next=i;
			i=i->next;
		}
		else
		{ 
			p->next=k;
			k=k->next;
		}
		p=p->next;
	}
	while(i)
	{
		p->next=i;
		i=i->next;
		p=p->next;
	}
	while(k)
	{
		p->next=k;
		k=k->next;
		p=p->next;
	}	
	p->next=NULL;
	return h->next;
}

1、总结两周里所学的知识点,回答下列问题?
(1)如何理解指针数组,它与指针、数组有何关系?为何可以用二级指针对指针数组进行操作?

我认为指针数组是对二维数组知识的一次提升,在函数中,它既可以做到以指针的方式返回给主函数,又可以像指针一样直观的储存元素,同时在处理若干字符串情况时,指针数组非常方便;

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

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

不必提前预设字符串长度。不行,因为未经过初始化指针数组存放的地址无法确定,编译器可能报错

2、将PTA作业的源代码使用git提交到托管平台上
https://git.coding.net/q871057265/lwq.git

3、点评3个同学的本周作业
赵寅胜:http://www.cnblogs.com/2017023960ZYS/p/8759455.html
徐铭博:http://www.cnblogs.com/xmb1547828350/p/8764221.html
刘宇:http://www.cnblogs.com/liuyuly/p/8914839.html
4、请用表格和折线图呈现你本周(4/9 8:00~4/23 8:00)的代码行数和所用时间、博客字数和所用时间

表格

折线

posted @ 2018-04-22 23:33  吉法师  阅读(282)  评论(0编辑  收藏  举报