8.4暑假课程
bailuoboyu · · 个人记录
错题总结
T644252 篝火问题
错误代码及注释
#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;
struct node{
long long x,y;
};
vector<node>v; // 存储所有烟雾位置
long long n,r,c;
string s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>r>>c>>s;
// 初始时刻(0,0)有烟雾
v.push_back({0,0});
for(long long i=0;i<n;i++){
bool f=0;
// 错误1:遍历所有历史烟雾点,时间复杂度O(N²),无法处理大数据
for(long long j=0;j<=i;j++){
// 根据风向移动每个烟雾点
if(s[i]=='N'){
v[j].x--;
}
else if(s[i]=='W'){
v[j].y--;
}
else if(s[i]=='S'){
v[j].x++;
}
else{
v[j].y++;
}
// 检查是否到达目标位置
if(v[j].x==r&&v[j].y==c&&f==0){
cout<<1;
f=1;
}
}
if(!f){
cout<<0;
}
// 错误2:每个时间步都添加新烟雾点,导致vector无限增长
v.push_back({0,0});
}
return 0;
}
正确代码及注释
#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记录烟雾位置,键为坐标,值为是否存在
map<pair<long long,long long>,bool>mp;
long long n,x,y,r,c; // n:时间步数,x,y:烟雾偏移量,r,c:目标位置
string s; // 风向字符串
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>r>>c>>s;
// 初始时刻(0,0)有烟雾
mp[{0,0}]=1;
for(long long i=0;i<n;i++){
// 根据风向更新烟雾偏移量
if(s[i]=='N'){
r++; x++; // 烟雾整体向北移动,目标位置相对向南
}
else if(s[i]=='W'){
c++; y++; // 烟雾整体向西移动,目标位置相对向东
}
else if(s[i]=='S'){
r--; x--; // 烟雾整体向南移动,目标位置相对向北
}
else{
c--; y--; // 烟雾整体向东移动,目标位置相对向西
}
// 记录新生成的烟雾位置(相对坐标)
mp[{x,y}]=1;
// 检查目标位置是否有烟雾
cout<<mp[{r,c}];
}
return 0;
}
T639146 落落的去的数学问题3
错误代码及注释
#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;
long long n, a[1000000], ans;
// 错误1:字符串转整数的函数实现错误
long long to_int(string s){
long long cnt = 0;
for(long long i = 0; i < s.size(); i++){
cnt += 10 << (s.size() - i - 1) * s[i]; // 位运算使用错误
}
return cnt;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
// 错误2:双重循环导致时间复杂度为O(N²),无法处理大数据
for(long long i = 1; i <= n; i++){
cin >> a[i];
for(long long j = 1; j < i; j++){
ans += to_int(to_string(a[j]) + to_string(a[i])); // 字符串拼接和转换效率低
}
}
cout << ans;
return 0;
}
正确代码及注释
#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;
const long long MOD = 998244353; // 定义模数
long long n, a[1000000], ans, s[1000000]; // n:数组长度,a:存储数组,ans:结果,s:前缀和数组
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
// 读取输入并计算前缀和
for(long long i = 1; i <= n; i++){
cin >> a[i];
s[i] = (s[i-1] + a[i]) % MOD; // 计算前缀和并取模
}
// 计算每对(i,j)的贡献
for(long long i = 1; i <= n; i++){
long long len = to_string(a[i]).size(); // 计算当前数字的位数
// 计算贡献并累加到结果中
ans = ((ans + s[i-1] * ((long long)pow(10, len) % MOD) % MOD) % MOD + (i-1) * a[i] % MOD) % MOD;
}
cout << ans;
return 0;
}
P1100 高低位交换
错误代码及注释
#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;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// 错误1:直接输出示例结果,没有实际计算
// 错误2:无法处理任意输入,只能输出固定值
cout << "249036820";
return 0;
}
正确代码及注释
#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;
unsigned int n; // 使用unsigned int确保32位无符号整数
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
// 核心算法:将低16位左移16位变成高位,高16位右移16位变成低位
// 然后相加得到交换后的结果
cout << (n << 16) + (n >> 16);
return 0;
}