A - Swapping Two Integers Read two integers, swap their values, and output them on separate lines.
B - Time Conversion Given N seconds where 0 ≤ N < 86400, convert it to 24-hour time format hh:mm:ss.
The conversion formula using modular arithmetic: [N \equiv a_0 \times 3600 + a_1 \times 60 + a_2 \times 1 \pmod{86400}]
Calculate hours, minutes, and seconds, then pad each with leading zeros to width 2:
int n;
std::cin >> n;
n %= 86400;
int parts[3] = {0};
parts[0] = n / 3600 % 24; // hours
parts[1] = n / 60 % 60; // minutes
parts[2] = n % 60; // seconds
for (int i = 0; i < 3; i++) {
std::cout << std::setw(2) << std::setfill('0') << parts[i];
if (i != 2) std::cout << ":";
}
C - Multiplication Table Input a number N representing the sum of all products from the 9×9 multiplication table, but with one pair missing. Output which pair might have been omitted, sorted lexicographically.
The complete multiplication table sum is: [\sum_{i = 1}^{9} \sum_{j = 1}^{9} i \times j = \sum_{i = 1}^{9} i \sum_{j = 1}^{9} j = \binom{10}{2} \times \binom{10}{2} = 2025]
Therefore, the missing pair satisfies: [p \times q = 2025 - N = M]
Factor M by iterating up to √M and collect valid divisor pairs within range [1, 9]. To maintain lexicographic order, add both (x, y) and (y, x) pairs.
int n;
std::cin >> n;
int product = 2025 - n;
std::vector<std::array<int, 2>> result;
for (int i = 1; i * i <= product; i++) {
if (product % i != 0) continue;
int j = product / i;
if (1 <= i && i <= 9 && 1 <= j && j <= 9)
result.push_back({i, j});
}
int sz = result.size();
for (int i = 0; i < sz; i++) {
int x = result[sz - 1 - i][0];
int y = result[sz - 1 - i][1];
if (x != y)
result.push_back({y, x});
}
for (const auto& p : result) {
std::cout << p[0] << " x " << p[1] << "\n";
}
D - Graph Center Given a simple undirected graph with n vertices and m edges, find the vertex that minimizes the maximum shortest path to all other vertices (the graph center). Constraints: 1 ≤ n ≤ 300, 1 ≤ m ≤ n(n-1)/2.
Aproach: Run Floyd-Warshall aglorithm (O(n³)) to compute all-pairs shortest paths. Then for each vertex, find the maximum distance to any other vertex and track the minimum of these maximums.
int n, m;
std::cin >> n >> m;
const i64 INF = 1LL << 60;
auto minimize = [&](i64& a, i64 b) { a = std::min(a, b); };
auto maximize = [&](i64& a, i64 b) { a = std::max(a, b); };
std::vector<std::vector<i64>> graph(n + 1, std::vector<i64>(n + 1, INF));
for (int i = 1; i <= n; i++) graph[i][i] = 0;
for (int i = 0; i < m; i++) {
int u, v, w;
std::cin >> u >> v >> w;
graph[u][v] = std::min(graph[u][v], (i64)w);
graph[v][u] = graph[u][v];
}
std::vector<std::vector<i64>> dist = graph;
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dist[i][k] != INF && dist[k][j] != INF) {
minimize(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
i64 answer = INF;
for (int i = 1; i <= n; i++) {
i64 maxDist = -INF;
for (int j = 1; j <= n; j++) {
if (i != j && dist[i][j] != INF) {
maximize(maxDist, dist[i][j]);
}
}
if (maxDist != -INF) {
minimize(answer, maxDist);
}
}
std::cout << answer << "\n";
The solution handles disconnected components by checking to reachable vertices before updating the maximum distance.