【试题一】
1.关羽过关斩三将,输入四个人的武力值(大于 0 小于 50),若超过界限 需
要重新输入,关羽的武力值 x,将士武力值为 y,满足(x-y)^2+(x-y)+41 若为素数则关羽获胜,若关羽三次获胜输出 WIN,若失败则输出 失败的将领序号(第几关)。
#include<iostream>
using namespace std;
bool Judge(int x){
if (x > 0 && x < 50)
return true;
return false;
}
bool Win(int x, int y) {
int temp = pow(x - y, 2) + (x - y) + 41;
for (int i = 2; i <=sqrt(temp) ; i++)
if (temp%i==0)
return false;
return true;
}
int main() {
int y, g[3], i, flag = 0;
cin >> y;
while (!Judge(y))
{
cout << "请重新输入关羽的武力值" << endl;
cin >> y;
}
for (i = 0; i < 3; i++) cin >> g[i];
while (!Judge(y)||!Judge(y)||!Judge(y))
{
cout << "请重新输入全部武将的武力值" << endl;
for (i = 0; i < 3; i++) cin >> g[i];
}
for ( i = 0; i < 3; i++)
if (Win(y, g[i])) flag++;
else break;
if (flag == 3)
cout << "WIN" << endl;
else
cout << ++flag << endl;
system("pause");
return 0;
}
【试题二】
2.输入 N 个员工,每个员工输出 ID 号,上班时间,下班时间,
第一行输出 最早去的员工的 ID 和上班时间
第二行输出 最迟走的员工的 ID 和下班时间
第三行输出 工作最久的员工的 ID 和上班时间
数据瞎写的:
ID100001 08:00:00 18:00:00
ID100002 07:00:00 17:00:00
ID100003 09:00:00 21:00:00
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct employee {
string Id;
int s_h, s_m, s_s;
int e_h, e_m, e_s;
int work_time;
};
bool early(employee a, employee b) {
if (a.s_h != b.s_h)
return a.s_h < b.s_h;
else if (a.s_m != b.s_m)
return a.s_m < b.s_m;
else
return a.s_s < b.s_s;
}
bool lately(employee a, employee b) {
if (a.e_h != b.e_h)
return a.e_h > b.e_h;
else if (a.e_m != b.e_m)
return a.e_m > b.e_m;
else
return a.e_s > b.e_s;
}
bool cmp_time(employee &a, employee &b) {
a.work_time = (a.e_h - a.s_h) * 3600 + (a.e_m - a.s_m) * 60 + (a.e_s - a.s_s);
b.work_time = (b.e_h - b.s_h) * 3600 + (b.e_m - b.s_m) * 60 + (b.e_s - b.s_s);
return a.work_time > b.work_time;
}
int main() {
int n, i;
cin >> n;
employee *p=new employee[n];//也可以用容器存放
for (i = 0; i < n; i++)
{
cin >> p[i].Id;//个人对于string类型的处理比较生疏
scanf_s("%d:%d:%d", &p[i].s_h, &p[i].s_m, &p[i].s_s);
scanf_s("%d:%d:%d", &p[i].e_h, &p[i].e_m, &p[i].e_s);
}
sort(p, p + n, early);
cout << "Open:" << p[0].Id << " ";
printf(" %02d:%02d:%02d\n", p[0].s_h, p[0].s_m, p[0].s_s);
sort(p, p + n, lately);
cout << "Close:" << p[0].Id << " ";
printf("%02d:%02d:%02d\n", p[0].e_h, p[0].e_m, p[0].e_s);
sort(p, p + n, cmp_time);
cout << "Longest:" << p[0].Id << " ";
printf("%02d:%02d:%02d\n", p[0].s_h, p[0].s_m, p[0].s_s);
system("pause");
return 0;
}