C程序設計第一章函數編程題_第1頁
C程序設計第一章函數編程題_第2頁
C程序設計第一章函數編程題_第3頁
C程序設計第一章函數編程題_第4頁
C程序設計第一章函數編程題_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優質文檔-傾情為你奉上6-1 工作備忘錄的生成(鏈表) (10 分)每天都要處理很多事務,為了更好地安排工作,希望在每天開始工作前,根據工作記錄,生成工作備忘錄。首先輸入工作記錄數(大于0的一個整數),再逐條輸入各條工作記錄,每條工作記錄包括:工作名,開始時間,結束時間。假設每項工作的開始時間均小于它的結束時間,并且各項工作的開始時間互不相同。我們的工作是需要把這些工作記錄按開始時間排序并輸出,在輸出時,如果某項工作與若干項工作沖突(在做該項工作時,需要同時做其它工作),則在該工作名前加'*'。函數接口定義:Node* add(Node *, Node *);void di

2、splay(Node *);裁判測試程序樣例:#include<iostream>#include <string>using namespace std;struct Node string name; int start; int end; Node *next;Node* add(Node *, Node *);void display(Node *);bool check(Node *head) if(head=NULL | head->next=NULL) return true; Node *p=head->next; if(head->s

3、tart > p->start) return false; return check(p);int main() Node *head=NULL, *p; int i, repeat; cin>>repeat; for(i=0;i<repeat;i+) p = new Node; cin>>p->name>>p->start>>p->end; p->next=NULL; head = add(head, p); if(!check(head) cout<<"ERROR"&

4、lt;<endl; display(head); return 0;/* 請在這里填寫答案 */輸入樣例:4aaa 19 20ccc 169 200ddd 153 170bbb 20 111輸出樣例:aaa 19 20bbb 20 111*ddd 153 170*ccc 169 200Node* add(Node *head, Node *p) /把節點p插入到鏈表,按照任務的起始時間升序排列 Node *q = head, *s = head; while(q != NULL)&&(q->start < p->start) /當前的q節點不是末尾,并且

5、q節點的起始時間早于p節點的起始時間 s = q; /s始終在q的前面 q = q->next; /直到當前節點NULL或者q的起始時間晚于p的起始時間 if(q = head) /p應該接在鏈表的開始位置,即成為新的表頭 p->next = head;head = p; elsep->next = q;s->next = p; return head; void display(Node *head)Node *p = head, *q;bool conflict; /用于標識是否有沖突 while(p != NULL) /當前p節點不空,是有效任務 conflict

6、 = false;q = head;while(!conflict && q != p) /檢查p的開始時間是否小于p之前的任務的結束時間 if(q->end > p->start) /p之前的任務的結束時間如果大于p的開始時間則沖突 conflict = true;break;q = q->next;q = p->next; while(!conflict && q != NULL) /檢查p的結束時間是否大于p之后的任務的開始時間if(p->end > q->start) /p之后的任務的開始時間如果大于p的結

7、束時間則沖突 conflict = true;break;q = q->next;if(conflict) /當前任務與其它的任務時間沖突cout << "*"cout << p->name << " " << p->start << " " << p->end << endl;p = p->next;6-2 函數調用 (10 分)編寫三個函數:求兩個整數的最大值、最小值、和。分別用這三個函數作為實參,再寫一個計算函數co

8、mpute,對兩個整數進行各種計算。其中一個形參為指向具體算法函數的指針。函數接口定義:int max(int a, int b);int min(int a, int b);int sum(int a, int b);int compute(int a, int b, int(*func)(int, int);裁判測試程序樣例:在這里給出函數被調用進行測試的例子。例如:#include <iostream>using namespace std;int max(int a, int b);int min(int a, int b);int sum(int a, int b);in

9、t compute(int a, int b, int(*func)(int, int);int main()int a, b, res; cin >> a >> b;res = compute(a, b, & max);cout << "Max of " << a << " and " << b << " is " << res << endl;res = compute(a, b, & min);cout &

10、lt;< "Min of " << a << " and " << b << " is " << res << endl;res = compute(a, b, & sum);cout << "Sum of " << a << " and " << b << " is " << res << endl;ret

11、urn 0;/* 請在這里填寫答案 */輸入樣例:3 5輸出樣例:Max of 3 and 5 is 5Min of 3 and 5 is 3Sum of 3 and 5 is 8int max(int a, int b) if(a>b) return a; else return b; int min(int a, int b) if(a<b) return a; else return b; int sum(int a, int b) return a+b; int compute(int a, int b, int(*func)(int, int) return (*func

12、)(a,b);6-3 函數指針(理科實驗班) (7 分)夢山高中現需要將某普通班的最優秀學生調整入理科實驗班。為此,將從兩個方面考察學生,一是數學和英語兩門課的總分;另一個是所有四門課的總分。分別找出兩科總分和全科總分的第一名,并從中決定調整人選。輸入將首先輸入學生數n, (n為不超過80的正整數);接下來依次輸入各位學生的學號,數學、英語、語文、理科綜合成績。學號及四科成績均為不超過的正整數。輸出時:第一行輸出兩科總分第一的學號,第二行輸出四科總分第一的學號。約定在兩位學生成績相同時,優先選擇學號較小的學生;各位學生的學號均不相同。裁判測試程序樣例:#inclu

13、de <iostream>using namespace std;constint N=80;struct Studentintnum;int score4;/* 請在這里填寫答案 */int main()inti, j, k, n;bool s2(const Student &, const Student &);bool s4(const Student &, const Student &); Student stN;cin>>n;for(i=0;i<n;i+)cin>>sti.num;for(j=0;j<4

14、;j+) cin>>sti.scorej; cout<<select(st, n, s2)<<endl;cout<<select(st, n, s4)<<endl;輸入樣例:36 148 150 120 2525 148 150 117 2607 145 148 128 287輸出樣例:57bool s2(const Student &s1, const Student &s2) /比較s1和s2兩位學生的數學+英語大小,如果s2的大則返回true if(s1.score0 + s1.score1 < s2.sc

15、ore0 + s2.score1) return true;if(s1.score0 + s1.score1 = s2.score0 + s2.score1 && s1.num > s2.num) return true;return false;bool s4(const Student &s1, const Student &s2)/比較s1和s2兩位學生的總分大小,如果s2的大則返回true if(s1.score0 + s1.score1 + s1.score2 + s1.score3 < s2.score0 + s2.score1 + s2

16、.score2 + s2.score3) return true;if(s1.score0 + s1.score1 + s1.score2 + s1.score3 = s2.score0 + s2.score1 + s2.score2 + s2.score3 && s1.num > s2.num) return true;return false;int select(Student s, int n, bool (*p)(const Student &s1, const Student &s2)int maxIndex = 0;for(int i = 1

17、; i < n; i+)if(*p)(smaxIndex, si) maxIndex = i;return smaxIndex.num;6-4 二維數組(海綿城市) (7 分)根據海綿城市建設指揮部要求,怡山小學將對校內道路進行改造,鋪設透水磚。這樣有些道路將不能通行。為了不妨礙假期少先隊的校內活動安排,大隊宣傳委員小黃需要知道一些關鍵的活動地點是否可以到達。已知校內一共有20處建筑,分別標為1號樓,號樓,.,號樓。有些樓之間有道路連接,道路是雙向的,如果樓與樓間有道路,那么既可以從樓到樓,也可以從樓到樓。首先將輸入校內的道路數n, 接下來分n行輸入各條道路的信息,每行有兩個整數(均在和

18、之間),代表這兩座樓之間有道路連接。接下來輸入查詢數m, 然后分m行輸入要查詢的樓間連路信息,每行有兩個整數(均在和之間)。如果兩樓之間可以通過一條路徑到達(中途有可能經過其它樓),則輸出兩樓是連接的,否則輸出兩樓是斷開的。函數接口定義:完成查詢兩建筑是否連通的函數test裁判測試程序樣例:#include <iostream>using namespace std;const int N=21;/* 請在這里填寫答案 */int main() int aNN=0, n, m, i, j, k; cin>>n; for(i=0;i<n;i+) cin>>

19、;j>>k; ajk=akj=1; cin>>m; for(i=0;i<m;i+) cin>>j>>k; cout<<j<<'-'<<k<<' ' if(test(a, j, k) cout<<"connected"<<endl; else cout<<"disconnected"<<endl; return 0;·輸入樣例:21 22 321 31 4輸出樣例:1

20、-3 connected1-4 disconnectedbool test(int aNN, int j, int k)/利用深度優先搜索找到一條從j到k的通路,若不存在則返回false/維護一個一維數組,模擬棧的操作,從j開始深搜到一個鄰居i且沒有搜索過,則將i入棧,如果i=k則搜索成功 /維護一個一維數組,有N個元素,用于記錄某個樓是否搜索過 int stackN+1 = 0; /存儲從j開始走過的路徑,如 j m n t,表示從j開始經過m n走到了t bool visitedN+1 = false;int top = 0; /top記錄數組stack的最后一個元素的位置, /首先j入棧

21、stack+top = j;visitedj = true;while(top > 0) /當前棧不空int cur = stacktop-; /得到當前棧頂/把與cur連接的所有未訪問過的樓號壓入棧for(int i = 0; i < N; i+)if(acuri = 1 && visitedi = false)stack+top = i;if(i = k) return true; /訪問到k則表示從j到k有通路visitedi = true; return false;6-5 引用作函數形參交換兩個整數 (10 分)設計一個void類型的函數Swap,該函數有

22、兩個引用類型的參數,函數功能為實現兩個整數交換的操作。裁判測試程序樣例:#include <iostream>using namespace std;void Swap(int& x,int& y)    int temp;    temp = x;    x = y;    y = temp; int main() int a, b; cin >> a >> b; Swap(a, b

23、); cout << a << " " << b << endl; return 0;輸入樣例:3 5輸出樣例:5 36-6 函數重載實現兩數相加 (15 分)設計一個重載函數add,該函數有兩個參數,可以實現兩個類型相同的參數相加的操作,函數返回相加的結果。兩個參數可以是整數、實數和字符串,但必須保證兩個參數類型相同。裁判測試程序樣例:#include <iostream>#include <string>#include <iomanip>using namespace std;int

24、add(int x,int y)    return x+y;double add(double x,double y)    return x+y;string add(string x,string y)    return x+y;int main() int a, b; double c, d; string s1, s2; cin >> a >> b; cin >> c >> d; cin >> s1 &g

25、t;> s2; cout << add(a, b) << endl; cout << fixed << setprecision(2) << add(c, d) << endl; cout << add(s1, s2) << endl; return 0;輸入樣例:3 53.3333 5.hello world輸出樣例:88.89helloworld6-7 帶默認形參值的函數 (10 分)設計一個帶默認形參值的函數add,該函數有三個參數,可以實現三個整數類型的參數相加的操作,函數返回相加的結

26、果。默認形參值從右至左分別是30、20。裁判測試程序樣例:#include <iostream>using namespace std;int add(int x,int y=20,int z=30)    return x+y+z;int main() int a, b, c; cin >> a >> b >> c; cout << add(a) << endl; cout << add(a, b) << endl; cout << add(a,

27、 b, c) << endl; return 0;輸入樣例:1 2 3輸出樣例:513366-8 使用動態內存分配的冒泡排序 (20 分)編程實現冒泡排序函數int* bubble_sort(int n);。其中n為數組長度(1n1000)。函數接口定義如下:int* bubble_sort(int n);/* 對長度為n的數組arr執行冒泡排序 */請實現bubble_sort函數,使排序后的數據從小到大排列。要求在bubble_sort函數內使用動態內存分配方式分配一個大小為n的數組,再讀入待排序數據,排序完成后返回數組。裁判測試程序樣例:#include <iostre

28、am>using namespace std;int* bubble_sort(int n);/* 對長度為n的數組執行冒泡排序 */int main() int n; cin >> n; int* a = bubble_sort(n); for (int i = 0; i < n; i+) cout << ai; if (i < n - 1)cout << " " cout << endl; return 0;/* 你的代碼將嵌在這里 */輸入樣例:1015168 28139 13714 27801 222

29、08 32524 21653 8353 28341 25922輸出樣例:8353 13714 15168 21653 22208 25922 27801 28139 28341 32524int* bubble_sort(int n) int *a=new intn; int i; for(i=0;i<n;i+)   cin>>ai; for(i=0;i<n;i+)     for(int j=0;j<n-1-i;j+)       if(aj&

30、gt;aj+1)           int temp;           temp = aj+1;           aj+1=aj;           aj=temp;  

31、;            return a; delete a; 6-9 逆序字符串 (10 分)設計一個void類型的函數reverse_string,其功能是將一個給定的字符串逆序。例如,給定字符串為“hello”,逆序后為“olleh”。函數接口定義如下:/* 函數原型參見main函數 */裁判測試程序樣例:#include <iostream>#include <string>using namespace std;/* 你的代碼將嵌在這里 */int main(

32、int argc, char const *argv) string str; getline(cin, str);/輸入字符串 reverse_string(str); /逆序字符串str cout << str << endl;/輸出逆序后的字符串 return 0;輸入樣例:hello輸出樣例:ollehvoid reverse_string(string &str)    int n;    char t;    n = str.length();  &#

33、160; for(int i=0;i<n/2;i+)        t=stri;        stri=strn-1-i;        strn-1-i=t;     7-1 時間換算 (10 分)輸入一個正整數 repeat (0<repeat<10),做 repeat 次下列運算:輸入一個時間數值,再輸入秒數

34、n,輸出該時間再過 n 秒后的時間值,時間的表示形式為時:分:秒,超過 24 時從 0 時重新開始計時。輸出格式: printf("time: %d:%d:%dn", );輸入輸出示例:括號內為說明,無需輸入輸出輸入樣例:3 (repeat=3)0:0:159 (秒數n=59)11:59:40 30 (秒數n=30)23:59:40 301 (秒數n=301)輸出樣例:time: 0:1:0 (0:0:01加上59秒的新時間) time: 12:0:10 (11:59:40加上30秒的新時間)time: 0:4:41 (23:59:40加上301秒的新時間)#include

35、 <iostream>using namespace std;struct Timeint hour;int minute;int second;void timeCompute(Time &t, int sec)int h, m, s;int a;s = t.second + sec;a = s / 60; /分鐘的進位 t.second = s % 60; /進位后剩余的秒數m = t.minute + a;a = m / 60;t.minute = m % 60;h = t.hour + a;t.hour = h % 24; int main()int repeat,

36、 sec;cin >> repeat;Time t;for(int i = 1; i <= repeat; i+)scanf("%d:%d:%d", &t.hour, &t.minute, &t.second);cin >> sec;timeCompute(t, sec);printf("time: %d:%d:%dn", t.hour, t.minute, t.second);7-2 查找單價最高和最低的書籍 (10 分)編寫程序,從鍵盤輸入 n (n<10)本書的名稱和定價并存入結構數組中,

37、查找并輸出其中定價最高和最低的書的名稱和定價。輸出格式語句:printf("highest price: %.1f, %sn", );printf("lowest price: %.1f, %sn",);輸入輸出示例:括號內為說明,無需輸入輸出輸入樣例:3(n=3)Programming in C21.5Programming in VB18.5Programming in Delphi25輸出樣例:highest price: 25.0, Programming in Delphi lowest price: 18.5, Programming in

38、VB #include<iostream>#include<string>using namespace std; struct BOOK char name20; double price; BOOK; int main() int i,n,j,k,max=0,min=0; scanf("%d",&n); struct BOOK an; for(i=0;i<n;i+) scanf("%sn",); scanf("%lf",&ai.price); for(j=0;j<n

39、;j+) if(aj.price>amax.price) max=j; for(k=0;k<n;k+) if(ak.price<amin.price) min=k; printf("highest price: %.1f, %sn",amax.price, ); printf("lowest price: %.1f, %sn",amin.price,); return 0; #include <iostream>using namespace std;struct Bookchar name50;float price;int main()int bookCnt;cin >> bookCnt;Book *book = new BookbookCnt;for(int i = 0; i < bookCnt; i+)cin.ignore();cin.getline(booki.na

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論