HDU 1236 排名

HDU 1236 排名

了解了使用string时常犯的错误:c_str函数的返回值是const char*的,因此绝对不能scanf("%s",s.c_str());。
另外题目中建议使用scanf()(本来使用cin/cout一点事情都没有,结果换了scanf()/printf()发现了这个错误)。

#include < iostream >
#include
< string >
#include
< algorithm >
#include
< cstdio >
using   namespace  std;

class  Student
{
    
public :
        Student():sum_score_(
0 ) {}
        
        
string  name_;
        
int  sum_score_;
};

bool   operator < ( const  Student  & a, const  Student  & b)
{
    
return  (a.sum_score_ > b.sum_score_  ||  (a.sum_score_ == b.sum_score_  &&  a.name_ < b.name_));
}

int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
    
int  n,m,g;
    
while (scanf( " %d%d%d " , & n, & m, & g) == 3   &&  n)
    {
        
int  score[ 17 ];
        
for ( int  i = 0 ;i < m;i ++ )
            scanf(
" %d " , & score[i]);
        
        Student stu[
1007 ];
        
int  passed( 0 );
        
for ( int  i = 0 ;i < n;i ++ )
        {
            
//   Can not use scanf("%s",stu[i].name_.c_str());
            cin >> stu[i].name_;
            
int  solved;
            scanf(
" %d " , & solved);
            
for ( int  j = 0 ;j < solved;j ++ )
            {
                
int  pro_num;
                scanf(
" %d " , & pro_num);
                stu[i].sum_score_
+= score[pro_num - 1 ];
            }
            
if (stu[i].sum_score_ >= g)
                passed
++ ;
        }
        
        sort(stu,stu
+ n);
        
        printf(
" %d\n " ,passed);
        
for ( int  i = 0 ;i < passed;i ++ )
            printf(
" %s %d\n " ,stu[i].name_.c_str(),stu[i].sum_score_);
    }
    
return   0 ;
}

你可能感兴趣的:(HDU 1236 排名)