今天你AC了吗?
每日两题day59
一、基础题
题目:P1421 小玉买文具 - 洛谷
思路:
涉及多种进制的题目都可以全部拆成最小的单位再处理。
代码:
cpp
#include <bits/stdc++.h>
int main() {
int a, b, sum = 0;
std::cin >> a >> b;
sum = a * 10 + b;
std::cout << (sum / 19);
return 0;
}
二、提高题
题目:P2615 [NOIP 2015 提高组] 神奇的幻方 - 洛谷
思路:
纯模拟,按题目来的写判别函数就好了
代码:
cpp
#include <bits/stdc++.h>
int n = 40, i = 1;
std::vector<std::vector<int>> a(n, std::vector<int>(n));
std::pair<int, int> next(std::pair<int, int> b) {
if (b.first == 0 && b.second != n - 1) {
return {n - 1, b.second + 1};
}
if (b.second == n - 1 && b.first != 0) {
return {b.first - 1, 0};
}
if (b.first == 0 && b.second == n - 1) {
return {1, n - 1};
}
if (b.first - 1 >= 0 && b.second + 1 < n && a[b.first - 1][b.second + 1] == 0) {
return {b.first - 1, b.second + 1};
} else {
return {b.first + 1, b.second};
}
}
int main() {
std::pair<int, int> before;
std::cin >> n;
a[0][n / 2] = 1;
before = {0, n / 2};
while (++i <= n * n) {
before = next(before);
a[before.first][before.second] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << a[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}