8.5

· · 个人记录

T644252 篝火问题

错误代码

#include<bits/stdc++.h>

using namespace std;

const int dx[] = {0, -1, 0, 1, 0};
const int dy[] = {0, 0, -1, 0, 1};

int n, x, y, f, flag, vis[4005][4005];
char a[4005];

int main(){
    cin >> n >> x >> y;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    y += n;
    x += n;
    vis[n][n] = 1;
    for(int i = 1; i <= n; i++){
        if(a[i] == 'N'){
            f = 1;
        }
        else if(a[i] == 'W'){
            f = 2;
        }
        else if(a[i] == 'S'){
            f = 3;
        }
        else{
            f = 4;
        }
        for(int j = 1; j <= n * 2 + 1; j++){
            for(int k = 1; k <= n * 2 + 1; k++){
                if(vis[j][k]){
                    vis[j][k]--;
                    vis[j + dx[f]][k + dy[f]]++;
                }
            }
        }
        vis[n][n] = 1;
        if(vis[x][y]){
            cout << 1;
        }
        else{
            cout << 0;
        }
    }
    return 0;
}

错误原因:骗分写错了。

正确代码

#include<bits/stdc++.h>

using namespace std;

const int dx[] = {0, 1, 0, -1, 0};
const int dy[] = {0, 0, 1, 0, -1};

long long n, x, f, y, xx, yy;
map<pair<int, int> , bool>vis;
char a[200005];

int main(){
    cin >> n >> x >> y;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    vis[{xx, yy}] = 1;
    for(int i = 1; i <= n; i++){//模拟
        if(a[i] == 'N'){//如果是风往上吹,那么人和火往下走。
            f = 1;
        }
        else if(a[i] == 'W'){//如果是风往左吹,那么人和火往右走。
            f = 2;
        }
        else if(a[i] == 'S'){//如果是风往下吹,那么人和火往上走。
            f = 3;
        }
        else{//如果是风往右吹,那么人和火往左走。
            f = 4;
        }
        x += dx[f];
        y += dy[f];
        xx += dx[f];
        yy += dy[f];
        vis[{xx, yy}] = 1;
        cout << vis[{x, y}];
    }
    return 0;
}

思路:让人和火逆着走。

T639146 落落的去的数学问题

错误代码

#include<bits/stdc++.h>

using namespace std;

long long n, a[200005], ans;

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            long long sum = a[j], pos = 1;
            while(sum){
                sum /= 10;
                pos *= 10;
            }
            ans += a[i] * pos + a[j];
        }
    }
    cout << ans;
    return 0;
}

错误原因:暴力骗分。

正确代码

#include<bits/stdc++.h>

using namespace std;

const long long mod = 998244353;

long long n, a[200005], ans, sum[200005];

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum[i] = a[i] + sum[i - 1];//求前缀和。
        sum[i] %= mod;
    }
    for(int i = 1; i <= n; i++){
        int len = to_string(a[i]).size();
        long long cnt = 1;
        for(int j = 1; j <= len; j++){
            cnt *= 10;
        }
        ans += (((sum[i - 1] * (cnt % mod))% mod) + (((i - 1) * a[i]) % mod)) % mod;//((sum[i - 1] * (cnt % mod))% mod)代表这个数这个数被前面的数被加了cnt次,((i - 1) * a[i]) % mod表示这个数这个数被加了i - 1次
        ans %= mod;
    }
    cout << ans % mod;
    return 0;
}

思路前缀和优化。

P1100 高低位交换

#include<bits/stdc++.h>

using namespace std;

long long n, ans = 1;
string s;

int main(){
    cin >> n;
    while(n){
        s += (n % 2) + '0';
        n /= 2;
    }
    for(int i = s.size(); i <= 32; i++){
        s += '0';
    }
    reverse(s.begin(), s.end());
    for(int i = 0; i < 16; i++){
        swap(s[i], s[i + 16]);
    }
    reverse(s.begin(), s.end());
    for(int i = 0; i < 32; i++){
        n += (s[i] - '0') * ans;
        ans *= 2;
    }
    cout << n;
    return 0;
}

错误原因:模拟写错了。

正确代码

#include<bits/stdc++.h>

using namespace std;

unsigned int n;

int main(){
    cin >> n;
    cout << (n << 16) + (n >> 16);
    return 0;
}

思路:位运算。