這個作業屬于那個課程 | C語言程序設計II | |
這個作業要求在哪里 | https://pintia.cn/problem-sets/1127748174659035136/problems/1127749414029729792 | |
我在這個課程的目標是 | 更好的學習函數 | |
這個作業在那個具體方面幫助我實現目 | 鍛煉了我的編程能力 | |
參考文獻 |
|
?
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 20int 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
實驗代碼:
int?max_len(?char?*s[],?int?n?)
{
????int?i,max=0;
????for(i=0;i<n;i++)
????{
????????if(strlen(s[max])<strlen(s[i]))
????????{
????????????max=i;
????????}
????}
????return?strlen(s[max]);
}?
{
????int?i,max=0;
????for(i=0;i<n;i++)
????{
????????if(strlen(s[max])<strlen(s[i]))
????????{
????????????max=i;
????????}
????}
????return?strlen(s[max]);
}?
正確截圖:
流程圖:
?
?
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\n", countcs(head));return 0;
}/* 你的代碼將被嵌在這里 */
輸入樣例:
1021202
2022310
8102134
1030912
3110203
4021205
#
輸出樣例:
3
實驗代碼:
鄧新龍?2019/5/17?18:16:14
int?countcs(?struct?ListNode?*head?)
{
????struct?ListNode?*p=head;
????int?count=0;
????for(p=head;p!=NULL;p=p->next)
????{
????????if(p->code[1]=='0'&&p->code[2]=='2')
????????{
????????????count++;
????????}
????}
????return?count;
}?
正確截圖:


流程圖:

?
6-3?刪除單鏈表偶數節點?(20?分)
本題要求實現兩個函數,分別將讀入的數據存儲為單鏈表、將鏈表中偶數值的結點刪除。鏈表結點定義如下:
struct ListNode {int data;struct ListNode *next;
};
函數接口定義:
struct ListNode *createlist();
struct ListNode *deleteeven( struct ListNode *head );
函數createlist
從標準輸入讀入一系列正整數,按照讀入順序建立單鏈表。當讀到?時表示輸入結束,函數應返回指向單鏈表頭結點的指針。
函數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("\n");
}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
實驗代碼:
struct?ListNode?*createlist()
{
????int?x;
????struct?ListNode?*head,*tail,*p;
????head=(struct?ListNode*)malloc(sizeof(struct?ListNode));
????head->next=NULL;
????tail=head;
????while(1)
????{
????????p=(struct?ListNode*)malloc(sizeof(struct?ListNode));
????????scanf("%d",&x);
????????p->next=NULL;
????????if(x==-1)
????????????break;
????????p->data=x;
????????p->next=NULL;
????????tail->next=p;
????????tail=p;
????}
????return?head;
}
struct?ListNode?*deleteeven(?struct?ListNode?*head?)
{
????struct?ListNode?*p1,*p2;
????int?flag;
????p1=head;
????p2=p1->next;
????while(p1->next)
????{
????????flag=0;
????????if(p2->data%2==0)
????????{
????????????p1->next=p2->next;
????????????p2=p2->next;
????????????flag=1;
????????}
????????if(flag==0)
????????{
????????????p1=p1->next;
????????????p2=p1->next;
????????}
????}
????return?head->next;
}
{
????int?x;
????struct?ListNode?*head,*tail,*p;
????head=(struct?ListNode*)malloc(sizeof(struct?ListNode));
????head->next=NULL;
????tail=head;
????while(1)
????{
????????p=(struct?ListNode*)malloc(sizeof(struct?ListNode));
????????scanf("%d",&x);
????????p->next=NULL;
????????if(x==-1)
????????????break;
????????p->data=x;
????????p->next=NULL;
????????tail->next=p;
????????tail=p;
????}
????return?head;
}
struct?ListNode?*deleteeven(?struct?ListNode?*head?)
{
????struct?ListNode?*p1,*p2;
????int?flag;
????p1=head;
????p2=p1->next;
????while(p1->next)
????{
????????flag=0;
????????if(p2->data%2==0)
????????{
????????????p1->next=p2->next;
????????????p2=p2->next;
????????????flag=1;
????????}
????????if(flag==0)
????????{
????????????p1=p1->next;
????????????p2=p1->next;
????????}
????}
????return?head->next;
}
正確截圖:

學習進度條:
周/日 | 這周所花時間 | 代碼行 | 學到的知識點 |
---|---|---|---|
5/11-5/17 | 十小時 | 700行 | 學習運用二級指針 |
?