6-1 輸出月份英文名(15 分)
6-2 查找星期(15 分)
6-3 計算最長的字元串長度(15 分)
6-4 指定位置輸出字元串(20 分)
篩選法
6-1 奇數值結點連結清單(20 分)
6-2 學生成績連結清單處理(20 分)
6-3 連結清單拼接(20 分)
PTA第一部分
本題要求實作函數,可以傳回一個給定月份的英文名稱。
函數接口定義:
char *getmonth( int n );
函數
getmonth
應傳回存儲了
n
對應的月份英文名稱的字元串頭指針。如果傳入的參數
n
不是一個代表月份的數字,則傳回空指針NULL。
裁判測試程式樣例:
#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;
}
/* 你的代碼将被嵌在這裡 */
輸入樣例1:
5
輸出樣例1:
May
輸入樣例2:
15
輸出樣例2:
wrong input!
1.設計思路:
(1).題目算法描述
第一步:定義
*month[]
并初始化為各個月份
第二步:定義
*s
第三步:當n是月份時将對應的月份指派給s,否則将wrong input!指派給s
第四步:return s
(2).流程圖
暫略
2.實驗代碼
char *getmonth( int n )
{
char *month[13] = {"January","February","March","April","May","June","July","August","September","October","November","December","wrong input!"};
char *s;
if(n>=1 && n<=12)
{
s = month[n-1];
}else
{
s = month[12];
}
return s;
}
2.錯誤調試
無
答案正确

本題要求實作函數,可以根據下表查找到星期,傳回對應的序号。
序号 | 星期 |
Sunday | |
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
int getindex( char *s );
getindex
應傳回字元串
s
序号。如果傳入的參數
s
不是一個代表星期的字元串,則傳回-1。
#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;
}
/* 你的代碼将被嵌在這裡 */
Tuesday
2
today
wrong input!
*week[]
并初始化為各個星期
第二步:周遊week判斷是否與s相同
第三步:相同傳回對應下标,不相同是傳回-1
int getindex( char *s )
{
int i=0;
char *week[8] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
for(i=0;i<7;i++)
{
if(strcmp(s,week[i])==0)
{
break;
}
}
if(i<7){
return i;
}else{
return -1;
}
}

本題要求實作一個函數,用于計算有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\n", max_len(string, n));
return 0;
}
/* 你的代碼将被嵌在這裡 */
輸入樣例:
4
blue
yellow
red
green
輸出樣例:
6
第一步:定義i,max
第二步:for循環周遊s判斷每個字元串長度與max的關系将最大長度指派給max
第三步:傳回max
int max_len( char *s[], int n )
{
int i=0,max=0;
for(i=0;i<n;i++)
{
if(strlen(s[i])>max){
max = strlen(s[i]);
}
}
return max;
}
本題要求實作一個函數,對給定的一個字元串和兩個字元,列印出給定字元串中從與第一個字元比對的位置開始到與第二個字元比對的位置之間的所有字元。
char *match( char *s, char ch1, char ch2 );
match
應列印
s
中從
ch1
到
ch2
之間的所有字元,并且傳回
ch1
的位址。
#include <stdio.h>
#define MAXS 10
char *match( char *s, char ch1, char ch2 );
int main()
{
char str[MAXS], ch_start, ch_end, *p;
scanf("%s\n", str);
scanf("%c %c", &ch_start, &ch_end);
p = match(str, ch_start, ch_end);
printf("%s\n", p);
return 0;
}
/* 你的代碼将被嵌在這裡 */
program
r g
rog
rogram
program
z o
(空行)
(空行)
輸入樣例3:
program
g z
輸出樣例3:
gram
gram
第一步:定義整型變量i,j,len
第二步:定義指針變量并初始化為NULL
第三步:統計len的長度并将值賦給len
第四步:周遊s當找到與找到第一個與ch1相同的字元是讓p指向它
第五步:從第一個與ch1相同字元串之後繼續向下周遊,如果其後的字元串與ch2不相同則輸出此字元串繼續向下周遊,如果相同輸出此字元串并換行傳回p
第六步:當周遊全部完成後則換行傳回p
char *match( char *s, char ch1, char ch2 )
{
int i = 0, j = 0,len = 0;
char *p=NULL;
len = strlen(s);//統計字元串長度
for(i = 0;i < len;i++)//周遊s
{
if(*(s+i)==ch1){
p = &s[i];//讓p指向第一個與ch1相同的字元
for(j = i;j < len;j++)//從第一個相同的字元開始繼續向下周遊
{
if(s[j]!=ch2){
printf("%c",*(s+j));//第一個字元之後的字元如果與最後一個字元串不相同則輸出此字元
}
if(s[j] == ch2){
printf("%c\n",*(s+j));//當與第二個字元相同時則輸出此字元并換行後傳回p
return p;
}
}
printf("\n");
return p;
}
}
printf("\n");
return p;
}
錯誤提示1:段錯誤
改正方法:将指針p初始化為NULL
改正後的代碼在Cold::Blocks上每個示例均能運作出正确答案,但是在PTA上仍是部分正确
求大佬指教!!!
程式設計題
有一個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的全部質數。
參考教程:https://baike.baidu.com/item/篩選法/9393288?fr=aladdin#2_2(來源百度百科)
第一步:計算學号前三位和後四位的乘積并指派給n
第二步:定義大小為n的數組axb
第三步:周遊數組輸入1到n的值
第四步:從2開始周遊數組将所有合數和能被此合數整除的數全都指派為一
第五步:輸出值不是1的數
#include <stdio.h>
#include <math.h>
int main ()
{
long int n=0,i=0,j=0,x=0;
n = 201*3990;
int axb[n];
for(i=1;i<n;i++)
{
axb[i-1] = i;
}
for(i=1;i<=n;i++)
{
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0){
axb[i-1]=1;
}
}
}
for(i=0,x=0;i<n;i++)
{
if(axb[i]!=1){
printf("%d ",axb[i]);
x++;
if(x%5 == 0){
printf("\n");
}
}
}
return 0;
}
修改
經過課堂講解後發現自己的錯誤,沒有将存儲學号的數組寫成二維數組。
修改後的代碼如下
#include <stdio.h>
#define N 201
#define M 3990
int a[N][M];//全局變量
void del(int a[N][M],int x){
int i=0,j=0;
for(i=0;i<N;i++){
for(j=1;j<M;j++){
if(i==0 && j==0){
continue;
}
if(a[i][j]>x &&(a[i][j] % x== 0)){
a[i][j]=0;
}
}
}
}
int main ()
{
int i=0,j=0,num=1,count=0;
for(i=0;i<N;i++){
for(j=0;j<M;j++){
a[i][j] = num++;
}
}
a[0][0]=0;//去掉a[0][0]從第一個數開始
for(i=0;i<N;i++){
for(j=0;j<M;j++){
if(a[i][j] != 0){
del(a,a[i][j]);
}
}
}
for(i=0;i<N;i++){
for(j=0;j<M;j++){
if(a[i][j] != 0){
printf("%d ",a[i][j]);
count++;
if(count%5 == 0){
printf("\n");
}
}
}
}
return 0;
}
PTA第二部分
本題要求實作兩個函數,分别将讀入的資料存儲為單連結清單、将連結清單中奇數值的結點重新組成一個新的連結清單。連結清單結點定義如下:
struct ListNode {
int data;
ListNode *next;
};
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
readlist
從标準輸入讀入一系列正整數,按照讀入順序建立單連結清單。當讀到−1時表示輸入結束,函數應傳回指向單連結清單頭結點的指針。
getodd
将單連結清單L中奇數值的結點分離出來,重新組成一個新的連結清單。傳回指向新連結清單頭結點的指針,同時将L中存儲的位址改為删除了奇數值結點後的連結清單的頭結點位址(是以要傳入L的指針)。
#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;
}
/* 你的代碼将被嵌在這裡 */
1 2 2 3 4 5 6 7 -1
1 3 5 7
2 2 4 6
第一個部分:
第一步:定義num并初始化為0
第二步:定義結構指針head p tail并初始化
第三步:用動态記憶體配置設定為p申請空間;讀入num,當num不是-1時将其賦給p->data;如果head是空head=p否則tail->next=p
第四步:重複以上過程知道讀入的資料為-1
第五步:釋放p
第六步:return head
第二部分:
第一步:定義n并初始化為0
第二步:定義結構指針head,tail,q,p,p1,p2,ptr
第三步:當p->data為奇數時,用動态記憶體配置設定為q申請空間q->data = p->data;q->next = NULL;如果head為空則head=q否則tail->next=q然後将q的值賦給tail将p的值賦給ptr,p指向下一個節點,并釋放申請的空間
第四步:當p->data不是奇數時,如果p1為空p1=p否則p2->next =p; p2=p; p= p->next; n=1
第五步:判斷當n=1時p2->next為空
第六步:*L = p1
第七步:return head
struct ListNode *readlist(){
int num=0;
struct ListNode *head=NULL, *p=NULL, *tail=NULL;
p = (struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &num);
while(num != -1){
p->data = num;
p->next=NULL;
if(head == NULL){
head=p;
}
else{
tail->next=p;
}
tail=p;
p=(struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &num);
}
free(p);
return head;
}
struct ListNode *getodd(struct ListNode **L){
int n = 0;
struct ListNode *head=NULL, *tail=NULL, *q=NULL, *p=*L, *p1=NULL, *p2=NULL, *ptr=NULL;
while (p!=NULL)
{
if (p->data % 2 != 0)
{
q = (struct ListNode *) malloc(sizeof(struct ListNode));
q->data = p->data;
q->next = NULL;
if (head == NULL)
head = q;
else
tail->next = q;
tail = q;
ptr = p;
p = p->next;
free(ptr);
}
else
{
if (p1 == NULL){
p1 = p;
}
else{
p2->next = p;
}
p2 = p;
p = p->next;
n = 1;
}
}
if (n==1){
p2->next = NULL;
}
*L = p1;
return head;
}
錯誤提示:全是奇數全是偶數第一個是奇數的測試點答案錯誤
錯誤改正:第一次上完課後對相關連結清單的操作隻是知道大概的操作思路,并不清楚相關的代碼應該怎麼寫,對于相關插入、删除的寫法更是一頭霧水,在網上簡單的看來相關的資料但并不是十分了解。上了第二次課後了解的相對好了一些,重新寫了所有代碼,以上貼的是修改後的代碼
本題要求實作兩個函數,一個将輸入的學生成績組織成單向連結清單;另一個将成績低于某分數線的學生結點從連結清單中删除。
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
createlis
t利用
scanf
從輸入中擷取學生的資訊,将其組織成單向連結清單,并傳回連結清單頭指針。連結清單節點結構定義如下:
struct stud_node {
int num; /*學号*/
char name[20]; /*姓名*/
int score; /*成績*/
struct stud_node *next; /*指向下個結點的指針*/
};
輸入為若幹個學生的資訊(學号、姓名、成績),當輸入學号為0時結束。
deletelist
從以
head
為頭指針的連結清單中删除成績低于
min_score
的學生,并傳回結果連結清單的頭指針。
#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;
}
/* 你的代碼将被嵌在這裡 */
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
2 wang 80
4 zhao 85
第一部分:
第一步:定義結構指針head,tail,q并初始化
第二步:定義num并讀入num
第三步:當num不等于0時,動态記憶體配置設定為q申請空間;讀入q->name和q->score;當head為空的時候head=q否則tail->next=q
第四步:tail=q;繼續讀入num
第五步:當num等于0時停止讀入并return head
第一步:定義結構指針ptr1和ptr2
第二步:當head不為空并且head->score小于min->score時ptr2=head;head=head->next;并釋放ptr2的空間
第三步:當head為空時return NULL;
第四步:ptr1=head;ptr2=head->next
第五步:當ptr2不為空時 判斷如果ptr2->score小于min_score則ptr1->next = ptr2->next;并釋放ptr2的空間否則ptr1=ptr2然後ptr2=ptr1->next
第六步:return head;
struct stud_node *createlist()
{
struct stud_node *head, *tail, *q;
head = NULL;
tail = NULL;
int num;
scanf ("%d", &num);
while (num != 0)
{
q = (struct stud_node *)malloc (sizeof (struct stud_node));
scanf ("%s %d", q->name, &q->score);
q->num = num;
q->next = NULL;
if (head == NULL){
head = q;
}
else{
tail->next = q;
}
tail = q;
scanf ("%d", &num);
}
return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
struct stud_node *ptr1, *ptr2;
while (head != NULL && head->score < min_score)
{
ptr2 = head;
head = head->next;
free(ptr2);
}
if (head == NULL){
return NULL;
}
ptr1 = head;
ptr2 = head->next;
while (ptr2 != NULL)
{
if (ptr2->score < min_score) {
ptr1->next = ptr2->next;
free(ptr2);
}
else{
ptr1 = ptr2;
}
ptr2 = ptr1->next;
}
return head;
}
本題要求實作一個合并兩個有序連結清單的簡單函數。連結清單結點定義如下:
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);
其中
list1
和
list2
是使用者傳入的兩個按
data
升序連結的連結清單的頭指針;函數
mergelists
将兩個連結清單合并成一個按
data
升序連結的連結清單,并傳回結果連結清單的頭指針。
#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;
}
/* 你的代碼将被嵌在這裡 */
1 3 5 7 -1
2 4 6 -1
1 2 3 4 5 6 7
第一步:定義結構指針p,head,tail并初始化
第二步:定義整型數組list[100]和整型變量i,j,swap,count并初始化為0
第三步:當list1不為空時list[i]=list1->data;list1=list1->next;i++;count++;
第四步:當list2不為空時list[i]=list2->data;list2=list2->next;i++;count++;
第五步:對list進行排序
第六步:周遊list用動态記憶體配置設定為p申請空間p->data=list[i];p->next=NULL;判斷當head=NULL時head=p否則tail->next=p;然後tail=p;
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
struct ListNode *p=NULL,*head=NULL,*tail=NULL;
int list[100],i=0,j=0,swap=0,count=0;
while(list1!=NULL){
list[i]=list1->data;
list1=list1->next;
i++;
count++;
}
while(list2!=NULL){
list[i]=list2->data;
list2=list2->next;
i++;
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;
}
}
}
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).篩選法:學習了篩選法的相關思路和具體的程式實作方法
(3).連結清單:學習并了解了連結清單的相關知識,明确了連結清單的優點,掌握了連結清單的相關思路但是連結清單具體的代碼實作方法還有待加強練習
(4).結構:為了更好的學習連結清單在mooc複習了相關結構的内容
(5)二級指針:還是感覺用起來很混亂,不清晰
2.問題回答
(1)如何了解指針數組,它與指針、數組有何關系?為何可以用二級指針對指針數組進行操作?
指針數組:元素全都是指針的數組
數組的名稱可以代表數組首元素的位址,有了首元素的位址就可以用二級指針對數組的元素進行操作
(2)将C進階第三次PTA作業(1)任何一個題目改為使用二級指針對指針數組進行操作。
修改C進階第三次PTA作業(1)中的第一題
修改後的代碼:
char *getmonth( int n )
{
char *month[13] = {"January","February","March","April","May","June","July","August","September","October","November","December","wrong input!"};
char **s;
if(n>=1 && n<=12)
{
s = &month[n-1];
}
else
{
s = &month[12];
}
return *s;
}
(3)用指針數組處理多個字元串有何優勢?可以直接輸入多個字元串給未初始化的指針數組嗎?為什麼?
優勢:處理起來更加便捷
不可以,指針沒有初始化的話就不知道指向哪裡,有可能指的是不允許使用者更改的區域,這時候就會出現段錯誤
2.代碼托管
我的GitHub位址:https://github.com/XINJY/The-homework-of-C
上傳成功截圖:
3.作業點評
李伍壹:http://www.cnblogs.com/chenxidream/p/8778302.html
李新華:http://www.cnblogs.com/Lixinhua18/p/8810369.html
馬钰娟:http://www.cnblogs.com/dfgfds/p/8906883.html