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

提交评论