描述
某个科室的病房分为重症和普通,只有当病人的疾病严重程度超过了入住重症病房的最低严重值,才可以安排入住重症病房。
现在要求设计一个程序,给病人安排好病房。疾病的严重程度用0到10来表示,0表示小毛病,10表示非常严重。
输入
第一行输入病人的个数m(m < 50),以及安排住入重症病房的最低严重值a
紧接着m行,每行表示病人编号(三个位,用0补齐)及其疾病的严重程度(浮点数,1位小数)。
每个病人的疾病严重程度都不一样。
输出
要求按照病人的严重程度输出住在重症病房里的病人的编号
注意:
如果当前所有病人的严重程度并不满足住在重症病房里,则输出“None.”(不包括引号)
解题思路:
1、病人的编号和严重程度可以分别用两个数组,或者使用结构(可以练习一下)
2、按照严重程度从小到大排列
3、输出格式(这个很重要)
小数输出格式
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecisionint main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout
使用typedef struct
typedef struct patient {
int num;
float degree;
};
#include <iostream>
#include <iomanip>
//#include <cstdio>
using namespace std;
int main()
{
float degree[50];
int num[50];
int m;
float max;
cin >> m >> max;
//int q = 0;
//while (q <m ) {
// cin >> num[i] >> degree[i];
// q++;
// m--;//问题出现在这里!!导致m出了问题(原先是用while(m>0))
//}
for (int i = 0; i < m; i++) {
cin >> num[i] >> degree[i];
}
/*
** 可以先对严重程度进行从大到小排序
*/
//bool flag = false;
for (int i = 0; i < m; i++) {
//if (degree[i] >= max) {
// flag = true;
//}
for (int j = i + 1; j < m; j++) {
float tempdegree;
int tempnum;
if (degree[i] >= max && degree[i] < degree[j]) {
tempdegree = degree[i];
tempnum = num[i];
degree[i] = degree[j];
num[i] = num[j];
degree[j] = tempdegree;
num[j] = tempnum;
}
}
}
/*
**如果把这个条件判断放在排序一起进行的话,排序的条件要做出相应的改变
*/
bool flag = false;
for (int i = 0; i < m; i++) {
if (degree[i] >= max) {
flag = true;
}
}
//cout << flag << endl;
if (flag == false) {
//cout << "None" << endl;//知道为什么一直错了吗?
cout << "None." << endl;
return 0;//直接结束
}
else {
for (int i = 0; i < m; i++) {
if (degree[i] >= max) {
printf("%03d", num[i]);
//std::cout << std::setprecision(1) << degree[i] << '\n';
//cout << ' ' << fixed << setprecision(1) << degree[i] << endl;
cout << ' ' << setiosflags(ios::fixed) << setprecision(1) << degree[i] << endl;
}
}
return 0;
}
}
觉得自己真的很搞笑,因为一个点“.” ,纠结了一两小时,不知道错哪了。哈哈哈