1048.找硬币
1063.集合相似度
Nc=两个集合中不同元素的数量,Nt=集合A的元素数量+集合B的元素数量-NC
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N= 60 ;
unordered_map < int , int > has[ N] ;
int n, k;
double check ( int a, int b) {
int diff= 0 , sum= 0 ;
for ( auto it : has[ a] )
if ( has[ b] . count ( it. first) ) diff++ ;
sum= has[ a] . size ( ) + has[ b] . size ( ) - diff;
return ( double ) diff/ sum;
}
int main ( ) {
cin>> n;
for ( int i= 1 ; i<= n; i++ ) {
int m;
cin>> m;
while ( m-- ) {
int x;
cin>> x;
has[ i] [ x] ++ ;
}
}
cin>> k;
while ( k-- ) {
int a, b;
cin>> a>> b;
double res= check ( a, b) ;
printf ( "%.1lf%%\n" , res* 100 ) ;
}
}
1120.朋友数
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
int count ( int x) {
int res= 0 ;
while ( x) res+ = x% 10 , x/ = 10 ;
return res;
}
int main ( ) {
int n;
cin>> n;
set< int > S;
while ( n-- ) {
int x;
cin>> x;
S. insert ( count ( x) ) ;
}
cout<< S. size ( ) << endl;
bool is_first= true ;
for ( auto it : S) {
if ( is_first) is_first= false ;
else cout<< " " ;
cout<< it;
}
}
1144.漏掉的数字
#include <iostream>
#include <set>
using namespace std;
int n;
int main ( ) {
cin>> n;
set< int > S;
while ( n-- ) {
int x;
cin>> x;
if ( x> 0 ) S. insert ( x) ;
}
int min= 1 ;
for ( auto x : S) {
if ( x== min) min++ ;
else break ;
}
cout<< min;
}
1149.危险品装箱
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int N= 1e4 + 10 ;
int n, m;
int a[ N] , b[ N] ;
bool check ( unordered_set< int > S) {
for ( int i= 0 ; i< n; i++ ) {
if ( S. count ( a[ i] ) && S. count ( b[ i] ) ) return false ;
}
return true ;
}
int main ( ) {
cin>> n>> m;
for ( int i= 0 ; i< n; i++ ) cin>> a[ i] >> b[ i] ;
while ( m-- ) {
int k;
cin>> k;
unordered_set< int > S;
while ( k-- ) {
int x;
cin>> x;
S. insert ( x) ;
}
if ( check ( S) ) cout<< "Yes" << endl;
else cout<< "No" << endl;
}
}
1078.哈希 *
本题注意二次增量探测法的定义 二次增量探测法:
哈希表长S,当前数x,则插入位置pos=(x+i*i)%S
,(i=0,1,2,3....S)
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;
const int N= 1e4 + 10 ;
int m, n;
unordered_set< int > S;
bool check ( int x) {
if ( x== 1 ) return true ;
for ( int i= 2 ; i<= x/ i; i++ ) {
if ( x% i== 0 ) return true ;
}
return false ;
}
int find ( int x) {
for ( int i= 0 ; i< m; i++ ) {
int pos = ( x+ i* i) % m;
if ( ! S. count ( pos) ) {
S. insert ( pos) ;
return pos;
}
}
return - 1 ;
}
int main ( ) {
cin>> m>> n;
while ( check ( m) ) m++ ;
bool is_first= true ;
for ( int i= 0 ; i< n; i++ ) {
int x;
cin>> x;
int pos= find ( x) ;
if ( is_first) is_first= false ;
else cout<< " " ;
if ( pos== - 1 ) cout<< "-" ;
else cout<< pos;
}
}
1137.期终成绩
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
struct Student {
string id;
int p, m, f, s;
Student ( ) : p ( - 1 ) , m ( - 1 ) , f ( - 1 ) , s ( 0 ) { }
void cal ( ) {
if ( f>= m) s= f;
else s= round ( m* 0.4 + f* 0.6 ) ;
}
bool operator < ( const Student & t) const {
if ( s!= t. s) return s> t. s;
else return id< t. id;
}
} ;
int main ( ) {
int P, M, N;
cin>> P>> M>> N;
string id;
int score;
unordered_map< string, Student> has;
for ( int i= 0 ; i< P; i++ ) {
cin>> id>> score;
has[ id] . id= id;
has[ id] . p= score;
}
for ( int i= 0 ; i< M; i++ ) {
cin>> id>> score;
has[ id] . id= id;
has[ id] . m= score;
}
for ( int i= 0 ; i< N; i++ ) {
cin>> id>> score;
has[ id] . id= id;
has[ id] . f= score;
}
vector< Student> res;
for ( auto item : has) {
auto stu = item. second;
stu. cal ( ) ;
if ( stu. p>= 200 && stu. s>= 60 ) res. push_back ( stu) ;
}
sort ( res. begin ( ) , res. end ( ) ) ;
for ( auto it : res) {
cout<< it. id<< " " << it. p<< " " << it. m<< " " << it. f<< " " << it. s<< endl;
}
}
1645.哈希-平均查找时间
#include <iostream>
using namespace std;
const int N = 10010 ;
int s, n, m;
int h[ N] ;
bool is_prime ( int x)
{
if ( x == 1 ) return false ;
for ( int i = 2 ; i * i <= x; i ++ )
if ( x % i == 0 )
return false ;
return true ;
}
int find ( int x, int & cnt)
{
int t = x % s;
cnt = 1 ;
for ( int k = 0 ; k < s; k ++ , cnt ++ )
{
int i = ( t + k * k) % s;
if ( ! h[ i] || h[ i] == x) return i;
}
return - 1 ;
}
int main ( )
{
cin >> s >> n >> m;
while ( ! is_prime ( s) ) s ++ ;
for ( int i = 0 ; i < n; i ++ )
{
int x, count;
cin >> x;
int t = find ( x, count) ;
if ( t == - 1 ) printf ( "%d cannot be inserted.\n" , x) ;
else h[ t] = x;
}
int cnt = 0 ;
for ( int i = 0 ; i < m; i ++ )
{
int x, count;
cin >> x;
find ( x, count) ;
cnt + = count;
}
printf ( "%.1lf\n" , ( double ) cnt / m) ;
return 0 ;
}