一道输入,判断,排序,输出题。
学到的点:
- 结构体vector的push_back 和sort
- log要用换底公式
- 编译的时候直接按特定if里的当前行可以跳过其它组数据
- 输出时的iomanip 头文件
I/O流常用控制符:
使用控制符时,在程序开头加投文件#include <iomanip> C++有两种方法控制格式输出:1、用格式控制符;2、用流对象的成员函数 格式控制符:
dec 设置基数为10
hex 设置基数为16
oct 设置基数为8
setfill(c) 设置填充字符c
setprecision(n) 设置显示小数精度为n位
setw(n) 设置域宽为n个字符
setiosflags(ios::fixed) 固定的浮点显示
setiosflags(ios::scientific) 指数表示
setiosflags(ios::left) 左对齐
setiosflags(ios::right) 右对齐
setiosflags(ios::skipws) 忽略前导空白
setiosflags(ios::uppercase) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
代码如下
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<sstream>
#include<algorithm>
#include<cmath>
using namespace std;
struct acm {
string name;
int score;
int time;
};
bool cmp(acm x, acm y) {
if (x.score != y.score)return x.score > y.score;
else return x.time < y.time;
}
int main()
{
vector<acm> a;
//acm first;
float m, n;
cin >> m >> n;
//a.push_back(first);
int t = 0;
string s;
while (cin >> s) {
acm now;
now.name = s;
now.time = 0;
now.score = 0;
for (int i = 0; i < m; i++) {
string x;
cin >> x;
stringstream ss(x);
int y;
string z;
ss >> y;
ss >> z;
//if (y < 0) now.time += -y;
//if (y == 0);
if (y > 0) {
now.score += 1;
now.time += y;
int q = (int)(log(y)/log(10)) + 1,w= x.length();
if (x.length() > q)
{z = x.substr(q+1, x.length() - 2);
stringstream sss(z);
int yy;
sss >> yy;
now.time += yy*n;
}
}
}
a.push_back(now);
}
sort(a.begin(), a.end(), cmp);
for (int j = 0; j < a.size(); j++) {
//cout << a[j].name << endl;
cout << setw(10) << setiosflags(ios::left) << a[j].name;
cout << setw(2) << a[j].score;
printf("%4d\n", a[j].time);
}
return 0;
}