11.数字颠倒
# include <iostream>
# include <string>
# include <algorithm>
using namespace std;
int main ( ) {
int num;
cin>> num;
string res = to_string ( num) ;
reverse ( res. begin ( ) , res. end ( ) ) ;
cout<< res<< endl;
return 0 ;
}
12.字符串反转
# include <iostream>
# include <string>
# include <algorithm>
using namespace std;
int main ( ) {
string res;
cin>> res;
reverse ( res. begin ( ) , res. end ( ) ) ;
cout<< res<< endl;
return 0 ;
}
13.句子逆序
# include <iostream>
# include <fstream>
# include <string>
# include <algorithm>
# include <vector>
using namespace std;
void process ( string str, string& res) {
string tmp = "" ;
vector< string> vec;
int idx = 0 ;
while ( str[ idx] == ' ' ) {
idx++ ;
}
str = str. substr ( idx) ;
str += ' ' ;
for ( char c : str) {
if ( c == ' ' ) {
if ( ! tmp. empty ( ) ) {
vec. push_back ( tmp) ;
tmp. clear ( ) ;
}
}
else {
tmp += c;
}
}
reverse ( vec. begin ( ) , vec. end ( ) ) ;
for ( string s : vec) {
res += s;
res += " " ;
}
res. pop_back ( ) ;
}
int main ( ) {
string str = "" ;
getline ( cin, str) ;
string res = "" ;
process ( str, res) ;
cout<< res<< endl;
return 0 ;
}
14.字符串排序
# include <iostream>
# include <string>
# include <algorithm>
# include <vector>
using namespace std;
int main ( ) {
int n = 0 ;
cin>> n;
vector< string> vec;
while ( n-- ) {
string str;
cin>> str;
vec. push_back ( str) ;
}
sort ( vec. begin ( ) , vec. end ( ) ) ;
for ( int i = 0 ; i < vec. size ( ) ; i++ ) {
cout<< vec[ i] << endl;
}
return 0 ;
}
15.求int型正整数在内存中存储时1的个数
# include <iostream>
# include <string>
# include <algorithm>
using namespace std;
void process ( int num, int & res) {
while ( num != 0 ) {
res++ ;
num &= ( num - 1 ) ;
}
}
int main ( ) {
int num = 0 ;
cin>> num;
int res = 0 ;
process ( num, res) ;
cout<< res<< endl;
return 0 ;
}
16.购物单
# include <iostream>
# include <vector>
# define max ( x, y) ( x) > ( y) ? ( x) : ( y)
using namespace std;
int getMax ( int x, int y) {
return ( x > y ? x : y) ;
}
int main ( ) {
int N = 0 ;
int m = 0 ;
cin>> N>> m;
int weight[ 60 ] [ 3 ] = { 0 } ;
int value[ 60 ] [ 3 ] = { 0 } ;
for ( int i = 1 ; i <= m; i++ ) {
int v = 0 ;
int p = 0 ;
int q = 0 ;
cin >> v >> p >> q;
if ( q == 0 ) {
weight[ i] [ 0 ] = v;
value[ i] [ 0 ] = v * p;
}
else {
if ( weight[ q] [ 1 ] == 0 ) {
weight[ q] [ 1 ] = v;
value[ q] [ 1 ] = v * p;
}
else {
weight[ q] [ 2 ] = v;
value[ q] [ 2 ] = v * p;
}
}
}
vector< vector< int > > dp ( m + 1 , vector < int > ( N+ 1 , 0 ) ) ;
for ( int i = 1 ; i <= m; i++ ) {
for ( int j = 1 ; j <= N; j++ ) {
dp[ i] [ j] = dp[ i - 1 ] [ j] ;
if ( j >= weight[ i] [ 0 ] ) {
dp[ i] [ j] = getMax ( dp[ i] [ j] , dp[ i - 1 ] [ j - weight[ i] [ 0 ] ] + value[ i] [ 0 ] ) ;
}
if ( j >= weight[ i] [ 0 ] + weight[ i] [ 1 ] ) {
dp[ i] [ j] = getMax ( dp[ i] [ j] , dp[ i - 1 ] [ j - weight[ i] [ 0 ] - weight[ i] [ 1 ] ] + value[ i] [ 0 ] + value[ i] [ 1 ] ) ;
}
if ( j >= weight[ i] [ 0 ] + weight[ i] [ 2 ] ) {
dp[ i] [ j] = getMax ( dp[ i] [ j] , dp[ i - 1 ] [ j - weight[ i] [ 0 ] - weight[ i] [ 2 ] ] + value[ i] [ 0 ] + value[ i] [ 2 ] ) ;
}
if ( j >= weight[ i] [ 0 ] + weight[ i] [ 1 ] + weight[ i] [ 2 ] ) {
dp[ i] [ j] = getMax ( dp[ i] [ j] , dp[ i - 1 ] [ j - weight[ i] [ 0 ] - weight[ i] [ 1 ] - weight[ i] [ 2 ] ] + value[ i] [ 0 ] + value[ i] [ 1 ] + value[ i] [ 2 ] ) ;
}
}
}
cout << dp[ m] [ N] << endl;
return 0 ;
}
17.坐标移动
# include <bits/stdc++.h>
# include <iostream>
# include <string>
using namespace std;
unordered_map< char , pair< int , int >> m = {
{ 'A' , make_pair ( - 1 , 0 ) } ,
{ 'W' , make_pair ( 0 , 1 ) } ,
{ 'S' , make_pair ( 0 , - 1 ) } ,
{ 'D' , make_pair ( 1 , 0 ) }
} ;
void process ( string str, pair< int , int > & res) {
int divnum = 0 ;
for ( char c : str) {
if ( c == ';' ) divnum++ ;
}
string s = "" ;
stringstream iss ( str) ;
while ( divnum-- ) {
getline ( iss, s, ';' ) ;
if ( s[ 0 ] == 'A' || s[ 0 ] == 'W' || s[ 0 ] == 'S' || s[ 0 ] == 'D' ) {
string tmp = s. substr ( 1 ) ;
int num = 0 ;
for ( char c : tmp) {
if ( c >= 'A' && c <= 'Z' ) {
num = 0 ;
break ;
}
else {
num = num * 10 + ( c - '0' ) ;
}
}
res. first += m[ s[ 0 ] ] . first * num;
res. second += m[ s[ 0 ] ] . second * num;
}
}
return ;
}
int main ( ) {
string str = "" ;
getline ( cin, str) ;
pair< int , int > res ( { 0 , 0 } ) ;
process ( str, res) ;
cout << res. first << "," << res. second << endl;
return 0 ;
}
18.识别有效的IP地址和掩码并进行分类统计
# include <iostream>
# include <string>
# include <sstream>
# include <vector>
using namespace std;
bool judgeIP ( string s) {
int count = 0 ;
stringstream iss ( s) ;
string tmp = "" ;
while ( getline ( iss, tmp, '.' ) ) {
if ( ++ count > 4 || tmp. empty ( ) || stoi ( tmp) > 255 ) {
return false ;
}
}
return count == 4 ;
}
bool isPrivate ( string s) {
stringstream iss ( s) ;
string tmp = "" ;
vector< int > vec;
while ( getline ( iss, tmp, '.' ) ) {
vec. push_back ( stoi ( tmp) ) ;
}
if ( vec[ 0 ] == 10 ) return true ;
if ( vec[ 0 ] == 172 && ( vec[ 1 ] >= 16 && vec[ 1 ] <= 31 ) ) return true ;
if ( vec[ 0 ] == 192 && vec[ 1 ] == 168 ) return true ;
return false ;
}
bool judgeMask ( string s) {
stringstream iss ( s) ;
string tmp = "" ;
unsigned int val = 0 ;
while ( getline ( iss, tmp, '.' ) ) {
val = ( val << 8 ) + stoi ( tmp) ;
}
if ( val == 0 ) return false ;
if ( ~ val + 1 == 1 ) return false ;
if ( ( val | ( val - 1 ) ) == 0xFFFFFFFF ) return true ;
return false ;
}
int main ( ) {
string str = "" ;
int a = 0 , b = 0 , c = 0 , d = 0 , e = 0 , err = 0 , p = 0 ;
while ( cin >> str) {
stringstream iss ( str) ;
string tmp = "" ;
vector< string> vec;
while ( getline ( iss, tmp, '~' ) ) {
vec. push_back ( tmp) ;
}
int first = stoi ( vec[ 0 ] . substr ( 0 , vec[ 0 ] . find_first_of ( '.' ) ) ) ;
if ( ( first != 127 ) && ( ! judgeIP ( vec[ 0 ] ) || ! judgeMask ( vec[ 1 ] ) ) ) err++ ;
else {
if ( ! judgeIP ( vec[ 0 ] ) ) err++ ;
else {
if ( isPrivate ( vec[ 0 ] ) ) p++ ;
if ( first == 0 || first == 127 ) continue ;
else if ( first >= 1 && first <= 126 ) a++ ;
else if ( first >= 128 && first <= 191 ) b++ ;
else if ( first >= 192 && first <= 223 ) c++ ;
else if ( first >= 224 && first <= 239 ) d++ ;
else if ( first >= 240 && first <= 255 ) e++ ;
}
}
}
cout << a << " " << b << " " << c << " " << d << " " << e << " " << err << " " << p << endl;
return 0 ;
}
19.简单错误记录
# include <bits/stdc++.h>
using namespace std;
int main ( ) {
string str = "" ;
unordered_map< string, int > mp;
deque< string> dq;
while ( getline ( cin, str) ) {
str = str. substr ( str. find_last_of ( '\\' ) + 1 ) ;
int spacePos = str. find_last_of ( ' ' ) ;
if ( spacePos > 16 ) {
str = str. substr ( spacePos - 16 ) ;
}
if ( mp. find ( str) == mp. end ( ) ) {
dq. push_back ( str) ;
}
mp[ str] ++ ;
if ( dq. size ( ) > 8 ) dq. pop_front ( ) ;
}
for ( auto s : dq) {
cout << s << " " << mp[ s] << endl;
}
return 0 ;
}
20.密码验证合格程序
# include <bits/stdc++.h>
using namespace std;
bool checkChar ( string str) {
int i = 0 , j = 0 , k = 0 , l = 0 ;
for ( char c : str) {
if ( isupper ( c) ) {
i = 1 ;
}
else if ( islower ( c) ) {
j = 1 ;
}
else if ( isdigit ( c) ) {
k = 1 ;
}
else {
l = 1 ;
}
}
if ( i + j + k + l >= 3 ) {
return true ;
}
return false ;
}
bool check ( string str) {
for ( int i = 0 ; i < str. size ( ) - 3 ; i++ ) {
string s = str. substr ( i, 3 ) ;
if ( str. find ( s, i + 3 ) != str. npos) {
return false ;
}
}
return true ;
}
int main ( ) {
string str = "" ;
while ( cin >> str) {
if ( str. size ( ) > 8 && checkChar ( str) && check ( str) ) {
cout << "OK" << endl;
}
else {
cout << "NG" << endl;
}
}
return 0 ;
}