错题总结
错误代码及注释
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
long long n,a[1000000 ],ans;
int main (){
cin>> n;
// 读取输入数组
for (long long i= 1 ;i<= n;i++ ){
cin>> a[i];
}
// 直接双重循环计算所有对的和的模
for (long long i= 1 ;i< n;i++ ){
for (long long j= i+ 1 ;j<= n;j++ ){
ans += (a[i]+ a[j]) % 100000000 ;
}
}
cout<< ans;
return 0 ;
}
正确代码及注释
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
long long n,a[1000000 ],sum,cnt;
int main (){
cin>> n;
// 读取输入数组
for (long long i= 1 ;i<= n;i++ ){
cin>> a[i];
}
// 对数组进行排序,这是优化算法的关键
sort (a+ 1 ,a+ 1 + n);
// 计算总和和需要调整的次数
for (long long i= 1 ;i<= n;i++ ){
// 计算所有对(i,j)中a[i]的总贡献
sum += a[i]* (n- 1 );
// 使用二分查找计算需要调整的对数
// 找到第一个不小于(1e8 - a[i])的元素位置
// 这些对的和会超过1e8,需要调整
cnt += n - (lower_bound (a+ 1 + i,a+ 1 + n,100000000 - a[i]) - a) + 1 ;
}
// 最终结果 = 总和 - 调整次数 * 1e8
cout << sum - cnt * 100000000 ;
return 0 ;
}
错误代码及注释
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
long long n,k,a;
int main (){
cin>> n>> k;
for (long long i= 1 ;i<= k;i++ ){ // 错误1:循环次数错误,应该是读取n种小球的值
cin>> a; // 错误2:只读取了一个值,覆盖了前面的输入
}
cout<< a; // 错误3:直接输出最后一个输入值,与题意完全不符
return 0 ;
}
正确代码及注释
C++ 复制 #include <bits/stdc++.h>
#define pqg (x ,y ) priority_queue< x,vector< x> ,greater< x> > y
#define pql (x ,y ) priority_queue< x,vector< x> ,less< x> > y
using namespace std ;
map<long long ,bool> mp; // 用于记录已经生成的数值,避免重复
long long n,k,a[1000000 ],cnt; // n:小球种类数,k:要找的第k小值,a:存储每种小球的值,cnt:计数器
int main (){
ios ::sync_with_stdio (false );
cin.tie (0 );
cout.tie (0 );
pqg (long long ,q); // 定义一个小根堆q,用于维护候选的最小值
cin>> n>> k;
for (long long i= 1 ;i<= n;i++ ){
cin>> a[i];
if (mp[a[i]]== 0 ){ // 如果这个值还没出现过
q.push (a[i]); // 加入优先队列
mp[a[i]]= 1 ; // 标记为已出现
}
}
while (q.size ()){
long long x= q.top (); // 取出当前最小值
cnt++ ; // 计数器加1
q.pop (); // 从队列中移除
if (cnt== k){ // 如果找到第k小值
cout<< x;
return 0 ;
}
// 生成新的候选值:当前最小值加上每种小球的值
for (long long i= 1 ;i<= n;i++ ){
if (mp[a[i]+ x]== 0 ){ // 如果新值还没出现过
q.push (a[i]+ x); // 加入优先队列
mp[a[i]+ x]= 1 ; // 标记为已出现
}
}
}
return 0 ;
}
错误代码及注释
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
long long n,m,dx[]= {1 ,0 ,- 1 ,0 },dy[]= {0 ,1 ,0 ,- 1 },ans= 1 ,cnt;
char a[1000 ][1000 ];
bool vis[1000 ][1000 ];
void dfs (long long x ,long long y ){
if (a[x][y]== ',' ){
return ;
}
for (long long i= 0 ;i< 4 ;i++ ){
long long xx= x+ dx[i],yy= y+ dy[i];
if (xx> 0 && xx<= n&& yy> 0 && yy<= m&& vis[xx][yy]== 0 ){
vis[xx][yy]= 1 ,cnt++ ;
dfs (xx,yy);
}
}
}
int main (){
cin>> n>> m;
for (long long i= 1 ;i<= n;i++ ){
for (long long j= 1 ;j<= m;j++ ){
cin>> a[i][j];
}
}
// 错误1:标记磁铁周围的格子为',',但没有正确处理边界条件
for (long long i= 1 ;i<= n;i++ ){
for (long long j= 1 ;j<= m;j++ ){
if (a[i][j]== '#' ){
for (long long k= 0 ;k< 4 ;k++ ){
a[i+ dx[k]][j+ dy[k]]= ',' ;
}
}
}
}
for (long long i= 1 ;i<= n;i++ ){
for (long long j= 1 ;j<= m;j++ ){
if (a[i][j]== '.' &&! vis[i][j]){
cnt= 1 ,vis[i][j]= 1 ;
dfs (i,j);
if (cnt> ans){
ans= cnt;
}
}
}
}
cout<< ans;
return 0 ;
}
正确代码及注释
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define endl ' \n '
typedef long long ll;
const int N = 1 e 3 + 10 ;
int n, m, a[N][N]; // n:行数,m:列数,a:格子状态数组
int ans = 1 ; // 记录最大自由度
int dx[] = {1 , - 1 , 0 , 0 }; // 四个方向的x偏移量
int dy[] = {0 , 0 , 1 , - 1 }; // 四个方向的y偏移量
int vis[N][N]; // 访问标记数组
int cnt, color; // cnt:当前连通块大小,color:当前颜色标记
// 深度优先搜索函数
void dfs (int x , int y ) {
for (int i = 0 ; i < 4 ; i++ ) {
int xx = x + dx[i], yy = y + dy[i];
// 检查边界和访问状态
if (xx< 1 || xx> n|| yy< 1 || yy> m|| vis[xx][yy]== color|| a[xx][yy]== 1 ) continue ;
// 处理被磁铁影响的格子
if (a[xx][yy]== 2 ) {
cnt++ ;
vis[xx][yy]= color;
continue ;
}
if (vis[xx][yy]!= 0 ) continue ;
// 标记访问并递归搜索
vis[xx][yy]= color;
cnt++ ;
dfs (xx, yy);
}
}
int main () {
ios ::sync_with_stdio (false );
cin.tie (0 );
cout.tie (0 );
cin >> n >> m;
// 读取输入并初始化格子状态
for (int i = 1 ; i <= n; i++ ) {
for (int j = 1 ; j <= m; j++ ) {
char c;
cin >> c;
if (c == '#' ) a[i][j] = 1 ; // 1表示有磁铁
else a[i][j] = 0 ; // 0表示空
}
}
// 标记被磁铁影响的格子(周围有磁铁的格子)
for (int i = 1 ; i <= n; i++ ) {
for (int j = 1 ; j <= m; j++ ) {
if (a[i][j]== 1 ) continue ;
if (a[i- 1 ][j]== 1 || a[i+ 1 ][j]== 1 || a[i][j- 1 ]== 1 || a[i][j+ 1 ]== 1 ) {
a[i][j] = 2 ; // 2表示被磁铁影响的格子
}
}
}
// 计算每个连通块的大小
for (int i = 1 ; i <= n; i++ ) {
for (int j = 1 ; j <= m; j++ ) {
if (vis[i][j]== 0 && a[i][j]== 0 ) {
color++ ;
cnt = 1 ;
vis[i][j] = color;
dfs (i, j);
ans = max (ans, cnt); // 更新最大自由度
}
}
}
cout << ans;
return 0 ;
}