some programs!!!

This commit is contained in:
rogerwang2008 committed 2026-10-09 10:21:51 +08:00
1 parent 71f1022dea
commit ca6daa582c
43 files changed
+1452 -381

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
int afford_year(double salary, double price, int current_year = 1, double current_money = 0) {
if ((current_money += salary) >= price) { return current_year; }
if (current_year > 100) { return -1; }
return afford_year(salary * 1.08, price * 1.10, current_year + 1, current_money);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
double money, price;
int ans;
cin >> money >> price;
if ((ans = afford_year(money, price)) == -1) {
cout << "Forget it." << endl;
} else {
cout << ans << endl;
}
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
double get_price(int spent_money, int distance) {
double price = 0;
if (distance <= 6) {
price = 3;
} else {
price = 4;
}
distance -= 12;
if (distance > 0 && distance <= 32 - 12) { price += ceil(distance / 10.0); }
distance -= 32 - 12;
if (distance > 0) { price += ceil(distance / 20.0); }
if (spent_money >= 400) { return price; }
if (spent_money >= 150) { return price * 0.5; }
if (spent_money >= 100) { return price * 0.8; }
return price;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int spent_money, distance;
cin >> spent_money >> distance;
cout << fixed << setprecision(2) << get_price(spent_money, distance) << endl;
return 0;
}
+24
View File
@@ -0,0 +1,24 @@
#include <iostream>
#include <sstream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string token;
float coord[2];
for (int i = 0; getline(cin, token, ','); i++) {
coord[i] = stof(token);
}
if (-1 <= coord[0] && coord[0] <= 1 && -1 <= coord[1] && coord[1] <= 1) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
using namespace std;
vector<int> calc_factors( int num) {
vector<int> factors;
for (const int factor : {3,5,7}) {
if (num % factor == 0) {
factors.push_back(factor);
}
}
return factors;
}
int num;
vector<int> factors;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> num;
factors = calc_factors(num);
if (factors.empty()) {
cout << "n" << endl;
} else {
int temp = factors[factors.size() - 1];
factors.pop_back();
for (const int factor : factors) {
cout << factor << " ";
}
cout << temp << endl;
}
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
#include <iostream>
using namespace std;
constexpr int MAX_X = 15;
int n, winner;
unsigned int bucket[MAX_X + 1];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int _ = 0; _ < n; _++) {
int x;
cin >> x;
bucket[x]++;
}
winner = 0;
for (int x = 1; x <= MAX_X; x++) {
if (bucket[x] > bucket[winner]) {
winner = x;
}
}
cout << winner << endl;
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
#include <iostream>
double get_bike_time(double distance) {
return 27 + 23 + distance / 3;
}
double get_walk_time(double distance) {
return distance * 5 / 6;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int distance;
std::cin >> distance;
while (std::cin >> distance) {
double diff = get_bike_time(distance) - get_walk_time(distance);
if (diff < 1e-8 && diff > -1e-8) {
std::cout << "All" << std::endl;
} else if (diff < 0) {
std::cout << "Bike" << std::endl;
} else {
std::cout << "Walk" << std::endl;
}
}
return 0;
}
@@ -0,0 +1,47 @@
#include <iostream>
void print_filled_rectangle(int h, int w, char c) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
std::cout << c;
}
std::cout << std::endl;
}
}
void print_unfilled_rectangle(int h, int w, char c) {
for (int i = 0; i < w; i++) {
std::cout << c;
}
std::cout << std::endl;
for (int i = 0; i < h - 2; i++) {
std::cout << c;
for (int j = 0; j < w - 2; j++) {
std::cout << " ";
}
std::cout << c << std::endl;
}
for (int i = 0; i < w; i++) {
std::cout << c;
}
std::cout << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int h, w;
char c;
bool filled;
while (std::cin >> h >> w >> c >> filled) {
if (filled) {
print_filled_rectangle(h, w, c);
} else {
print_unfilled_rectangle(h, w, c);
}
}
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include <iostream>
#include <cmath>
int get_digit(int num, int digit) {
num /= static_cast<int>(std::pow(10, digit));
// std::clog << num << " is " << digit << "->" << std::endl;
return num % 10;
}
bool is_good_num(int num) {
return get_digit(num, 0)
- (get_digit(num, 3) + get_digit(num, 2) + get_digit(num, 1))
> 0;
}
int n, ans;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
for (int _ = 0; _ < n; _++) {
int num;
std::cin >> num;
ans += is_good_num(num);
}
std::cout << ans << std::endl;
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
#include <iostream>
constexpr int MAX_N = 1001;
int n;
bool lamps[MAX_N]; // 第 0 盏放弃
void simulate() {
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n; j += i) {
lamps[j] = !lamps[j];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
simulate();
for (int i = 1; i <= n; i++) {
if (!lamps[i]) {
std::cout << i << " ";
}
}
std::cout << std::endl;
return 0;
}
+54
View File
@@ -0,0 +1,54 @@
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
struct PerfectCubeNumber {
int num = 0;
std::tuple<int, int, int> children;
constexpr bool operator<(const PerfectCubeNumber& other) const {
return std::tie(num, children) < std::tie(other.num, other.children);
}
};
constexpr int MAX_N = 101;
std::vector<PerfectCubeNumber> perfect_cube_numbers;
int n;
constexpr int cubes_sum(int a, int b, int c) {
return a * a * a + b * b * b + c * c * c;
}
void init_numbers() {
for (int a = 2; a < MAX_N; a++) {
for (int b = a; b < MAX_N; b++) {
for (int c = b; ; c++) {
const int x_cube = cubes_sum(a, b, c);
if (x_cube > MAX_N * MAX_N * MAX_N) { break; }
for (int x = std::floor(std::cbrt(x_cube)), i = 0; i < 3; x++, i++) {
if (x * x * x == x_cube) {
perfect_cube_numbers.push_back({x, std::tuple(a, b, c)});
}
}
}
}
}
std::sort(perfect_cube_numbers.begin(), perfect_cube_numbers.end());
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
init_numbers();
std::cin >> n;
for (const auto [num, children] : perfect_cube_numbers) {
if (num > n) { break; }
auto [a,b,c] = children;
std::cout << "Cube = " << num << ", Triple = (" << a << "," << b << "," << c << ")" << std::endl;
}
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include <iostream>
#include <array>
constexpr int MAX_N = 21;
// 编译期生成斐波那契数列
constexpr std::array<int, MAX_N> make_fib() {
std::array<int, MAX_N> fib{}; // 零初始化
fib[0] = 1;
fib[1] = 1;
for (int i = 2; i < MAX_N; ++i) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib;
}
static constexpr auto fib = make_fib();
int x;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> x;
while (std::cin >> x) {
std::cout << fib[x - 1] << std::endl;
}
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
/*
* 本来想打表的,打着打着发现了一些数学规律。具体怎么证明嘛……🤷‍
*/
#include <iostream>
#include <cmath>
constexpr size_t MAX_N = 10001;
int get_ans(int n) {
const int k = ceil(-0.5 + 0.5 * sqrt(4 * n + 1));
const bool extra = k * k < n;
const int ans = k * (k - 1) + 1 + k * extra;
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::cout << get_ans(n) << std::endl;
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
def solve(n: int) -> int:
balloons = [i for i in range(1, n + 1)]
while True:
deleted_amount = 0
for x in range(1, n):
if len(balloons) <= x * x - deleted_amount - 1 or len(balloons) == 1:
break
del balloons[x * x - deleted_amount - 1]
deleted_amount += 1
if len(balloons) == 1:
break
return balloons[0]
if __name__ == '__main__':
for n in range(1, 1150):
print(n, "->", solve(n))
+29
View File
@@ -0,0 +1,29 @@
#include <cmath>
#include <iomanip>
#include <iostream>
constexpr float ALLOWED_DIFF = 1e-6;
std::tuple<int, double> sqrt_custom(double x) {
double est_prev = -1, est_curr = x;
int rounds = 0;
while (std::abs(est_curr - est_prev) > ALLOWED_DIFF) {
est_prev = est_curr;
est_curr = 0.5 * (est_prev + x / est_prev);
// est_curr = x / (2 * est_prev) + 0.5 * est_prev;
rounds++;
}
return std::make_tuple(rounds, est_curr);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int x;
while (std::cin >> x) {
auto [rounds, ans] = sqrt_custom(x);
std::cout << rounds << " " << std::fixed << std::setprecision(2) << ans << std::endl;
}
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
#include <iostream>
#include <cmath>
#include <cstring>
#include <vector>
short get_digit(int num, int digit) {
return num / static_cast<int>(pow(10, digit)) % 10;
}
std::vector<std::tuple<int, int, int>> answers;
bool digits_bucket[10];
int nums[3];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (nums[0] = 123; nums[0] <= 333; nums[0]++) {
memset(digits_bucket, false, sizeof(digits_bucket));
nums[1] = 2 * nums[0], nums[2] = 3 * nums[0];
bool success_flag = true;
for (int num : nums) {
for (int digit = 0; digit < 3; digit++) {
int x = get_digit(num, digit);
if (digits_bucket[x] || x == 0) {
success_flag = false;
break;
}
digits_bucket[x] = true;
}
if (!success_flag) { break; }
}
if (success_flag) {
answers.emplace_back(nums[0], nums[1], nums[2]);
}
}
for (const auto [a,b,c] : answers) {
std::cout << a << " " << b << " " << c << std::endl;
}
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
#include <iostream>
int ans;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int _ = 0; _ < n; _++) {
int x;
std::cin >> x;
ans ^= x;
}
std::cout << ans << std::endl;
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
#include <iostream>
#include <cmath>
int chicken_count, coroutines;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> chicken_count >> coroutines;
if (chicken_count == 1) {
std::cout << 2 << std::endl;
return 0;
}
std::cout << static_cast<int>(ceil(1.0l * chicken_count * 2 / coroutines)) << std::endl;
return 0;
}
@@ -0,0 +1,28 @@
#include <iostream>
constexpr int MAX_N = 1e5 + 1;
int calc_factors_sum(int x) {
int sum = 0, i = 1;
for (i = 1; i * i < x; i++) {
if (x % i == 0) { sum += i + x / i; }
}
if (i * i == x) { sum += i; }
return sum - x;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int a = 220; a <= n; a++) {
int b = calc_factors_sum(a);
if (b > a && b <= n && calc_factors_sum(b) == a) {
std::cout << a << " " << b << std::endl;
}
}
return 0;
}
@@ -0,0 +1,137 @@
#include <array>
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
#include <map>
#include <set>
constexpr int MAX_N = 1e5 + 1;
std::vector<int> primes;
std::array<bool, MAX_N> not_prime;
std::multiset<std::pair<long long, int>, std::greater<>> calc_times, calc_times_old;
void init_primes() {
primes.reserve(MAX_N / 10);
primes.push_back(2);
for (int i = 3; i < MAX_N; i++) {
if (!not_prime[i]) {
primes.push_back(i);
for (int x = i + i; x < MAX_N; x += i) {
not_prime[x] = true;
}
}
}
// int shit = 0;
// for (int i = 0; i < 10000000; i++) {
// shit++;
// }
}
long long calc_factors_sum(int x) {
// std::vector<int> prime_exponents(primes.size());
std::array<int, MAX_N / 10> prime_exponents{};
int prime_idx = 0, old_x = x;
while (x > 1) {
if (x % primes[prime_idx] == 0) {
x /= primes[prime_idx], prime_exponents[prime_idx]++;
} else {
prime_idx++;
}
}
long long sum = 1;
for (int i = 0; i <= prime_idx; i++) {
if (!prime_exponents[i]) { continue; }
if (prime_exponents[i] == 1) {
sum *= primes[i] + 1;
} else {
sum *= (static_cast<long long>(std::pow(primes[i], prime_exponents[i] + 1)) - 1)
/ (primes[i] - 1);
}
}
// std::clog << sum << std::endl;
return sum - old_x;
}
long long calc_factors_sum_old(int x) {
long long sum = 0;
int i = 1;
for (i = 1; i * i < x; i++) {
if (x % i == 0) { sum += i + x / i; }
}
if (i * i == x) { sum += i; }
return sum - x;
}
template <typename Func, typename... Args>
auto time_function(const std::string& name, Func&& func, Args&&... args) {
auto start = std::chrono::high_resolution_clock::now();
// 完美转发参数并调用函数
for (int i = 0; i < 100; i++) {
auto result = std::forward<Func>(func)(std::forward<Args>(args)...);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// std::clog << "[" << name << "] 耗时: "
// << duration.count() << " μs " << std::endl;
// return result; // 返回函数的返回值(如果有)
return duration.count() / 100;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// auto s1 = std::chrono::high_resolution_clock::now();
// init_primes();
// auto e1 = std::chrono::high_resolution_clock::now();
// auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(e1 - s1);
//
// std::clog << duration.count() << std::endl;
init_primes();
std::cout << "OLD" << std::endl;
for (int x = 2; x < MAX_N; x++) {
calc_times_old.insert({time_function("", calc_factors_sum_old, x), x});
}
int count = 0;
for (auto it = calc_times_old.begin(); it != calc_times_old.end() && count < 10; ++it, ++count) {
std::cout << "OLD " << it->second << "->" << it->first << std::endl;
}
std::cout << "NEW" << std::endl;
for (int x = 2; x < MAX_N; x++) {
calc_times.insert({time_function("", calc_factors_sum, x), x});
}
count = 0;
for (auto it = calc_times.begin(); it != calc_times.end() && count < 10; ++it, ++count) {
std::cout << it->second << "->" << it->first << std::endl;
}
// int n;
// std::cin >> n;
// for (int a = 220; a <= n; a++) {
// long long b = calc_factors_sum(a);
// if (b > MAX_N) { continue; }
// if (b > a && b <= n && calc_factors_sum(static_cast<int>(b)) == a) {
// std::cout << a << " " << b << std::endl;
// }
// }
return 0;
}
@@ -0,0 +1,68 @@
#include <array>
#include <iostream>
#include <vector>
#include <cmath>
#include <chrono>
constexpr int MAX_N = 1e5 + 1;
std::vector<int> primes;
std::array<bool, MAX_N> not_prime;
void init_primes() {
primes.reserve(MAX_N / 10);
primes.push_back(2);
for (int i = 3; i < MAX_N; i++) {
if (!not_prime[i]) {
primes.push_back(i);
for (int x = i + i; x < MAX_N; x += i) {
not_prime[x] = true;
}
}
}
}
long long calc_factors_sum(int x) {
std::vector<int> prime_exponents(primes.size());
int prime_idx = 0, old_x = x;
while (x > 1) {
if (x % primes[prime_idx] == 0) {
x /= primes[prime_idx], prime_exponents[prime_idx]++;
} else {
prime_idx++;
}
}
long long sum = 1;
for (int i = 0; i <= prime_idx; i++) {
if (!prime_exponents[i]) { continue; }
if (prime_exponents[i] == 1) {
sum *= primes[i] + 1;
} else {
sum *= (static_cast<long long>(std::pow(primes[i], prime_exponents[i] + 1)) - 1)
/ (primes[i] - 1);
}
}
// std::clog << sum << std::endl;
return sum - old_x;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
init_primes();
int n;
std::cin >> n;
for (int a = 220; a <= n; a++) {
long long b = calc_factors_sum(a);
if (b > MAX_N) { continue; }
if (b > a && b <= n && calc_factors_sum(static_cast<int>(b)) == a) {
std::cout << a << " " << b << std::endl;
}
}
return 0;
}
+67
View File
@@ -0,0 +1,67 @@
#include <array>
#include <iostream>
#include <vector>
class Vector2 {
public:
int x, y;
constexpr int& operator[](const std::size_t idx) {
switch (idx) {
case 0: return x;
case 1: return y;
default: throw std::invalid_argument("Index should be 0 or 1");
}
}
constexpr Vector2 operator+(const Vector2 other) const {
return Vector2{x + other.x, y + other.y};
}
constexpr Vector2 operator-() const {
return Vector2{-x, -y};
}
constexpr Vector2 operator-(const Vector2 other) const {
return *this + -other;
}
constexpr static int cross(const Vector2 a, const Vector2 b) {
return a.x * b.y - a.y * b.x;
}
};
bool is_convex(std::vector<Vector2> polygon) {
for (int i = 0; i < polygon.size() - 1; i++) {
// if (Vector2::cross(polygon[i], polygon[i + 1]) * sign <= 0) { return false; }
// Vector2 side1 = polygon[i + 1] - polygon[i];
int sign = 0;
for (int j = 0; j < polygon.size(); j++) {
if (i == j || i + 1 == j) { continue; }
if (sign == 0) {
sign = Vector2::cross(polygon[i] - polygon[i + 1], polygon[j] - polygon[i + 1]);
if (sign == 0) { return false; }
sign = sign > 0 ? 1 : -1;
continue;
}
if (Vector2::cross(polygon[i] - polygon[i + 1], polygon[j] - polygon[i + 1]) * sign <= 0) {
return false;
}
}
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::vector<Vector2> coords(4);
while (std::cin >> coords[0].x >> coords[0].y) {
for (int i = 1; i <= 3; ++i) { std::cin >> coords[i].x >> coords[i].y; }
std::cout << (is_convex(coords) ? "yes" : "no") << std::endl;
}
return 0;
}
+46
View File
@@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
constexpr int MAX_N = 10001, MAX_D = 50;
int get_digits_sum(int x) {
int sum = 0;
while (x) {
sum += x % 10;
x /= 10;
}
return sum;
}
int m, n, k;
std::vector<int> bucket[MAX_D];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
char temp_buffer;
std::cin >> m >> temp_buffer >> n >> temp_buffer >> k;
for (int x = m + 1; x < n; x++) {
int digits_sum = get_digits_sum(x);
if (digits_sum % k != 0) { continue; }
bucket[digits_sum].push_back(x);
}
for (const auto& nums : bucket) {
if (nums.empty()) { continue; }
bool first = true;
for (int num : nums) {
if (first) {
first = false;
std::cout << num;
} else {
std::cout << ',' << num;
}
}
std::cout << std::endl;
}
return 0;
}