8.18总结

· · 个人记录

U599097 四舍五入

错误原因:w没开long long 错误代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long x,k;
    cin>>x>>k;
    long long mod=1;
    for(long long i=0;i<k;i++){
        mod*=10;
        int w=x%mod;//没开long long
        x/=mod;
        if(w>=mod/2){
            x++;
        }
        x*=mod;
    }
    cout<<x;
    return 0;
} 

正确代码:

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long x, k;
    cin >> x >> k;
    long long mod = 1;
    for(long long i = 0; i < k; i++) {
        mod *= 10;//自增
        long long w = x % mod;//取余
        x /= mod;//去掉后面的数位
        if(num >= mod / 2) {//如果大于取余值的一半那么四舍五入
            x++;
        }
        x *= mod;//乘回来
    }
    cout << x;
    return 0;
}

U599136 横冲直撞

#include <bits/stdc++.h>
using namespace std;

int n, m, sx, sy, k;
map<int, vector<int>> hang, lie;

int main() {
    cin >> n >> m >> sx >> sy >> k;

    // 存储障碍物坐标
    while (k--) {
        int r, c;
        cin >> r >> c;
        hang[r].push_back(c);
        lie[c].push_back(r);
    }

    // 预处理:为每行/列添加边界并排序
    for (auto& x : hang) {
        x.second.insert(x.second.begin(), 0);
        sort(x.second.begin(), x.second.end());
        x.second.push_back(m + 1);
    }
    for (auto& x : lie) {
        x.second.insert(x.second.begin(), 0);
        sort(x.second.begin(), x.second.end());
        x.second.push_back(n + 1);
    }

    // 处理移动指令
    int q;
    cin >> q;
    while (q--) {
        char d;
        int l;
        cin >> d >> l;

        if (d == 'L' || d == 'R') {
            // 左右移动处理
            auto& row = hang[sx];
            if (row.empty()) {
                sy = (d == 'L') ? max(1, sy - l) : min(m, sy + l);
            } else {
                auto it = (d == 'L') 
                    ? lower_bound(row.begin(), row.end(), sy) - 1
                    : upper_bound(row.begin(), row.end(), sy);
                sy = (d == 'L') 
                    ? max(*it + 1, sy - l) 
                    : min(*it - 1, sy + l);
            }
        } else {
            // 上下移动处理
            auto& col = lie[sy];
            if (col.empty()) {
                sx = (d == 'U') ? max(1, sx - l) : min(n, sx + l);
            } else {
                auto it = (d == 'U')
                    ? lower_bound(col.begin(), col.end(), sx) - 1
                    : upper_bound(col.begin(), col.end(), sx);
                sx = (d == 'U')
                    ? max(*it + 1, sx - l)
                    : min(*it - 1, sx + l);
            }
        }
        cout << sx << " " << sy << endl;
    }
    return 0;
}

U599138 笔记本

#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int N=2e6;

ll tree[N], fa[N], cnt, cur; // 树结构存储
map<ll,ll> mp;               // 版本快照

int main() {
    ll Q; cin >> Q;
    while (Q--) {
        string op; ll x;
        cin >> op;

        if (op == "ADD") {       // 新增节点
            cin >> x;
            tree[++cnt]=x;
            fa[cnt]=cur;
            cur=cnt;
        }
        else if (op == "DELETE") cur=fa[cur];  // 回退节点
        else if (op == "SAVE") { cin>>x; mp[x]=cur; }  // 保存版本
        else if (op == "LOAD") { cin>>x; cur=mp[x]; }  // 加载版本

        cout << (cur ? tree[cur] : -1) << " ";  // 输出当前值
    }
    return 0;
}