此次做業屬於哪一個課程 | C語言程序設計 |
---|---|
此次做業要求在哪 | 要求 |
我在這個課程的目標是 | 學會運用字符排序 |
這個做業在那個具體方面幫助我實現目標 | 字符串的計數和排序 |
參考文獻 | c語言教科書和大佬博客 |
6-1 計算最長的字符串長度 (15 分)
本題要求實現一個函數,用於計算有n個元素的指針數組s中最長的字符串的長度。c++
函數接口定義:數組
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
int max_len( char *s[], int n ) { int i,max=0,len; for(i=0;i<n;i++){ len=strlen(s[i]); if(max<len) max=len;; } return max; }
截圖
3d
6-2 統計專業人數 (15 分)
本題要求實現一個函數,統計學生學號鏈表中專業爲計算機的學生人數。鏈表結點定義以下:指針
struct ListNode { char code[8]; struct ListNode *next; };
這裏學生的學號共7位數字,其中第二、3位是專業編號。計算機專業的編號爲02。code
函數接口定義:blog
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
int countcs( struct ListNode *head ) { struct ListNode *ptr; int c=0; if(head==NULL) return 0; else{ for(ptr=head;ptr!=NULL;ptr=ptr->next){ if((ptr->code[1])=='0'&&(ptr->code[2])=='2') c++; } } return c; }
截圖
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 7
不會
截圖
周/日期 | 這周所花的時間 | 代碼行 | 學到的知識點簡介 | 目前比較困惑的問題 |
---|---|---|---|---|
3/9-3/15 | 3d | 100 | 文件創建及其文件的利用 | 無 |
3/15-3/18 | 4d | 150 | 二位數組和暴力解法 | 最大子數組最優的解法是什麼,如何下降時間複雜度 |
3/18-3/19 | 5d | 250 | 選擇排序法 | 我沒有徹底消化選擇排序法的思路 |
3/25-3/29 | 5d | 250 | 冒泡排序不會 | 指針也不會 |
3/29-4/5 | 5d | 300 | 指針的具體用法 | 函數和指針的結合 |
4/5-4/12 | 6d | 350 | 字符數組和指針的結合 | 綜合運用的方法 |
4/12-419 | 6d | 400 | 指針函數和數組函數 | 拓展函數 |
5/5-5/10 | 6d | 200 | 宏定義 | 基本知識 |
5/11-5/17 | 7d | 500 | 指針數組 | 數組的進一步學習 |