[大学计算] 自测4 答案
最长劳动时间
输入n个人的劳动时间,时间用类型为tTime的结构体数组变量存放,输出劳动时间最长的数据。tTime的定义如下:
struct tTime
{
int hour, min, sec;
};
请完成一个计算最长劳动时间的C++函数,该函数原型是:
tTime maxTime(tTime a[], int n);
其中a为劳动时间数组,n是a中元素个数,返回最长的劳动时间。
例如:输入5个人的劳动时间分别为:
2 50 0 // 第1个人: 2小时50分0秒
1 10 10
1 10 9
5 0 0
35 0 1
函数返回的结构成员值分别为:35, 0, 1
注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。
#include <iostream>
using namespace std;
struct tTime
{
int hour, min, sec;
};
tTime maxTime(tTime a[], int n)
{
/**********Program**********/
/********** End **********/
}
int main()
{
//忽略阅读
int n;
tTime a[1005], d;
cin >> n;
for(int j=0; j<n; j++)
cin >> a[j].hour>>a[j].min>>a[j].sec;
d = maxTime(a, n);
cout<<d.hour<<" "<<d.min<<" "<<d.sec<<endl;
//忽略阅读结束
return 0;
}
答案:
#include <iostream>
using namespace std;
struct tTime
{
int hour, min, sec;
};
tTime maxTime(tTime a[], int n)
{
/**********Program**********/
int Max = 0;
int res;
for (int i = 0; i < n; i ++)
{
int tmp = a[i].hour * 3600 + a[i].min * 60 + a[i].sec;
if (Max < tmp)
{
Max = tmp;
res = i;
}
}
return a[res];
/********** End **********/
}
int main()
{
//忽略阅读
int n;
tTime a[1005], d;
cin >> n;
for(int j=0; j<n; j++)
cin >> a[j].hour>>a[j].min>>a[j].sec;
d = maxTime(a, n);
cout<<d.hour<<" "<<d.min<<" "<<d.sec<<endl;
//忽略阅读结束
return 0;
}
某日期是本年中第几天
请完成函数int days(tDate t);
函数参数t是一个结构体变量(包括年、月、日),该函数计算并返回该天在本年中为第几天。
例如,2011年2月1日是该年的第32天。
提示:闰年的条件是,年份能够被4整除且不能被100整除,或者能够被400整除。
注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。
#include <iostream>
using namespace std;
struct tDate
{
int year;
int mon;
int day;
};
int days(tDate t) {
/**********Program**********/
/********** End **********/
}
int main()
{
tDate t;
cin >> t.year >> t.mon >> t.day;
cout << days(t) << endl;
return 0;
}
答案:
#include <iostream>
using namespace std;
struct tDate
{
int year;
int mon;
int day;
};
int days(tDate t) {
/**********Program**********/
int m[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((t.year % 4 == 0 && t.year % 100 != 0) || t.year % 400 == 0)
{
m[2] ++;
}
int tot = 0;
for (int i = 1; i < t.mon; i ++)
{
tot += m[i];
}
return tot + t.day;
/********** End **********/
}
int main()
{
tDate t;
cin >> t.year >> t.mon >> t.day;
cout << days(t) << endl;
return 0;
}
面试成绩评分
某次面试总共有n个面试人员,但该岗位只有1个录取名额。
面试有7个专家,面试成绩的计算方法是去掉1个最高分和1个最低分,然后再取平均值,最后成绩最高的同学将获得录取名额(不用考虑有多个最高成绩的问题)。
请编写函数int HighestScore(Student stuLst[], int n),根据n个同学的专家打分计算并返回获得录取名额的同学的下标。
每个学生的信息用一个结构体Student表示,其中name表示学生姓名,scores表示对该学生的7个专家打分,每个学生的name和scores是已知的。
struct Student{
char name[30];
int scores[7];
};
例如,假设4个面试人员的姓名和专家打分如下:
alfa: 86, 83, 90, 80, 92, 87, 84
beta: 82, 87, 78, 93, 83, 85, 82
cigar: 83, 85, 93, 84, 83, 90, 89
delta: 90, 84, 91, 85, 81, 84, 85
则最终获得录取名额的是cigar, 去掉1个最高分和1个最低分后,平均分86.2分。
注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。
#include <iostream>
using namespace std;
struct Student{
char name[30];
int scores[7];
};
int HighestScore(Student stuLst[], int n);
/**********Program**********/
/********** End **********/
int main()
{
int n;
cin >> n;
Student* stuLst = new Student[n];
for (int i = 0; i < n; i++)
{
cin >> stuLst[i].name;
for (int j = 0; j < 7; j++)
{
cin >> stuLst[i].scores[j];
}
}
cout << HighestScore(stuLst, n) << endl;
delete[] stuLst;
return 0;
}
答案:
#include <iostream>
using namespace std;
struct Student{
char name[30];
int scores[7];
};
int HighestScore(Student stuLst[], int n);
/**********Program**********/
int HighestScore(Student stuLst[], int n)
{
double Max = 0;
int res;
for (int i = 0; i < n; i ++)
{
double minn = 100, maxx = 0;
double tot = 0;
for (int j = 0; j < 7; j ++)
{
tot += stuLst[i].scores[j];
if (minn > stuLst[i].scores[j]) minn = stuLst[i].scores[j];
if (maxx < stuLst[i].scores[j]) maxx = stuLst[i].scores[j];
}
tot = (tot - minn - maxx) / 5;
if (tot > Max)
{
res = i;
Max = tot;
}
}
return res;
}
/********** End **********/
int main()
{
int n;
cin >> n;
Student* stuLst = new Student[n];
for (int i = 0; i < n; i++)
{
cin >> stuLst[i].name;
for (int j = 0; j < 7; j++)
{
cin >> stuLst[i].scores[j];
}
}
cout << HighestScore(stuLst, n) << endl;
delete[] stuLst;
return 0;
}
两链表求并集
给定两个链表A和B,每个链表里面都不存在重复元素,函数ListUnion(A, B)求A,B两个链表元素的并集,并将结果存入链表A中,即对B中的每个结点,若该结点在A中不存在,则将该元素加到链表A末尾,并返回新的链表首节点,请实现该函数。
例如链表A = 2->3->5->7->8,链表B = 1->9->3->6->8,
则执行该函数后,A = 2->3->5->7->8->1->9->6。
提示:先找到链表A的末尾节点。
注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
node* ListUnion(node* A, node* B)
{
/**********Program**********/
/********** End **********/
}
int main()
{
int i,j,m,n;
node *t;
node* A = NULL, *B = NULL;
cin >> m >> n ; //两链表数据个数
int a[m];
int b[n];
for(j=0;j<m;j++)
{
cin >>a[j];
t=new node;
t->data=a[j];
t->next=A;
A=t;
}
for(j=0;j<n;j++)
{
cin >>b[j];
t=new node;
t->data=b[j];
t->next=B;
B=t;
}
A = ListUnion(A, B);
t=A;
while(t)
{
cout<<t->data;
t=t->next;
if (t != NULL)
cout << " ";
}
cout<<endl;
return 0;
}
答案:
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
};
node* ListUnion(node* A, node* B)
{
/**********Program**********/
node * ptrb = B;
node * preb = NULL;
while (ptrb)
{
int a = ptrb -> data;
node * ptra = A;
node * prea = NULL;
bool judge = 0;
while (ptra)
{
if (ptra -> data == a)
{
judge = 1;
}
prea = ptra;
ptra = ptra -> next;
}
if (judge == 0)
{
node * tmp = new node;
tmp -> data = a;
tmp -> next = NULL;
prea -> next = tmp;
}
preb = ptrb;
ptrb = ptrb -> next;
}
return A;
/********** End **********/
}
int main()
{
int i,j,m,n;
node *t;
node* A = NULL, *B = NULL;
cin >> m >> n ; //两链表数据个数
int a[m];
int b[n];
for(j=0;j<m;j++)
{
cin >>a[j];
t=new node;
t->data=a[j];
t->next=A;
A=t;
}
for(j=0;j<n;j++)
{
cin >>b[j];
t=new node;
t->data=b[j];
t->next=B;
B=t;
}
A = ListUnion(A, B);
t=A;
while(t)
{
cout<<t->data;
t=t->next;
if (t != NULL)
cout << " ";
}
cout<<endl;
return 0;
}
按值大小顺序插入链表
实现函数insert,其功能为:将np指向的结点(该结点在外面已经创建好)按num从小到大的顺序插入head指向的单向链表中,即最后head指向的链表结点是按num从小到大排列的(提示:过程中也是),函数返回插入后的新链表。
注意:仅在标有”Program”和”End”的注释行之间补充填写代码。
请勿改动主函数main和其它任何已有内容。
#include <iostream>
using namespace std;
struct node
{
int num;
struct node * next;
};
node * insert(node *head, node *np)
{
/**********Program**********/
/********** End **********/
}
void del(node *head)
{
node *p=head;
while(p)
{
head = head->next;
delete p;
p=head;
}
}
int main()
{
//以下忽略阅读
node * phead=NULL;
int num;
phead=NULL;
for (int j=0;j<10;j++)
{
cin>>num;
node *tmp=new node;
tmp->num = num;
phead = insert(phead,tmp);
}
node *p = phead;
while(p)
{
cout<<p->num<<" ";
p=p->next;
}
cout<<endl;
del(phead);
//忽略阅读结束
return 0;
}
答案:
#include <iostream>
using namespace std;
struct node
{
int num;
struct node * next;
};
node * insert(node *head, node *np)
{
/**********Program**********/
node * ptr = head;
node * pre = NULL;
if (head == NULL)
{
head = np;
return head;
}
while (ptr != NULL)
{
if (ptr -> num > np -> num)
{
if (ptr == head)
{
// cout << "head " << ptr -> num << endl;
np -> next = ptr;
head = np;
}
else
{
// cout << pre -> num << " " << ptr -> num << endl;
np -> next = pre -> next;
pre -> next = np;
pre = pre -> next;
}
break;
}
pre = ptr;
ptr = ptr -> next;
}
if (ptr == NULL)
{
pre -> next = np;
}
return head;
/********** End **********/
}
void del(node *head)
{
node *p=head;
while(p)
{
head = head->next;
delete p;
p=head;
}
}
int main()
{
//以下忽略阅读
node * phead=NULL;
int num;
phead=NULL;
for (int j=0;j<10;j++)
{
cin>>num;
node *tmp=new node;
tmp->num = num;
phead = insert(phead,tmp);
}
node *p = phead;
while(p)
{
cout<<p->num<<" ";
p=p->next;
}
cout<<endl;
del(phead);
//忽略阅读结束
return 0;
}
检查注册账号
题目大意:fileName是文件的名称,文件里面存着所有已经注册的用户名(一行一个,每个用户名中间没有空格之类的),然后判断userName是否已经存在于文件中。如果有,返回0,如果没有,返回1
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool checkUserName(char *fileName, char *userName)
{
/**********Program**********/
/********** End **********/
}
int main()
{
//忽略阅读
char userName[30];
char *fileName = "/data/workspace/myshixun/step1/UserNames.txt";
for(int j = 0; j < 10; j++)
{
cin >> userName;
cout << checkUserName(fileName, userName);
}
//忽略阅读结束
return 0;
}
测试样例:
输入:
ABCDEFGH
fxlmwfirlg
jdbdrgqv
pvwdctd
buufn
vu
mvzogpepmecx
ctnaahh
sc
bhvdxrptha
输出:
1101000110
答案:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool checkUserName(char *fileName, char *userName)
{
/**********Program**********/
FILE * fptr = fopen(fileName, "r");
// cout << feof(fptr) << " ";
// char tmp[256] = {0};
// fgets(tmp, 256, fptr);
// cout << tmp;
while (!feof(fptr))
{
char tmp[256] = {0};
fgets(tmp, 256, fptr);
tmp[strlen(tmp) - 1] = '\0';
if (strcmp(tmp, userName) == 0)
{
fclose(fptr);
return 0;
}
}
return 1;
/********** End **********/
}
int main()
{
//忽略阅读
char userName[30];
char *fileName = "/data/workspace/myshixun/step1/UserNames.txt";
for(int j = 0; j < 10; j++)
{
cin >> userName;
cout << checkUserName(fileName, userName);
}
//忽略阅读结束
return 0;
}
班级平均成绩
题目大意:文件名为fileName,文件中每一行一个学生的信息,从左到右依次是学号(20位整数)、班级(整数,0、1、2之类)、成绩(浮点类型),中间用空格隔开。现在要求统计特定班级(classNo)的学生的平均成绩(相同班级的学生再文件中可能不是连续的)。其中,0班用于测试,平均成绩应该是70.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
double average(char *fileName, int classNo)
{
/**********Program**********/
/********** End **********/
}
int main()
{
//忽略阅读
int nos;
cin >> nos;
int classNo = nos;
cout << setprecision(4) << average("/data/workspace/myshixun/step1/scores.txt", classNo) << endl;
//忽略阅读结束
return 0;
}
输入输出样例:
输入:
1
输出:
73.85
答案:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
double average(char *fileName, int classNo)
{
/**********Program**********/
FILE * fptr = fopen(fileName, "r");
double tot = 0;
int cnt = 0;
while (!feof(fptr))
{
char * num = new char[25];
int no;
double score;
fscanf(fptr, "%s %d %lf\n", num, &no, &score);
if (no == classNo)
{
// cout << num << " " << no << " " << score << endl;
tot += score;
cnt ++;
}
}
fclose(fptr);
return tot / cnt;
/********** End **********/
}
int main()
{
//忽略阅读
int nos;
cin >> nos;
int classNo = nos;
cout << setprecision(4) << average("/data/workspace/myshixun/step1/scores.txt", classNo) << endl;
//忽略阅读结束
return 0;
}

浙公网安备 33010602011771号