数算!
This commit is contained in:
1 parent
ca6daa582c
commit
e0c09e0c7b
22 files changed
+1344
-105
No files matched your search
@@ -0,0 +1,35 @@
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
double calc_average(const std::vector<double>& nums) {
|
||||
return std::accumulate(nums.begin(), nums.end(), 0.0) / static_cast<double>(nums.size());
|
||||
}
|
||||
|
||||
double calc_variance(const std::vector<double>& nums) {
|
||||
double avg = calc_average(nums);
|
||||
double accumulated = std::accumulate(
|
||||
nums.begin(), nums.end(),
|
||||
0.0,
|
||||
[avg](double prev, double x) {
|
||||
return prev + (x - avg) * (x - avg);
|
||||
}
|
||||
);
|
||||
return accumulated / static_cast<double>(nums.size());
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
std::vector<double> nums(n);
|
||||
for (int i = 0; i < n; i++) { std::cin >> nums[i]; }
|
||||
|
||||
std::cout << std::fixed << std::setprecision(5)
|
||||
<< calc_average(nums) << " " << calc_variance(nums) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::string s;
|
||||
std::getline(std::cin, s);
|
||||
|
||||
int uppers_amount = 0, lowers_amount = 0, digits_amount = 0;
|
||||
|
||||
for (char c : s) {
|
||||
if (std::isupper(c)) { uppers_amount++; }
|
||||
if (std::islower(c)) { lowers_amount++; }
|
||||
if (std::isdigit(c)) { digits_amount++; }
|
||||
}
|
||||
std::cout << uppers_amount << " " << lowers_amount << " " << digits_amount << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
int x = 0, y = 0;
|
||||
|
||||
constexpr Vector2 operator+(const Vector2 other) const {
|
||||
return {x + other.x, y + other.y};
|
||||
}
|
||||
};
|
||||
|
||||
using TumorGraphLine = std::vector<unsigned short>;
|
||||
using TumorGraph = std::vector<TumorGraphLine>;
|
||||
|
||||
constexpr int THRESHOLD = 50;
|
||||
constexpr std::array<Vector2, 4> DIRS = {Vector2{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
|
||||
|
||||
bool is_border(const TumorGraph& tumor_graph, Vector2 pos) {
|
||||
if (tumor_graph[pos.x][pos.y] > THRESHOLD) { return false; }
|
||||
for (const auto dir : DIRS) {
|
||||
Vector2 new_pos = pos + dir;
|
||||
if (tumor_graph[new_pos.x][new_pos.y] > THRESHOLD) { return true; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int calc_area(const TumorGraph& tumor_graph) {
|
||||
int area = 0;
|
||||
for (const auto& line : tumor_graph) {
|
||||
for (const auto val : line) {
|
||||
if (val <= THRESHOLD) { area++; }
|
||||
}
|
||||
}
|
||||
return area;
|
||||
}
|
||||
|
||||
int calc_perimeter(const TumorGraph& tumor_graph) {
|
||||
int perimeter = 0;
|
||||
for (int i = 1; i < tumor_graph.size() - 1; i++) {
|
||||
for (int j = 1; j < tumor_graph.size() - 1; j++) {
|
||||
if (is_border(tumor_graph, {i, j})) { perimeter++; }
|
||||
}
|
||||
}
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
TumorGraph tumor_graph(n + 2, TumorGraphLine(n + 2, 127));
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
std::cin >> tumor_graph[i][j];
|
||||
}
|
||||
}
|
||||
std::cout << calc_area(tumor_graph) << ' ' << calc_perimeter(tumor_graph) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
constexpr int DAYS_OF_MONTHS[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
constexpr bool is_leap(int year) {
|
||||
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
|
||||
}
|
||||
|
||||
constexpr int get_days_of_month(int year, int month) {
|
||||
if (month == 2) { return is_leap(year) ? 29 : 28; }
|
||||
return DAYS_OF_MONTHS[month];
|
||||
}
|
||||
|
||||
constexpr int get_days_of_year(int year) {
|
||||
return is_leap(year) ? 366 : 365;
|
||||
}
|
||||
|
||||
constexpr int calc_passed_days(int year, int month, int day) {
|
||||
int passed_days = 0;
|
||||
for (int m = 1; m < month; m++) {
|
||||
passed_days += get_days_of_month(year, m);
|
||||
}
|
||||
return passed_days + day;
|
||||
}
|
||||
|
||||
constexpr int calc_remaining_days(int year, int month, int day) {
|
||||
return get_days_of_year(year) - calc_passed_days(year, month, day);
|
||||
}
|
||||
|
||||
constexpr int calc_days_delta(int year1, int month1, int day1,
|
||||
int year2, int month2, int day2) {
|
||||
int days_delta = 0;
|
||||
if (year1 == year2) {
|
||||
return calc_passed_days(year2, month2, day2) - calc_passed_days(year1, month1, day1);
|
||||
}
|
||||
for (int y = year1 + 1; y < year2; y++) {
|
||||
days_delta += get_days_of_year(y);
|
||||
}
|
||||
return days_delta
|
||||
+ calc_remaining_days(year1, month1, day1)
|
||||
+ calc_passed_days(year2, month2, day2);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int year1, month1, day1, year2, month2, day2;
|
||||
std::cin >> year1 >> month1 >> day1 >> year2 >> month2 >> day2;
|
||||
std::cout << calc_days_delta(year1, month1, day1, year2, month2, day2) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
int x = 0, y = 0;
|
||||
|
||||
constexpr Vector2 operator+(Vector2 other) const {
|
||||
return {x + other.x, y + other.y};
|
||||
}
|
||||
|
||||
constexpr Vector2 operator+=(Vector2 other) {
|
||||
x += other.x, y += other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2 rotated(bool clockwise = false) const {
|
||||
if (clockwise) {
|
||||
return {y, -x}; // (2,1) -> (1,-2)
|
||||
}
|
||||
return {-y, x}; // (2,1) -> (-1,2)
|
||||
}
|
||||
};
|
||||
|
||||
struct GridElement {
|
||||
int value = -1;
|
||||
bool wall = false;
|
||||
};
|
||||
|
||||
using Grid = std::vector<std::vector<GridElement>>;
|
||||
|
||||
Grid get_initial_grid(int h, int w) {
|
||||
Grid grid = Grid(h + 2, std::vector<GridElement>(w + 2));
|
||||
grid[0] = grid[h + 1] = std::vector<GridElement>(w + 2, {.wall = true});
|
||||
for (int i = 1; i < h + 2; i++) {
|
||||
grid[i][0].wall = grid[i][w + 1].wall = true;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
void move(const Grid& grid, Vector2& pos, Vector2& dir) {
|
||||
Vector2 new_pos = pos + dir;
|
||||
if (grid[new_pos.x][new_pos.y].wall) {
|
||||
dir = dir.rotated(true);
|
||||
}
|
||||
pos += dir;
|
||||
}
|
||||
|
||||
void iter_and_print_grid(Grid& grid) {
|
||||
Vector2 pos = {1, 1}, dir = {0, 1};
|
||||
for (int _ = 0; _ < (grid.size() - 2) * (grid[0].size() - 2); _++) {
|
||||
std::cout << grid[pos.x][pos.y].value << std::endl;
|
||||
grid[pos.x][pos.y].wall = true;
|
||||
move(grid, pos, dir);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int h, w;
|
||||
std::cin >> h >> w;
|
||||
Grid grid = get_initial_grid(h, w);
|
||||
for (int i = 1; i <= h; i++) {
|
||||
for (int j = 1; j <= w; j++) {
|
||||
std::cin >> grid[i][j].value;
|
||||
}
|
||||
}
|
||||
iter_and_print_grid(grid);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using BigBin = std::vector<int>;
|
||||
|
||||
BigBin plus(BigBin a, const BigBin& b) {
|
||||
if (a.size() < b.size()) { return plus(b, a); }
|
||||
for (int i = 0; i < b.size(); i++) {
|
||||
a[i] += b[i];
|
||||
}
|
||||
a.push_back(0);
|
||||
for (int i = 1; i < a.size(); i++) {
|
||||
if (a[i - 1] > 1) { a[i - 1] -= 2, a[i]++; }
|
||||
}
|
||||
while (a.back() == 0) { a.pop_back(); }
|
||||
return a;
|
||||
}
|
||||
|
||||
BigBin parse_string(const std::string s) {
|
||||
BigBin big_bin(s.size());
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
big_bin[i] = s[s.size() - i - 1] - '0';
|
||||
}
|
||||
return big_bin;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const BigBin& big_bin) {
|
||||
for (int i = big_bin.size() - 1; i >= 0; i--) {
|
||||
os << big_bin[i];
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::string s1, s2;
|
||||
std::cin >> n;
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
std::cin >> s1 >> s2;
|
||||
std::cout << plus(parse_string(s1), parse_string(s2)) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
constexpr int GRID_SIZE = 101;
|
||||
std::array<std::array<bool, GRID_SIZE>, GRID_SIZE> has_point, is_invalid;
|
||||
|
||||
void add_point(int x, int y) {
|
||||
has_point[x][y] = true;
|
||||
for (int i = 0; i <= x; i++) {
|
||||
for (int j = 0; j <= y; j++) {
|
||||
if (i == x && j == y) { break; }
|
||||
is_invalid[i][j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_ans() {
|
||||
bool first = true;
|
||||
for (int i = 0; i < GRID_SIZE; i++) {
|
||||
for (int j = 0; j < GRID_SIZE; j++) {
|
||||
if (has_point[i][j] && !is_invalid[i][j]) {
|
||||
if (!first) { std::cout << ','; }
|
||||
std::cout << '(' << i << ',' << j << ')';
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
int x, y;
|
||||
std::cin >> x >> y;
|
||||
add_point(x, y);
|
||||
}
|
||||
|
||||
print_ans();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
constexpr int GRID_SIZE = 101;
|
||||
std::array<std::array<bool, GRID_SIZE>, GRID_SIZE> grid;
|
||||
std::array<int, GRID_SIZE> row_maxs, col_maxs;
|
||||
bool has_0_0 = false;
|
||||
|
||||
void init_maxs() {
|
||||
for (int i = 0; i < GRID_SIZE; i++) {
|
||||
for (int j = GRID_SIZE - 1; j >= 0; j--) {
|
||||
if (grid[i][j]) {
|
||||
row_maxs[i] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < GRID_SIZE; j++) {
|
||||
for (int i = GRID_SIZE - 1; i >= 0; i--) {
|
||||
if (grid[i][j]) {
|
||||
col_maxs[j] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_ans() {
|
||||
bool first = true;
|
||||
for (int i = 0; i < GRID_SIZE; i++) {
|
||||
int j = row_maxs[i];
|
||||
if (col_maxs[j] == i) {
|
||||
if (i == 0 && j == 0 && !has_0_0) { continue; }
|
||||
if (!first) { std::cout << ','; }
|
||||
std::cout << '(' << i << ',' << j << ')';
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
for (int _ = 0; _ < n; _++) {
|
||||
int x, y;
|
||||
std::cin >> x >> y;
|
||||
grid[x][y] = true;
|
||||
if (x == 0 && y == 0) { has_0_0 = true; }
|
||||
}
|
||||
|
||||
init_maxs();
|
||||
print_ans();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
|
||||
using Coord = std::pair<int, int>;
|
||||
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
Coord top_left = {INF, INF}, bottom_right = {-INF, -INF};
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
int x;
|
||||
std::cin >> x;
|
||||
if (x != 0) { continue; }
|
||||
top_left = std::min(top_left, {i, j});
|
||||
bottom_right = std::max(bottom_right, {i, j});
|
||||
// top_left.first = std::min(top_left.first,i);
|
||||
// top_left.second = std::min(top_left.second,j);
|
||||
// bottom_right.first = std::max(bottom_right.first,i);
|
||||
// bottom_right.second = std::max(bottom_right.second,j);
|
||||
}
|
||||
}
|
||||
std::cout
|
||||
<< std::max(
|
||||
(bottom_right.first - top_left.first - 1) * (bottom_right.second - top_left.second - 1)
|
||||
, 0)
|
||||
<< std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
constexpr int AUTHORS_AMOUNT = 26;
|
||||
std::array<std::vector<int>, AUTHORS_AMOUNT> books_of_authors;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
int books_amount;
|
||||
std::cin >> books_amount;
|
||||
|
||||
for (int _ = 0; _ < books_amount; _++) {
|
||||
int book_id;
|
||||
std::string authors;
|
||||
std::cin >> book_id >> authors;
|
||||
for (char c : authors) {
|
||||
books_of_authors[c - 'A'].push_back(book_id);
|
||||
}
|
||||
}
|
||||
|
||||
auto best_author = std::max_element(books_of_authors.begin(), books_of_authors.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.size() < b.size();
|
||||
});
|
||||
std::cout << static_cast<char>(best_author - books_of_authors.begin() + 'A') << std::endl
|
||||
<< best_author->size() << std::endl;
|
||||
for (auto book : *best_author) {
|
||||
std::cout << book << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::vector<int> numbers;
|
||||
int chr;
|
||||
bool in_number = false;
|
||||
while ((chr = std::getchar()) != EOF && chr != '\n') {
|
||||
char c = static_cast<char>(chr);
|
||||
if (!isdigit(c)) {
|
||||
in_number = false;
|
||||
continue;
|
||||
}
|
||||
if (in_number) {
|
||||
numbers.back() = numbers.back() * 10 + (c - '0');
|
||||
} else {
|
||||
in_number = true;
|
||||
numbers.push_back(c - '0');
|
||||
}
|
||||
}
|
||||
std::sort(numbers.begin(), numbers.end());
|
||||
bool first_flag = true;
|
||||
for (int x : numbers) {
|
||||
if (!first_flag) { std::cout << ','; }
|
||||
std::cout << x;
|
||||
first_flag = false;
|
||||
}
|
||||
if (first_flag) {
|
||||
std::cout << 0 << std::endl;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user