這個作業屬于那個課程 | c語言 |
這個作業要求在哪里 | https://edu.cnblogs.com/campus/zswxy/computer-scienceclass4-2018/homework/3236 |
我在這個課程的目標是 | 學習掌握單向鏈表,掌握二級指針的概念,以及指針數組 |
這個作業在哪個具體方面幫助我實現目標 | 前面3道題目 |
參考文獻 | c語言程序設計,c primer plus ? ? |
本題要求實現一個函數,用于計算有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 max=0;//假設max為s[0] int i,j;for(i=0;i<n;i++){if(strlen(s[max])<strlen(s[i])){max=i; }} return strlen(s[max]);
}
2.設計思路
??
?
3.本題調試過程碰到的問題及解決辦法?
本題目沒有遇到問題
4.運行結果截圖?
?
本題要求實現一個函數,統計學生學號鏈表中專業為計算機的學生人數。鏈表結點定義如下:
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
代碼
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.本題調試過程碰到的問題及解決辦法?
1.答案錯誤,if語句中的條件寫錯了,導致結果錯誤。
4.運行結果截圖?
?
?
本題要求實現兩個函數,分別將讀入的數據存儲為單鏈表、將鏈表中偶數值的結點刪除。鏈表結點定義如下
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()
{struct ListNode *head, *p1, *tail;int num;head=(struct ListNode *)malloc(sizeof(struct ListNode));p1=tail=(struct ListNode *)malloc(sizeof(struct ListNode));p1->next=tail->next=head->next=NULL;while(1){scanf("%d",&num);if(num!=-1){p1->data=num;if(head->next==NULL){head->next=p1;}else {tail->next=p1;tail=p1;}p1=(struct ListNode *)malloc(sizeof(struct ListNode));p1->next=NULL;}else break;}return head;
}struct ListNode *deleteeven(struct ListNode *head)
{struct ListNode *num,*p;p=head;num=head->next;while(num!=NULL){if(num->data%2==0){p->next=num->next;}elsep=p->next;num=num->next;}return head->next;
}
2.設計思路
?
3.本題調試過程碰到的問題及解決辦法?
這次作業的鏈表在書上看的有些不一樣,剛剛做的時候出現了很多次的答案錯誤,是因為沒弄清楚到底是哪個結點解除,結點鏈接不對導致刪除的過多,
或者是全部都被刪除了。
然后出現最多的是字符指向結點的問題也出現了混亂,慢慢摸索,還是懂了許多。
4.運行結果截圖?
在國際象棋中,皇后是最厲害的棋子,可以橫走、直走,還可以斜走。棋手馬克斯·貝瑟爾 1848 年提出著名的八皇后問題:即在 8 × 8 的棋盤上擺放八個皇后,使其不能互相攻擊 —— 即任意兩個皇后都不能處于同一行、同一列或同一條斜線上。
現在我們把棋盤擴展到 n × n 的棋盤上擺放 n 個皇后,請問該怎么擺?請編寫程序,輸入正整數 n,輸出全部擺法(棋盤格子空白處顯示句點“.”,皇后處顯示字母“Q”,每兩格之間空一格)。
輸入格式
正整數 n (0 < n ≤ 12)
輸出格式
若問題有解,則輸出全部擺法(兩種擺法之間空一行),否則輸出 None。
要求:試探的順序逐行從左往右的順序進行,請參看輸出樣例2。
輸入樣例1
3
輸出樣例1
None
輸入樣例2
6
輸出樣例2
. Q . . . .
. . . Q . .
. . . . . Q
Q . . . . .
. . Q . . .
. . . . Q .. . Q . . .
. . . . . Q
. Q . . . .
. . . . Q .
Q . . . . .
. . . Q . .. . . Q . .
Q . . . . .
. . . . Q .
. Q . . . .
. . . . . Q
. . Q . . .. . . . Q .
. . Q . . .
Q . . . . .
. . . . . Q
. . . Q . .
. Q . . . .
代碼
#include<stdio.h>void queen(int i,int j); //遞歸函數,
int check(int i,int j); //判斷是否會遇到其他皇后 的函數 char chess[13][13]; //最大棋盤
int a,b,n,sum=0; //sum記錄能擺放的方法次數 int main(void){scanf("%d",&n);queen(0,0);if(sum==0){printf("None"); }return 0;}void queen(int i,int j){if(j>=n){return ; }if(check(i,j)==1){ //如果能放chess[i][j]='Q'; //放皇后if(i==n-1){ //如果是最后一行,記錄情況sum++; //記錄符合的次數if(sum!=1) //如果有新方法,就在新方法前加一個換行,使得最后一種方法后面沒有換行printf("\n");for(a=0;a<n;a++){ for(b=0;b<n;b++){if(chess[a][b]!='Q'){if(b!=n-1)printf(". ");elseprintf(".");}if(chess[a][b]=='Q'){if(b!=n-1)printf("Q ");elseprintf("Q");}}printf("\n");}}else{queen(i+1,0); //不是最后一行就分析下一行}}chess[i][j]='.'; //如果此位置不能放,就置空(0),判斷旁邊的格子。//如果此位置能放,走到這里就意味著上面的代碼全部執行了,把皇后拿走(置零),再討論其他情況,拿旁邊位置試探。queen(i,j+1);
}int check(int i,int j){int k;for(k=0;k<n;k++){if(chess[i][k]=='Q')return 0; //0=不能放}for(k=0;k<n;k++){if(chess[k][j]=='Q')return 0; }for(k=-n;k<=n;k++){ //兩對角線if(i+k>=0&&i+k<n&&j+k>=0&&j+k<n)//從左上到右下對角線if(chess[i+k][j+k]=='Q') return 0;if(i-k>=0&&i-k<n&&j+k>=0&&j+k<n)//從左下到右上對角線if(chess[i-k][j+k]=='Q') return 0;}return 1;
}
2.設計思路
思路不對,所以我就沒有畫流程圖了
3.本題調試過程碰到的問題及解決辦法?
本題目做錯了,到現在為止還是沒有調試正確,部分正確和運行超時讓我十分苦惱
4.運行結果截圖?
?
?
?
學習感悟?
本周學習的鏈表實在有難度,結點的鏈接問題和刪除時結點的解除問題和方向問題。今天上課老師講的很清楚,感覺茅塞頓開。哈哈。