第十二周作业,
| 這個作業要求在哪里 | https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/3234 | |
| 我在這個課程的目標是 | 熟練二級指針的操作,對二級指針會一定的使用 | |
| 這個作業在哪個具體方面幫助我實現目標 | 初步了解了二級指針的作用,以及在何種情況下使用二級指針 | |
| 參考文獻 | C語言程序設計II |
正文
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輸出樣例:
61.代碼
int max_len( char *s[], int n ) {int k;int max=0;for (k=0;k<n;k++){int len=strlen(s[k]);if (strlen(s[max])<len)max=k;}return strlen(s[max]); }實驗截圖:
實驗思路:
出現問題以及解決方法:在自己編寫這道題目的時候,一直顯示超時,也就是說自己的代碼過于長,達不到條件,于是換了網上的辦法,比自己的簡單很多;
6-3 刪除單鏈表偶數節點 (20 分)
本題要求實現兩個函數,分別將讀入的數據存儲為單鏈表、將鏈表中偶數值的結點刪除。鏈表結點定義如下:
struct ListNode {int data;struct ListNode *next; };函數接口定義:
struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head );函數createlist從標準輸入讀入一系列正整數,按照讀入順序建立單鏈表。當讀到?1時表示輸入結束,函數應返回指向單鏈表頭結點的指針。
函數deleteeven將單鏈表head中偶數值的結點刪除,返回結果鏈表的頭指針。
裁判測試程序樣例:
輸入樣例:
1 2 2 3 4 5 6 7 -1輸出樣例:
1 3 5 71.代碼:
int countcs(struct ListNode*head) { int num=0; struct ListNode *p=head; while(p!=NULL){if (p->code[1]=='0'&&p->code[2]=='2'){num++;}p=p->next;} return num; }2.思路流程圖
3.本題遇到的問題及解決辦法
問題:本題自己編寫一直只有部分之正確的,然后別的地方不管怎么改,都顯示不正確,
解決辦法:主動進行了調試,將原本的一小部分(原本中間一部分還有一個if的循環)刪去之后顯示正確
實驗截圖:
6-3 刪除單鏈表偶數節點 (20 分)
本題要求實現兩個函數,分別將讀入的數據存儲為單鏈表、將鏈表中偶數值的結點刪除。鏈表結點定義如下:
struct ListNode {int data;struct ListNode *next; };函數接口定義:
struct ListNode *createlist(); struct ListNode *deleteeven( struct ListNode *head );函數createlist從標準輸入讀入一系列正整數,按照讀入順序建立單鏈表。當讀到?1時表示輸入結束,函數應返回指向單鏈表頭結點的指針。
函數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 71.代碼
struct ListNode *createlist() {struct ListNode *head,*p;head=(struct ListNode*)malloc(sizeof(struct ListNode));p=head;head->next=NULL;int num;while(scanf("%d",&num)!=EOF&&num!=-1){ p->next=(struct ListNode*)malloc(sizeof(struct ListNode));p=p->next;p->data=num;p->next=NULL;}head=head->next;return(head); } struct ListNode*deleteeven(struct ListNode*head) {struct ListNode*p,*q;if(head==NULL){return NULL;} p=head;q=p->next;while(q!=NULL){if(q->data%2==0){p->next=q->next;free(q);q=p->next;}else{p=p->next;q=p->next;}}if(head->data%2==0) {head=head->next; } return(head); }2.思路流程圖:
3.本題遇到的問題及解決辦法
問題:編譯錯誤
解決辦法:換了好多遍思路了
4.運行結果截圖:
| 3.4-3.11 | 三天 | 45 |
| 3.12-3.16 | 7小時 | 76行 |
| 3.17-3.22 | 五天 | 278 |
| 3.22-3.29 | 七天 | 記不清了 |
| 3.31-4.5 | 這肯定是好久了 | 學了一點的使用,不過比較懵 |
| 4.7-4.12 | 334 | 對數組的理解清晰了一點,通過借鑒一班助教的代碼,理解更加清楚 |
| 4.22-4.26 | 四天 | |
| 5.5-5.9 | 一天 | 34 |
轉載于:https://www.cnblogs.com/langz594212/p/10883405.html
總結
- 上一篇: 第13章 C#中的多线程
- 下一篇: clientHeight,offsetH