题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2093
所需知识储备:C\C++
涉及知识点:字符串处理、格式化输入输出、STL string以及sort()的使用。
思路:
(1) 输入数据的处理
难点在于正数后面括号的处理。
补充:很巧妙地一种方式:
int t1=0, n2=0;
if(scanf("%d(%d)", &t1, &n2) == 1)
{
if(t1 > 0)
{
s.num++;
s.time += t1;
}
}
else
{
s.num++;
s.time += t1 + n2 * m;
}
我想到了两种方式。
第一种: 读入一个数(不包括第一行)后,利用getchar()函数读入一个字符,判断其是否为 ‘(’。若是,处理括号内的数;否则,继续读取后面的数据。
第二种:不管整数后面有没有括号,将其当做一个字符串读入,之后从中提取数据,代码如下:
string str;
cin>>str;
if(str[0] == '-') continue;
else if(str.length() == 1 && str[0] == '0') continue;
int t1=0, t2=0;
int j=0;
for(;j<str.length();j++)
{
if(str[j] == '(')
{
j++;
for(;str[j] != ')';j++)
{
t2 = t2*10 + (str[j] - '0');
}
}
else
{
t1 = t1 * 10 + (str[j]-'0');
}
}
t2 *= m;
s.num++;
s.time += t1+t2;
(2) 数据的排序
这是一个多关键字排序的问题,我们可以维护一个结构体STD,里面存储着学生姓名(name)、做对题数(num)、消耗时长(time),对应数据类型为string,int,int。我们只要规定好STD之间比较的规则,用vector存储STD类型的元素(因为题中并明确没有给出学生的数量,用可变长的vector比较方便),利用sort函数即可完成排序。有两种方法。
第一种:在结构体内重载运算符 “<”, 一定要注意,这个函数得是常函数,定义时一定要加const,否则就会出错。代码如下:
bool operator < (const Std &s) const
{
if (num != s.num) return num > s.num;
else if(time != s.time) return time < s.time;
else return name < s.name;
}
第二种:自定义cmp函数,代码如下:
bool cmp(const Std &s1, const Std &s2)
{
if (s1.num != s2.num) return s1.num > s2.num;
else if(s1.time != s2.time) return s1.time < s2.time;
else return s1.name < s2.name;
}
(3) 格式化输出
注意,如果读入使用了cin,这里最好不要用printf,否则可能真的会出玄学问题(因为这个原因我卡了好长时间)。
一种可行的输出代码如下:
for(vector<Std>::iterator it=V.begin();it != V.end();it++)
{
cout<<setw(10)<<left<<it->name<<" ";
cout<<setw(2)<<right<<it->num<<" ";
cout<<setw(4)<<right<<it->time<<endl;
}
完整代码1
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include <iomanip>
using namespace std;
struct Std{
int num;
int time;
string name;
Std(){
num=0;
time=0;
};
bool operator < (const Std &s) const
{
if (num != s.num) return num > s.num;
else if(time != s.time) return time < s.time;
else return name < s.name;
}
};
vector<Std> V;
int main ()
{
int n, m;
cin>>n>>m;
int N=0;
while(true)
{
Std st;
if(!(cin>>st.name)) break;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x < 0)
{
}
else if(x > 0)
{
bool f = (getchar() == '(');
st.num++;
st.time += x;
if (f)
{
int y;
cin>>y;
getchar();
st.time += y*m;
}
}
}
V.push_back(st);
N++;
}
sort(V.begin(), V.end());
for(vector<Std>::iterator it=V.begin();it !=V.end();it++)
{
cout<<setw(10)<<left<<(*it).name<<" ";
cout<<setw(2)<<right<<(*it).num<<" ";
cout<<setw(4)<<right<<(*it).time<<endl;
}
return 0;
}
完整代码2
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
int n, m;
struct Std{
int num;
int time;
string name;
};
bool cmp(const Std &s1, const Std &s2){
if (s1.num != s2.num) return s1.num > s2.num;
else if(s1.time != s2.time) return s1.time < s2.time;
else return s1.name < s2.name;
}
vector<Std> V;
bool getALine()
{
Std s;
s.num=0;
s.time=0;
if(!(cin>>s.name)) return false;
for(int i=0;i<n;i++)
{
string str;
cin>>str;
if(str[0] == '-') continue;
else if(str.length() == 1 && str[0] == '0') continue;
int t1=0, t2=0;
int j=0;
for(;j<str.length();j++)
{
if(str[j] == '(')
{
j++;
for(;str[j] != ')';j++)
{
t2 = t2*10 + (str[j] - '0');
}
}
else
{
t1 = t1 * 10 + (str[j]-'0');
}
}
t2 *= m;
s.num++;
s.time += t1+t2;
}
V.push_back(s);
return true;
}
int main ()
{
cin>>n>>m;
while(true)
{
if(!getALine()) break;
}
sort(V.begin(), V.end(), cmp);
for(vector<Std>::iterator it=V.begin();it != V.end();it++)
{
cout<<setw(10)<<left<<it->name<<" ";
cout<<setw(2)<<right<<it->num<<" ";
cout<<setw(4)<<right<<it->time<<endl;
}
return 0;
}
补充,可以用scanf("%d(%d)", &t1, &n2) == 1
来巧妙地处理()。
完整代码3:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
int n, m;
struct Std{
int num;
int time;
string name;
bool operator <(const Std &s) const
{
if(num != s.num) return num > s.num;
else if(time != s.time) return time < s.time;
else name < s.name;
}
};
vector<Std> V;
int main ()
{
scanf("%d %d", &n, &m);
while(true)
{
Std s; s.num=0, s.time=0;
char name[15];
if(!(~scanf("%s", name))) break;
for(int i=0;i<n;i++)
{
s.name = name;
int t1=0, n2=0;
if(scanf("%d(%d)", &t1, &n2) == 1)
{
if(t1 > 0)
{
s.num++;
s.time += t1;
}
}
else
{
s.num++;
s.time += t1 + n2 * m;
}
}
V.push_back(s);
}
sort(V.begin(), V.end());
for(vector<Std>::iterator it=V.begin();it != V.end();it++)
{
printf("%-10s ", it->name.c_str());
printf("%2d ", it->num);
printf("%4d\n", it->time);
}
return 0;
}