This commit is contained in:
Bluemangoo 2025-04-21 20:10:45 +08:00
parent 849819dff3
commit 9a7e511c12
Signed by: Bluemangoo
GPG Key ID: F2F7E46880A1C4CF
7 changed files with 209 additions and 7 deletions

View File

@ -1,7 +1,11 @@
#ifndef bool
#define bool int #define bool int
#define true 1 #define true 1
#define false 0 #define false 0
#endif
#ifndef _template
#define _template
#define _template_func_def(T, func) T_DEFINE_##func(T) #define _template_func_def(T, func) T_DEFINE_##func(T)
#define _template_func(T, func) T_##func(T) #define _template_func(T, func) T_##func(T)
#define _template_func_call(T, func, ...) \ #define _template_func_call(T, func, ...) \
@ -9,19 +13,25 @@
#define $fd _template_func_def #define $fd _template_func_def
#define $f _template_func #define $f _template_func
#define $fc _template_func_call #define $fc _template_func_call
#endif
#include <stdio.h> #ifndef T_swap
#include <stdlib.h> #define T_swap__(size, a, b) \
#include <string.h>
#define SORT_PREDICATE(type, name) \
bool name(const size_t size, type *a, type *b)
#define SORT_SWAP(size, a, b) \
{ \ { \
void *INBLOCK_TMP = malloc(size); \ void *INBLOCK_TMP = malloc(size); \
memcpy(INBLOCK_TMP, a, size); \ memcpy(INBLOCK_TMP, a, size); \
memcpy(a, b, size); \ memcpy(a, b, size); \
memcpy(b, INBLOCK_TMP, size); \ memcpy(b, INBLOCK_TMP, size); \
} }
#define T_swap(T) T_swap__
#endif
#ifndef T_sort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SORT_PREDICATE(type, name) \
bool name(const size_t size, type *a, type *b)
int memcmp_reverse(const void *ptr1, const void *ptr2, const size_t size) { int memcmp_reverse(const void *ptr1, const void *ptr2, const size_t size) {
size_t i = size; size_t i = size;
@ -70,23 +80,29 @@ void T_sort__(const size_t size, void *start, const void *end,
for (i = (size_t)start; i < (size_t)end; i += size) { for (i = (size_t)start; i < (size_t)end; i += size) {
for (j = i + size; j < (size_t)end; j += size) { for (j = i + size; j < (size_t)end; j += size) {
if (predicate(size, (void *)i, (void *)j)) { if (predicate(size, (void *)i, (void *)j)) {
SORT_SWAP(size, (void *)i, (void *)j); $f(_, swap)(size, (void *)i, (void *)j);
} }
} }
} }
} }
#define T_sort(T) T_sort__ #define T_sort(T) T_sort__
void T_merge_sort__(){
}
void T_sort_default__(const size_t size, void *start, const void *end) { void T_sort_default__(const size_t size, void *start, const void *end) {
T_sort__(size, start, end, T_less(T)); T_sort__(size, start, end, T_less(T));
} }
#define T_sort_default(T) T_sort_default__ #define T_sort_default(T) T_sort_default__
#endif
#ifndef for_i
#define for_i(i, range, inner) \ #define for_i(i, range, inner) \
{ \ { \
int i; \ int i; \
for (i = 0; i < range; i++) \ for (i = 0; i < range; i++) \
inner; \ inner; \
} }
#endif
int main() { int main() {
int n, a[301] = {0}; int n, a[301] = {0};

26
0401/10.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
int search(int arr[], int value, int l, int r) {
int mid = (l + r) / 2;
if (l > r) {
return -1;
}
if (arr[mid] == value) {
return mid;
} else if (arr[mid] < value) {
return search(arr, value, mid + 1, r);
} else {
return search(arr, value, l, mid - 1);
}
}
int main() {
int arr[1005], n, i, value;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
scanf("%d", &value);
printf("%d\n", search(arr, value, 0, n - 1));
return 0;
}

16
0401/8.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
scanf("%d", &num);
printf("%d\n", factorial(num));
return 0;
}

26
0401/9.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
struct _Pair {
int x;
int y;
};
typedef struct _Pair Pair;
Pair fun(Pair p){
Pair q;
q.x = p.y;
q.y=p.x%p.y;
if (q.y!=0){
return fun(q);
}
return q;
}
void fake_fn(){}
int main() {
Pair p;
scanf("%d %d", &p.x, &p.y);
printf("%d\n", fun(p).x);
return 0;
}

29
0417/1.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#define INT32_MIN (-2147483647 - 1)
#define for_i(i, range, inner) \
{ \
int i; \
for (i = 0; i < range; i++) \
inner; \
}
typedef struct {
int x;
int y;
int z;
} Point3D;
int main() {
Point3D max = {INT32_MIN, INT32_MIN, INT32_MIN};
Point3D input;
int n = 0;
scanf("%d", &n);
for_i(i, n, {
scanf("%d %d %d", &input.x, &input.y, &input.z);
if (input.z > max.z) {
max = input;
}
});
printf("%d %d %d\n", max.x, max.y, max.z);
return 0;
}

19
0417/2.c Normal file
View File

@ -0,0 +1,19 @@
#include <string.h>
#include <stdio.h>
typedef struct {
char areaCode[10];
char exchangeCode[40];
} PhoneNumber;
int main() {
PhoneNumber a, b;
scanf("%s %s", a.areaCode, a.exchangeCode);
scanf("%s %s", b.areaCode, b.exchangeCode);
if (strcmp(a.areaCode, b.areaCode) == 0) {
printf("%s", b.exchangeCode);
} else {
printf("%s%s", b.areaCode, b.exchangeCode);
}
return 0;
}

70
0417/3.c Normal file
View File

@ -0,0 +1,70 @@
#include <stdio.h>
#define for_i(i, range, inner) \
{ \
int i; \
for (i = 0; i < range; i++) \
inner; \
}
typedef struct {
int year;
int month;
int day;
} Date;
int is_leap_year(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
int get_day_of_month(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return is_leap_year(year) ? 29 : 28;
default:
return -1;
}
}
int main() {
Date date1, date2;
int diff = 0;
scanf("%d %d %d", &date1.year, &date1.month, &date1.day);
scanf("%d %d %d", &date2.year, &date2.month, &date2.day);
for_i(i, date1.month - 1, { diff += get_day_of_month(date1.year, i + 1); })
diff += date1.day - 1;
for_i(i, date2.month - 1, { diff -= get_day_of_month(date2.year, i + 1); })
diff -= date2.day - 1;
{
int year_max = date1.year > date2.year ? date1.year : date2.year;
int year_min = date1.year < date2.year ? date1.year : date2.year;
int sign = date1.year > date2.year ? 1 : -1;
int i;
for (i = year_min; i < year_max; i++) {
diff += sign * (is_leap_year(i) ? 366 : 365);
}
}
if (diff < 0) {
diff = -diff;
}
printf("%d\n", diff);
return 0;
}