A1153
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int maxn= 10010 ;
int n, m;
struct node
{
string name;
int grade;
} Node[ maxn] ;
bool cmp ( node a, node b)
{
if ( a. grade!= b. grade)
return a. grade> b. grade;
return a. name< b. name;
}
int main ( )
{
cin >> n >> m;
for ( int i= 0 ; i< n; i++ )
{
cin >> Node[ i] . name >> Node[ i] . grade;
}
string a, b;
for ( int T= 1 ; T<= m; T++ )
{
cin >> a >> b;
printf ( "Case %d: %s %s\n" , T, a. c_str ( ) , b. c_str ( ) ) ;
if ( a== "1" )
{
vector< node> path;
for ( int i= 0 ; i< n; i++ )
if ( Node[ i] . name[ 0 ] == b[ 0 ] )
{
path. push_back ( Node[ i] ) ;
}
sort ( path. begin ( ) , path. end ( ) , cmp) ;
if ( path. size ( ) == 0 )
{
printf ( "NA\n" ) ;
}
else
{
for ( int i= 0 ; i< path. size ( ) ; i++ )
{
printf ( "%s %d\n" , path[ i] . name. c_str ( ) , path[ i] . grade) ;
}
}
}
else if ( a== "2" )
{
int cnt= 0 , all= 0 ;
for ( int i= 0 ; i< n; i++ )
if ( b== Node[ i] . name. substr ( 1 , 3 ) )
{
all+ = Node[ i] . grade;
cnt++ ;
}
if ( cnt== 0 )
{
printf ( "NA\n" ) ;
}
else
{
printf ( "%d %d\n" , cnt, all) ;
}
}
else
{
map< string, int > mp;
for ( int i= 0 ; i< n; i++ )
if ( b== Node[ i] . name. substr ( 4 , 6 ) )
{
mp[ Node[ i] . name. substr ( 1 , 3 ) ] ++ ;
}
vector< pair< int , string>> q;
for ( map< string, int > :: iterator it= mp. begin ( ) ; it!= mp. end ( ) ; it++ )
{
q. push_back ( {
it- > second* ( - 1 ) , it- > first} ) ;
}
sort ( q. begin ( ) , q. end ( ) ) ;
if ( q. size ( ) == 0 )
{
printf ( "NA\n" ) ;
}
else
{
for ( int i= 0 ; i< q. size ( ) ; i++ )
{
printf ( "%s %d\n" , q[ i] . second. c_str ( ) , - q[ i] . first) ;
}
}
}
}
}
A1141
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn= 100010 ;
int n;
struct node
{
string id;
double grade;
int cnt;
node ( ) : grade ( 0 ) , cnt ( 0 ) {
}
} ;
map< string, node> mp;
bool cmp ( node a, node b)
{
if ( a. grade!= b. grade)
return a. grade> b. grade;
if ( a. cnt!= b. cnt)
return a. cnt< b. cnt;
return a. id< b. id;
}
int main ( )
{
cin >> n;
string id;
double chengji;
string name;
for ( int i= 0 ; i< n; i++ )
{
cin >> id >> chengji >> name;
for ( int j= 0 ; j< name. size ( ) ; j++ )
name[ j] = tolower ( name[ j] ) ;
if ( id[ 0 ] == 'T' )
{
chengji* = 1.5 ;
}
else if ( id[ 0 ] == 'B' )
{
chengji/ = 1.5 ;
}
mp[ name] . id= name;
mp[ name] . grade+ = chengji;
mp[ name] . cnt++ ;
}
vector< node> path;
for ( map< string, node> :: iterator it= mp. begin ( ) ; it!= mp. end ( ) ; it++ )
{
it- > second. grade= ( int ) ( it- > second. grade) ;
path. push_back ( it- > second) ;
}
sort ( path. begin ( ) , path. end ( ) , cmp) ;
int rank= 1 ;
cout << mp. size ( ) << endl;
for ( int i= 0 ; i< path. size ( ) ; i++ )
{
if ( i && path[ i] . grade!= path[ i- 1 ] . grade)
rank= i+ 1 ;
printf ( "%d %s %d %d\n" , rank, path[ i] . id. c_str ( ) , ( int ) path[ i] . grade, path[ i] . cnt) ;
}
}
A1124
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int maxn= 1111 ;
int n, m, s;
int a[ maxn] ;
string b[ maxn] ;
map< string, bool > c;
vector< int > path;
int main ( )
{
cin >> m >> n >> s;
for ( int i= 1 ; i<= m; i++ )
{
cin >> b[ i] ;
a[ i] = i;
}
while ( s<= m)
{
if ( c[ b[ s] ] == true )
{
s++ ;
}
else
{
path. push_back ( s) ;
c[ b[ s] ] = true ;
s+ = n;
}
}
if ( path. size ( ) != 0 )
{
for ( int i= 0 ; i< path. size ( ) ; i++ )
cout << b[ path[ i] ] << endl;
}
else
cout << "Keep going..." << endl;
}
A1108
#include <iostream>
using namespace std;
int n;
int main ( )
{
cin >> n;
string temp;
int cnt= 0 ;
double all= 0.0 ;
while ( n-- )
{
cin >> temp;
bool success= true ;
double num;
try
{
size_t idx;
num= stof ( temp, & idx) ;
if ( idx< temp. size ( ) )
{
success= false ;
}
}
catch ( . . . )
{
success= false ;
}
if ( num< - 1000 || num> 1000 )
{
success= false ;
}
int k= temp. find ( '.' ) ;
if ( k!= - 1 && temp. size ( ) - k> 3 )
success= false ;
if ( success)
{
cnt++ ;
all+ = num;
}
else
{
printf ( "ERROR: %s is not a legal number\n" , temp. c_str ( ) ) ;
}
}
if ( cnt== 0 )
printf ( "The average of 0 numbers is Undefined\n" ) ;
else if ( cnt== 1 )
printf ( "The average of 1 number is %.2lf\n" , all) ;
else
{
printf ( "The average of %d numbers is %.2lf\n" , cnt, all/ cnt) ;
}
}
stof
A1113
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn= 101111 ;
int n;
vector< int > child;
int main ( )
{
cin >> n;
int temp;
for ( int i= 0 ; i< n; i++ )
{
cin >> temp;
child. push_back ( temp) ;
}
sort ( child. begin ( ) , child. end ( ) ) ;
int kk= child. size ( ) / 2 ;
int sum1= 0 , sum2= 0 ;
for ( int i= 0 ; i< kk; i++ )
{
sum1+ = child[ i] ;
}
for ( int i= kk; i< child. size ( ) ; i++ )
{
sum2+ = child[ i] ;
}
cout << child. size ( ) - kk- kk << " " << sum2- sum1;
return 0 ;
}