ZZJC新生訓練賽第二場題解

Proaes發表於2024-10-02

先給出比賽連結:
https://ac.nowcoder.com/acm/contest/92036

A 小紅打怪

Show Code A

class Point { // 點類
public:
    int x, y;
    Point () {}
    Point (int x, int y) : x(x), y(y) {}
    Point operator+(const Point &P) const { return Point(x + P.x, y + P.y); }
};
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    map<har, point=""> mp{{'W', {-1,0}}, {'S', {1,0}}, {'A', {0,-1}}, {'D', {0,1}}};
    int n, m;
    cin >> n >> m;
    Point be;
    vector<ector> g(n + 1, vector(m + 1));
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            cin >> g[i][j];
            if (g[i][j] != '.' && g[i][j] != '*') be = {i, j};
        }
    }
    auto check = [&](Point p) {
        if (1 <= p.x && p.x <= n && 1 <= p.y && p.y <= m) {
            return 1;
        } else {
            return 0;
        }
    }; // 用於判斷點是否在範圍內
    int ans = 0;
    Point d = mp[g[be.x][be.y]];
    Point cur = be + d;
    while (check(cur)) {
        if (g[cur.x][cur.y] == '*') {
            ans ++;
        }
        cur = cur + d;
    }
    cout << ans << "\n";
}
<r




B 馬走日

Show Code B

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--) {
        ll n, m;
        cin >> n >> m;
        if (n == 1 || m == 1) {
            cout << 1 << "\n";
        } else if (n == 1 && m == 2) {
            cout << 1 << "\n";
        } else if (n == 2 && m == 1) {
            cout << 1 << "\n";
        } else if (n == 2 && m == 2) {
            cout << 1 << "\n";
        } else if (n == 2 && m == 3) {
            cout << 2 << "\n";
        } else if (n == 3 && m == 2) {
            cout << 2 << "\n";
        } else if (n == 3 && m == 3) {
            cout << 8 << "\n";
        } else {
            ll ans;
            if (n == 2) {
                ans = (m + 1) / 2;
            } else if (m == 2) {
                ans = (n + 1) / 2;
            } else {
                ans = n * m;
            }
            cout << ans << "\n";
        }
    }
}




C 撿石頭

Show Code C

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    ll n, m;
    cin >> n >> m; 
    if (n % (m + 1) == 0) {
        cout << "second\n";
    } else {
        cout << "first\n";
    }
}




D 小紅進地下城

Show Code D

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    string s1 , s2 ;
    cin >> s1 >> s2 ;
    if (s1 == s2) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}




F 小紅的字串生成

Show Code F

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    string s1, s2;
    cin >> s1 >> s2;
    if (s1 == s2) {
        cout << 2 << "\n";
        cout << s1 << "\n";
        cout << s1 + s2 << "\n";
    } else {
        cout << 4 << "\n";
        cout << s1 << "\n";
        cout << s2 + s1 << "\n";
        cout << s1 + s2 << "\n";
        cout << s2 << "\n";
    }
}




G 小紅的字串中值

Show Code G

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    char chr;
    cin >> n >> chr;
    string s;
    cin >> s;
    ll ans = 0;
    for (int i = 0; i < n; ++ i) {
        if (s[i] == chr) {
            ll left = i + 1;
            ll right = n - i;
            ans += min(left, right);
        }
    }
    cout << ans << "\n";
}




H [NOIP2015]掃雷遊戲

Show Code H

class Point { // 點類
public:
    int x, y;
    Point () {}
    Point (int x, int y) : x(x), y(y) {}
    Point operator+(const Point &P) const { return Point(x + P.x, y + P.y); }
};
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<ector> g(n + 1, vector(m + 1));
    vector<ector> dp(n + 1, vector(m + 1)); // dp[i][j] 表示(i, j)周圍有幾個雷
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            cin >> g[i][j];
        }
    }
    auto check = [&](Point p) {
        if (1 <= p.x && p.x <= n && 1 <= p.y && p.y <= m) {
            return 1;
        } else {
            return 0;
        }
    }; // 用於判斷點是否在範圍內
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            if (g[i][j] == '*') {
                dp[i][j] = -1;
                for (int k1 = i - 1; k1 <= i + 1; ++ k1) {
                    for (int k2 = j - 1; k2 <= j + 1; ++ k2) {
                        if (check({k1, k2}) && (k1 != i || k2 != j) && g[k1][k2] != '*') {
                            dp[k1][k2]++;
                        }
                    }
                }
            } 
        }
    }
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            if (dp[i][j] == -1) {
                cout << '*';
            } else {
                cout << dp[i][j];
            }
        }
        cout << "\n";
    }
}
<or<r




I 可程式設計拖拉機比賽

Show Code I

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= 3; i ++) {
        ans += (n * i + 9) / 10 - n * i / 10;
        cout << ans << " ";
    }
}




J 數圈圈

Show Code A

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    map<har, ll=""> mp{{'0', 1}, {'4', 1}, {'6', 1}, {'8', 2}, {'9', 1}};
    int N = 1e6;
    vector a(N + 1); // a[i] i這個數有幾個圈
    vector sa(N + 1); // sa[i] 1~i這些數有幾個圈
    for (int i = 1; i <= N; ++ i) {
        string s = to_string(i);
        for (auto si : s) {
            sa[i] += mp[si];
        }
        sa[i] += sa[i - 1];
    }
    int tt = 1;
    cin >> tt;
    while (tt--) {
        int a, b;
        cin >> a >> b;
        cout << sa[b] - sa[a - 1] << "\n";
    }
}
<har,>




K

Show Code A

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    cout << (10 - n % 10) % 10 << "\n";
}




相關文章