第3次作业

作业要求一 (20分)
完成下列编程题目,每次上完课都会增加2-3道题目,并将编程过程记录在博客里,一次PTA作业任选一道题目给出设计思路、流程图、源代码和错误记录,其他题目可只给出设计思路、源代码和错误记录。另外将每次PTA作业的提交列表贴在博客里,每次5分。

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

2)一道编程题:

有一个axb的数组,该数组里面顺序存放了从1到a*b的数字。其中a是你大学号的前三位数字,b是你大学号的后四位数字,比如你的学号是2017023936,那么数组大小是201 x 3936,数组中顺序存放了1到791136(201和3936的积)的整数. 要求用筛选法,把该数组里的质数找出并打印出来,打印格式为5个质数一行,数字间用空格隔开。

筛选法具体做法是:先把N个自然数按次序排列起来。1不是质数,也不是合数,要划去。第二个数2是质数留下来,而把2后面所有能被2整除的数都划去。2后面第一个没划去的数是3,把3留下,再把3后面所有能被3整除的数都划去。3后面第一个没划去的数是5,把5留下,再把5后面所有能被5整除的数都划去。这样一直做下去,就会把不超过N的全部合数都筛掉,留下的就是不超过N的全部质数。

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

6-1 输出月份英文名

1.设计思路(6分)
(1)主要描述题目算法(2分)。
第一步:判断是否为合法月份。
第二步:字符数组储存相应月份英文。
第三步:在循环内输出指定月份。
(2)流程图(4分)


2.实验代码(2分)

#include <stdio.h>

char *getmonth( int n );

int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}

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

3.本题调试过程碰到问题及解决办法(4分)
未遇到问题。

6-2 查找星期

1.设计思路(6分)
(1)主要描述题目算法(2分)。
第一步:在字符数组储存相应星期英文。
第二步:在循环内判断返回相应星期号。
第三步:无相应星期返回-1。
2.实验代码(2分)

#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 )
{
	char *data[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	int a;
	for(a = 0;a < 7;a = a + 1)
	{
		if(strcmp(s,*(data + a)) == 0)
		{
			return a;
		}
	}
	return -1;
}

3.本题调试过程碰到问题及解决办法(4分)
未遇到问题。

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

1.设计思路(6分)
(1)主要描述题目算法(2分)。
第一步:记录初始字符串长度。
第二步:在循环内判断较长字符串。
第三步:记录长度值返回。
2.实验代码(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,num = strlen(s[0]);
	for(a = 0;a < n;a = a + 1)
	{
		if(strlen(*(s + a)) > num)
		{
			num = strlen(*(s + a));
		}
	}
	return num;
}

3.本题调试过程碰到问题及解决办法(4分)
未遇到问题。

编程题

1.设计思路
(1)主要描述题目算法。
第一步:建立二维指针。
第二步:动态申请空间。
第三步:填充二维数组。
第四步:在循环中判断能够取余当前数字的数组元素,将非当前数字的符合条件的数组元素设为-1。
第五步:当前数字递增。
第六步:在循环中输出非-1的数组元素,每五次换一次行。
第七步:释放内存。
实验代码

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int **n;
	int a,b,c,d,e,f,g;
	a = 201;
	b = 3960;
	n = (int **)malloc(sizeof(int *) * a);
	for(c = 0;c < a;c = c + 1)
	{
		n[c] = (int *)malloc(sizeof(int) * b);
	}
	for(c = 0,e = 0;c < a;c = c + 1)
	{
		for(d = 0;d < b;d = d + 1)
		{
			e = e + 1;
			n[c][d] = e;
		}
	}
	for(c = 0,e = 2;c < a;c = c + 1)
	{
		for(d = 0;d < b;d = d + 1)
		{
			if(n[c][d] != -1)
			{
				if(n[c][d] == 1)
				{
					n[c][d] = -1;
				}
				else if((n[c][d] % e) == 0)
				{
					for(f = 0;f < a;f = f + 1)
					{
						for(g = 0;g < b;g = g + 1)
						{
							if(n[f][g] != -1)
							{
								if((n[f][g] != e) && ((n[f][g] % e) == 0))
								{
									n[f][g] = -1;
								}
							}
						}
					}
					e = e + 1;
				}
			}else
			{
				e = e + 1;
			}
		}
	}
	for(f = 0,e = 0,c = 0;f < a;f = f + 1)
	{
		for(g = 0;g < b;g = g + 1)
		{
			if(n[f][g] > 0)
			{
				if(((e % 5) == 0)&&(e != 0))
				{
					printf("\n");
				}
				printf("%d ",n[f][g]);				
				e = e + 1;
			}
		}
	}
	for(c = 0;c < a;c = c + 1)
	{
		free(n[c]);
	}
	free(n);
	return 0;
}

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

6-1 奇数值结点链表

1.设计思路(6分)
(1)主要描述题目算法(2分)。
第一步:创建几个初始化链表。
第二步:输入第一个值。
第三步:在循环内输入值赋值并记录链表首地址,输入-1退出循环,返回首地址。
第四步:创建几个初始化链表。
第五步:在循环内判断链表内值奇偶,记录首地址并标记不再更改首地址。
第六步:在循环内判断链表内值奇偶并分别创建奇偶链表。
第七步:在循环内判断同时存在奇偶链时尾项为空。
第八步:赋值偶数链首地址。
第九步:返回奇数链首地址。
(2)流程图(4分)




2.实验代码(2分)

#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 *head,*p,*t;
	int n = 0;
	head = NULL;
	p = NULL;
	t = NULL;
	scanf("%d",&n);
	while(n != -1)
	{
		p = (struct ListNode *)malloc(sizeof(struct ListNode));
		p->data = n;
		p->next = NULL;
		if(head == NULL)
		{
			head = p;
			t = p;
		}else
		{
			t->next = p;
			t = p;
		}
		scanf("%d",&n);
	}
	return head;
}
struct ListNode *getodd( struct ListNode **L )
{
	struct ListNode *p,*t,*h1,*h2;
	p = NULL;
	t = NULL;
	h1 = NULL;
	h2 = NULL;
	int f1 = 0,f2 = 0;
	while(*L != NULL)
	{
		if(((*L)->data % 2) != 0)
		{
			if(f1 == 0)
			{
				p = *L;
				h1 = p;
				*L = (*L)->next;
				f1 = 1;
			}else
			{
				p->next = *L;
				p = *L;
				*L = (*L)->next;
			}
		}else
		{
			if(f2 == 0)
			{
				t = *L;
				h2 = t;
				*L = (*L)->next;
				f2 = 1;
			}else
			{
				t->next = *L;
				t = *L;
				*L = (*L)->next;
			}
		}
		if((f1 == 1) && (f2 == 1))
		{
			t->next = NULL;
			p->next = NULL;
		}
	}
	*L = h2;
	return h1;
}

3.本题调试过程碰到问题及解决办法(4分)
未遇到问题。

6-2 学生成绩链表处理

1.设计思路(6分)
(1)主要描述题目算法(2分)。
第一步:创建几个初始化链表。
第二步:输入头链表值。
第三步:在循环内输入值赋值并记录链表首地址,输入0退出循环,返回首地址。
第四步:创建几个初始化链表。
第五步:在循环内判断符合成绩的链表,记录首地址并标记不再改变首地址。
第六步:在循环内判断符合成绩的链表,并形成符合数值的链表。
第七步:在循环内判断首地址已被标记,链表尾地址为空。
第八步:返回首地址
2.实验代码(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;
}

#include <string.h>
struct stud_node *createlist()
{
	struct stud_node *head,*p,*t;
	int num,score;
	char name[20];
	head = NULL;
	p = NULL;
	scanf("%d %s %d",&num,name,&score);
	while(num != 0)
	{
		p = (struct stud_node *)malloc(sizeof(struct stud_node));
		p->num = num;
		strcpy(p->name,name);
		p->score = score;
		p->next = NULL;
		if(head == NULL)
		{
			head = p;
			t = p;
		}else
		{
			t->next = p;
			t = p;
		}
		scanf("%d",&num);
		if(num == 0)
		{
			break;
		}else
		{
			scanf(" %s %d",name,&score);
		}
	}
	return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
	struct stud_node *p,*t,*h;
	p = head;
	t = NULL;
	h = NULL;
	int f;
	f = 0;
	while(p != NULL)
	{
		if(f == 1)
		{
			t->next = NULL;
		}
		if(p->score >= min_score)
		{
			if(f == 0)
			{
				t = p;
				h = t;
				f = 1;
			}else
			{
				t->next = p;
				t = p;
			}
		}
		p = p->next;
	}
	return h;
}

3.本题调试过程碰到问题及解决办法(4分)
未遇到问题。

6-3 链表拼接

1.设计思路(6分)
(1)主要描述题目算法(2分)。
第一步:创建几个初始化链表,记录两条链表的首地址。
第二步:在循环中记录其中一条链表的尾地址。
第三步:在循环中将其中一条链表的首地址赋给另一条链表的尾地址,并记录总长度。
第四步:建立结构数组,在循环内储存每个链表元素的地址和数值。
第五步:冒泡排序更改数组顺序为升序。
第六步:在循环中顺次建立一条链表,记录首地址。
第七步:返回首地址。
2.实验代码(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 *p,*t,*h,*head;
	int f,g,n1,n2,m;
	int *a;
	p = list1;
	t = list2;
	h = NULL;
	head = NULL;
	while(p != NULL)
	{
		if(head == NULL)
		{
			head = p;
		}
		if(p->next == NULL)
		{
			p->next = t;
			break;
		}
		p = p->next;
	}
	h = head;
	p = NULL;
	t = NULL;
	n1 = head->data;
	n2 = 0;
	while(head != NULL)
	{
		if(n1 > head->data)
		{
			n1 = head->data;
		}
		head = head->next;
		n2 = n2 + 1;
	}
	head = h;
	a = (int *)calloc(n2,sizeof(int));
	struct ListNode *b[n2];
	f = 0;
	while(head != NULL)
	{
		a[f] = head->data;
		f = f + 1;
		head = head->next;
	}
	f = 0;
	head = h;
	t = head;
	while(f < n2)
	{
		b[f] = t;
		t = head->next;
		head->next = NULL;
		head = t;
		f = f + 1;
	}
	head = h;
	f = 0;
	t = NULL;
	for(f = 0;f < (n2 - 1);f = f + 1)
	{
		for(g = 0;g < (n2 - f - 1);g = g + 1)
		{
			if((*(b + g))->data > (*(b + g + 1))->data)
			{
				t = *(b + g);
				*(b + g) = *(b + g + 1);
				*(b + g + 1) = t;
			}
		}
	}
	f = 0;
	p = NULL;
	t = NULL;
	while(f < n2)
	{
		if(f == 1)
		{
			p->next = NULL;
		}
		if(f == 0)
		{
			p = b[f];
			t = p;
		}else
		{
			p->next = b[f];
			p = b[f];
		}
		f = f + 1;
	}
	return t;
}

3.本题调试过程碰到问题及解决办法(4分)
错误信息:编译出错。
错误原因:数组变量指针取值的正确形式。
改正方法:多次试验改正。

提交列表

要求三、学习总结和进度(15分)
1、总结两周里所学的知识点,回答下列问题?(用自己的话表达出你的理解,网上复制粘贴没有分数)(5分)
(1)如何理解指针数组,它与指针、数组有何关系?为何可以用二级指针对指针数组进行操作?
指针数组为存放指针的数组,数组中的每一个指针指向相应地址。二级指针存放着指针数组首地址的指针,可以通过**p控制指针数组中的元素。

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

#include <stdio.h>

char *getmonth( int n );

int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}

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

(3)用指针数组处理多个字符串有何优势?可以直接输入多个字符串给未初始化的指针数组吗?为什么?
不必限制字符串长度相同。不行,未初始化的指针数组存放的地址不定,可能会报错。

2、将PTA作业的源代码使用git提交到托管平台上,要求给出上传成功截图和你的git地址。(3分)
1.git地址
https://git.coding.net/z732511533/ZYS.git


3、点评3个同学的本周作业(在作业中给出被点评同学博客的链接),并邀请3名同学点评你的作业,无点评作业(你的作业未被3人点评)/或者没有回复同学或老师的点评都倒扣该题分数。(4分)
http://www.cnblogs.com/2719610441qqcom/p/8762037.html
http://www.cnblogs.com/jj990519/p/8763063.html
http://www.cnblogs.com/caobaiqiang/p/8810067.html
4、请用表格和折线图呈现你本周(4/9 8:00~4/23 8:00)的代码行数和所用时间、博客字数和所用时间(3分)

时间 代码行数 时间1(min) 博客字数 时间2(min)
4.09 43 27 814 42
4.10 0 0 0 0
4.11 71 135 0 0
4.12 0 0 0 0
4.13 0 0 0 0
4.14 0 0 0 0
4.15 0 0 0 0
4.16 74 120 0 0
4.17 67 79 0 0
4.18 94 120 0 0
4.19 0 0 0 0
4.20 379 300 0 0
4.21 343 180 969 85
4.22 0 0 648 65
4.23 0 0 0 0

posted @ 2018-04-22 21:57  左右羽  阅读(336)  评论(6编辑  收藏  举报