O(∩_∩)O哈哈 ,我又回来了
这次打算做洛谷题库中的习题 (这不马上就要考C++四级了嘛)
四级大纲参考:
这次习题有点混合,大家适当食用!全部都是跟四级考点有关的!
直接进入正文——
P4325 [COCI2006-2007#1] Modulo
考点:排序+枚举
这道题十分水,直接存入set容器自动排序 (不对,我为什么会在诸位大佬面前说这种低级话语)
# include <iostream>
# include <cmath>
# include <vector>
# include <set>
# include <algorithm>
using namespace std;
typedef long long ll;
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
vector< ll> a ( 11 , 0 ) ;
set< ll> s;
for ( ll i = 1 ; i <= 10 ; i++ ) {
cin >> a[ i] ;
s. insert ( a[ i] % 42 ) ;
}
cout << s. size ( ) << endl;
return 0 ;
}
P7614 [COCI2011-2012#2] NAJBOLJIH 5
考点:枚举+排序
用pair做是不是更简单呢
# include <iostream>
# include <algorithm>
# include <cmath>
# include <vector>
using namespace std;
typedef long long ll;
pair< ll, ll> a[ 10 ] ;
inline bool cmp ( pair< ll, ll> a, pair< ll, ll> b) {
return a. second < b. second;
}
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
ll ans = 0 ;
for ( ll i = 1 ; i <= 8 ; i++ ) {
cin >> a[ i] . first;
a[ i] . second = i;
}
sort ( a + 1 , a + 9 ) ;
for ( ll i = 4 ; i <= 8 ; i++ ) {
ans += a[ i] . first;
}
cout << ans << endl;
sort ( a + 4 , a + 9 , cmp) ;
for ( ll i = 4 ; i <= 8 ; i++ ) {
cout << a[ i] . second << " " ;
}
return 0 ;
}
P2813 母舰
考点:模拟+排序+贪心
这基本上就是排序常考知识点了 (居然还是广东的)
# include <iostream>
# include <vector>
# include <cmath>
# include <algorithm>
using namespace std;
typedef long long ll;
ll m, n, s, d;
ll df[ 10000000 ] , wf[ 10000000 ] ;
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
cin >> m >> n;
for ( ll i = 1 ; i <= m; i++ ) cin >> df[ i] ;
for ( ll i = 1 ; i <= n; i++ ) cin >> wf[ i] ;
sort ( df + 1 , df + 1 + m) ;
sort ( wf + 1 , wf + 1 + n) ;
d = 1 ;
for ( ll i = 1 ; i <= n; i++ ) {
if ( df[ d] == 0 ) d++ ;
if ( df[ d] < wf[ i] && df[ d] != 0 ) {
wf[ i] = 0 ;
d++ ;
}
}
if ( d <= m) {
cout << 0 << endl;
return 0 ;
}
for ( ll i = 1 ; i <= n; i++ ) s += wf[ i] ;
cout << s << endl;
return 0 ;
}
P4305 [JLOI2011] 不重复数字
考点:排序+哈希(亿点点……)
这道题是省选题(吉林省选)不过这也太shui了吧…… 用unordered_set 即可完成去重不排序的结果!
# include <iostream>
# include <vector>
# include <unordered_set>
# include <cmath>
using namespace std;
typedef long long ll;
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
ll T;
cin >> T;
while ( T-- ) {
ll n;
cin >> n;
vector< ll> a ( n + 1 , 0 ) ;
unordered_set< ll> seen;
for ( ll i = 1 ; i <= n; i++ ) {
cin >> a[ i] ;
if ( seen. find ( a[ i] ) == seen. end ( ) ) {
cout << a[ i] << " " ;
seen. insert ( a[ i] ) ;
}
}
cout << endl;
}
return 0 ;
}
好了,排序的题目刷完了,来“吃”点“大餐”——真题 :
链接:CCF GESP C++ 四级上机题
由于一些已做过,我就只做上几道即可
B3870 [GESP202309 四级] 变长编码
考点:字符串+模拟+进制
这道题去年9月份的,虽说不难,但也不水……简直就可以概括成一句话:位操作的基本应用
# include <iostream>
# include <algorithm>
# include <vector>
# include <cmath>
using namespace std;
typedef long long ll;
ll n;
void print ( ll x) {
if ( x <= 9 ) cout << x;
else cout << char ( x - 10 + 'A' ) ;
}
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
cin >> n;
if ( n == 0 ) {
cout << "00" ;
return 0 ;
}
while ( n) {
ll t = n & 0x7f ;
n >>= 7 ;
if ( n > 0 ) t |= 0x80 ;
print ( ( t >> 4 ) & 0xf ) ;
print ( t & 0xf ) ;
cout << " " ;
}
return 0 ;
}
B3927 [GESP202312 四级] 小杨的字典
考点:字符串+模拟
纯字符串模拟题目
# include <iostream>
# include <vector>
# include <algorithm>
# include <string>
# include <map>
using namespace std;
static string fy ( const string& t, const map< string, string> & dictionary) {
auto it = dictionary. find ( t) ;
if ( it != dictionary. end ( ) ) {
return it-> second;
}
return "UNK" ;
}
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
int n;
cin >> n;
map< string, string> dictionary;
for ( int i = 1 ; i <= n; i++ ) {
string a, b;
cin >> a >> b;
dictionary[ a] = b;
}
cin. ignore ( numeric_limits < streamsize> :: max ( ) , '\n' ) ;
string c;
getline ( cin, c) ;
string t;
for ( char ch : c) {
if ( ch >= 'a' && ch <= 'z' ) {
t += ch;
}
else {
if ( ! t. empty ( ) ) {
cout << fy ( t, dictionary) ;
t. clear ( ) ;
}
cout << ch;
}
}
if ( ! t. empty ( ) ) {
cout << fy ( t, dictionary) ;
}
cout << endl;
return 0 ;
}
B4006 [GESP202406 四级] 宝箱
考点:排序+贪心
开始吓到我了,还以为是动态规划(背包),原来就是简单的贪心啊!
# include <iostream>
# include <algorithm>
# include <cmath>
# include <vector>
using namespace std;
typedef long long ll;
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
ll n, k;
cin >> n >> k;
vector< ll> a ( n + 1 , 0 ) ;
for ( ll i = 1 ; i <= n; i++ ) {
cin >> a[ i] ;
}
sort ( a. begin ( ) , a. end ( ) ) ;
ll ans = 0 ;
for ( ll i = 1 ; i <= n; i++ ) {
ll sum = 0 ;
for ( ll j = i; j >= 1 ; j-- ) {
if ( a[ i] - a[ j] <= k) {
sum += a[ j] ;
}
else break ;
}
ans = max ( ans, sum) ;
}
cout << ans << endl;
return 0 ;
}
一波AC(满屏绿):
B3959 [GESP202403 四级] 做题
考点:排序+模拟+贪心
没什么好说的(代码十分简单)
# include <iostream>
# include <algorithm>
# include <cmath>
# include <vector>
using namespace std;
typedef long long ll;
ll sum = 1 , n, a[ 1000010 ] ;
int main ( ) {
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
cin >> a[ i] ;
}
sort ( a + 1 , a + n + 1 ) ;
for ( int i = 1 ; i <= n; i++ ) {
if ( a[ i] < sum) continue ;
else sum++ ;
}
cout << sum - 1 << endl;
}
B3928 [GESP202312 四级] 田忌赛马
考点:排序+模拟+贪心
# include <iostream>
# include <vector>
# include <cmath>
# include <algorithm>
using namespace std;
typedef long long ll;
int main ( ) {
ios:: sync_with_stdio ( false ) ;
cin. tie ( nullptr ) ;
cout. tie ( nullptr ) ;
ll T;
cin >> T;
while ( T-- ) {
}
return 0 ;
}
B4005 [GESP202406 四级] 黑白方块
考点:二维数组+模拟+前缀和
居然超纲了!考的是二维数组前缀和!
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
const int MAXN = 11 ;
int n, m;
int grid[ MAXN] [ MAXN] ;
int preSum[ MAXN] [ MAXN] ;
void calculatePrefixSum ( ) {
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= m; j++ ) {
preSum[ i] [ j] = preSum[ i - 1 ] [ j] + preSum[ i] [ j - 1 ] - preSum[ i - 1 ] [ j - 1 ] + grid[ i] [ j] ;
}
}
}
int getSubmatrixSum ( int r1, int c1, int r2, int c2) {
return preSum[ r2] [ c2] - preSum[ r1 - 1 ] [ c2] - preSum[ r2] [ c1 - 1 ] + preSum[ r1 - 1 ] [ c1 - 1 ] ;
}
int main ( ) {
cin >> n >> m;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= m; j++ ) {
char c;
cin >> c;
grid[ i] [ j] = c - '0' ;
}
}
calculatePrefixSum ( ) ;
int maxBalancedSize = 0 ;
for ( int r1 = 1 ; r1 <= n; r1++ ) {
for ( int c1 = 1 ; c1 <= m; c1++ ) {
for ( int r2 = r1; r2 <= n; r2++ ) {
for ( int c2 = c1; c2 <= m; c2++ ) {
int totalCells = ( r2 - r1 + 1 ) * ( c2 - c1 + 1 ) ;
int blackCells = getSubmatrixSum ( r1, c1, r2, c2) ;
int whiteCells = totalCells - blackCells;
if ( blackCells == whiteCells) {
maxBalancedSize = max ( maxBalancedSize, totalCells) ;
}
}
}
}
}
cout << maxBalancedSize << endl;
return 0 ;
}
参考习题:
完结撒花!
做题不易,望各位大佬RP++,点个赞,关注一下吧!
再说个悄悄话 ,对了,我的粉丝破200 啦!谢谢大家一直以来对我的关照!点赞争取破500 !!!