commit bcd520ef121f0f47b16393ab7861b8219b56c01c Author: xw_y_am Date: Tue Jan 15 23:58:49 2019 +0800 init with old from practice diff --git a/old/circularly_single_linked_list_stack.c b/old/circularly_single_linked_list_stack.c new file mode 100644 index 0000000..3041c17 --- /dev/null +++ b/old/circularly_single_linked_list_stack.c @@ -0,0 +1,103 @@ +#include +#include + + +typedef struct _st_node { + int val; + struct _st_node *next; +} st_node; + +typedef struct { + st_node *tail; +} st_stack; + + +void stack_destroy(st_stack *stack) +{ + if (stack->tail) { + st_node *p = stack->tail->next; + stack->tail->next = 0; + for (; p; p = p->next) free(p); + stack->tail = 0; + } +} + + +int stack_push(st_stack *stack, int value) +{ + st_node *p = (st_node *)malloc(sizeof (st_node)); + if (!p) return -1; + + if (stack->tail) { + p->next = stack->tail->next; + stack->tail->next = p; + } else { + p->next = p; + } + + p->val = value; + stack->tail = p; + return 0; +} + + +int stack_pop(st_stack *stack, int *value) +{ + st_node *p = stack->tail; + if (!p) return -1; + + *value = p->val; + + if (p->next == p) { + free(p); + stack->tail = 0; + } else { + while (p->next != stack->tail) p = p->next; + p->next = p->next->next; + free(stack->tail); + stack->tail = p; + } + return 0; +} + + +void stack_dump(st_stack *stack) +{ + printf("stack: ["); + if (stack->tail) { + st_node *p = stack->tail->next; + for (; p != stack->tail; p = p->next) printf("%d, ", p->val); + printf("%d]\n", p->val); + } else { + printf("]\n"); + } +} + + +int main() +{ + st_stack stack = {0}; + int get; + + stack_push(&stack, 5); + stack_push(&stack, 6); + stack_push(&stack, 7); + stack_push(&stack, 8); + stack_dump(&stack); + + stack_pop(&stack, &get); + printf("pop: %d\n", get); + stack_pop(&stack, &get); + printf("pop: %d\n", get); + stack_dump(&stack); + + stack_push(&stack, 10); + stack_push(&stack, 11); + stack_push(&stack, 12); + stack_dump(&stack); + + stack_destroy(&stack); + stack_dump(&stack); + + return 0; +} diff --git a/old/cpp_class/main.cpp b/old/cpp_class/main.cpp new file mode 100644 index 0000000..184dd0c --- /dev/null +++ b/old/cpp_class/main.cpp @@ -0,0 +1,17 @@ + +#include "test.h" +#include + +using namespace std; + +int main() +{ + test t( 3 ); + cout << t.getval() << endl; + + t.setval( 5 ); + cout << t.getval() << endl; + + return 0; +} + diff --git a/old/cpp_class/test.cpp b/old/cpp_class/test.cpp new file mode 100644 index 0000000..ffd6095 --- /dev/null +++ b/old/cpp_class/test.cpp @@ -0,0 +1,20 @@ + +#include "test.h" + +test::test( int val ) +{ + setval( val ); +} + + +void test::setval( int val ) +{ + this->val = val; +} + + +int test::getval() +{ + return this->val; +} + diff --git a/old/cpp_class/test.h b/old/cpp_class/test.h new file mode 100644 index 0000000..07efa5b --- /dev/null +++ b/old/cpp_class/test.h @@ -0,0 +1,13 @@ + +class test { + + public: + test( int val ); + void setval( int val ); + int getval(); + + private: + int val; + +}; + diff --git a/old/pc.c b/old/pc.c new file mode 100644 index 0000000..03689ba --- /dev/null +++ b/old/pc.c @@ -0,0 +1,55 @@ +#include + +long long mod_rev(long long x, long long mod) +{ + long long i = 0; + long long a = x; + long long b = mod; + long long pa[3] = {0, 1, 0}; + long long pb[3] = {0, 0, 1}; + + for (;;) { + long long q = a / b; + long long r = a % b; + pa[i] = pa[(i + 1) % 3] - q * pa[(i + 2) % 3]; + pb[i] = pb[(i + 1) % 3] - q * pb[(i + 2) % 3]; + + if (!r) { + long long rev = pa[(i + 2) % 3]; + if (0 > rev) { + rev += mod; + } + return rev; + } + + a = b; + b = r; + i = (i + 1) % 3; + } +} + + +long long calc(long long m, long long n) +{ + const long long mod = 1000000007; + long long x; + long long total = 1; + + long long d = n - m; + long long p = d < m ? d : m; + long long q = d < m ? m : d; + + for (x = 2; x <= n; x++) { + total = (total * x) % mod; + } + for (x = 2; x <= p; x++) { + long long rev = mod_rev(x, mod); + total = (total * rev) % mod; + total = (total * rev) % mod; + } + for (; x <= q; x++) { + long long rev = mod_rev(x, mod); + total = (total * rev) % mod; + } + return total; +} diff --git a/old/pow_recr.c b/old/pow_recr.c new file mode 100644 index 0000000..2ea8fd6 --- /dev/null +++ b/old/pow_recr.c @@ -0,0 +1,49 @@ + +#include + +float pow( float base, int exp ) +{ + /** + * 该函数为 base != 0, exp >=1 编写 + */ + + if ( exp == 1 ) { + return base; + } else { + return base * pow( base, exp - 1 ); + } +} + + +int main() +{ + float a; + int b; + + printf( "input a, b: " ); + scanf( "%f %d", &a, &b ); + + /** + * 默认输入没有错误,且 b >= 0 + */ + + if ( base == 0 ) { + if ( exp == 0 ) { + printf( "err to calc 0^0\n" ); + } else { + printf( "0" ); + } + } else { + if ( exp == 0 ) { + printf( "1" ); + } else if ( exp > 0 ) { + printf( "%f", exp( a, b ) ); + } else { + printf( "%f", 1 / exp( a, 0 - b ) ); + } + } + + printf( "\n" ); + return 0; +} + diff --git a/old/recurring_decimal_digit.c b/old/recurring_decimal_digit.c new file mode 100644 index 0000000..9e1e67a --- /dev/null +++ b/old/recurring_decimal_digit.c @@ -0,0 +1,96 @@ +#include + +typedef unsigned long long int ul; + + +ul gcd(ul a, ul b) +{ + if (!b) return a; + return gcd(b, a % b); +} + + +char recurring_decimal_digit(ul a, ul b, ul n) +{ + ul d = gcd(a, b); + a /= d; + b /= d; + + /* 将 + * \frac{a}{b} + * 转化为 + * \frac{a}{2^{b2} \cdot 5^{b5} \cdot b'} + * 的形式 + */ + ul b2 = 0; + ul b5 = 0; + for (; !(b % 2); b /= 2) b2 += 1; + for (; !(b % 5); b /= 5) b5 += 1; + + /* 计算 + * \frac{1}{10^\mbox{offset}} \cdot \frac{a \cdot m}{b} + * 其中 + * \mbox{offset} = \max(c2, c5) + * m = \frac{10^\mbox{offset}}{2^{b2} \cdot 5^{b5}} + * + * offset 表示原小数中前 offset 位不在循环体内 + */ + ul m = 1; + ul base = 2; + ul offset = b5; + ul exp = b5 - b2; + if (b2 > b5) { + base = 5; + offset = b2; + exp = b2 - b5; + } + while (exp--) m *= base; + + /* + * 计算 + * \frac{1}{10^\mbox{offset}} \cdot \left( \mbox{pre} + \frac{r_m \cdot r_a \mod b}{b} \right) + * 其中 + * \frac{r_m \cdot r_a \mod b}{b} + * 为纯循环小数,容易计算循环节 + */ + ul qm = m / b; + ul rm = m % b; + ul qa = a / b; + ul ra = a % b; + ul pre = (qa * b + ra) * qm + qa * rm; + + /* 若 n 在非循环节中,则直接返回 pre 中对应的数位 */ + if (n <= offset) { + n = offset - n; + while (n--) pre /= 10; + return pre % 10; + } + + n -= offset; + a = rm * ra % b; + if (!a) return 0; // 有限小数补0 + + /* 计算循环节unit,进而将 n 轮运算优化为 n % unit 轮 */ + ul unit = 1; + ul mod = 10; + for (; 1 != mod; mod = (10 * mod) % b) unit += 1; + + ul r = 0; + n %= unit; + if (!n) n = unit; + while (n--) { + a *= 10; + r = a / b; + a -= r * b; + } + + return r % 10; +} + + +int main(int argc, char **argv) +{ + if (3 > argc) return -1; + printf("%d\n", recurring_decimal_digit(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]))); + return 0; +} diff --git a/old/reverse_number.py b/old/reverse_number.py new file mode 100644 index 0000000..3aa7c0b --- /dev/null +++ b/old/reverse_number.py @@ -0,0 +1,39 @@ + +def reverse_num_as_int(x): + if x < 0: + neg = True + x = -x + else: + neg = False + + while not (x % 10): + x //= 10 + + rev = 0 + while x: + rev *= 10 + rev += x % 10 + x //= 10 + + if neg: + return -rev + else: + return rev + + +import re + +def reverse_num_as_str(s): + if s[0] == '-': + neg = True + s = s[1:] + else: + neg = False + + s = re.sub(r'^0*', '', s) + s = re.sub(r'0*$', '', s) + + if neg: + return '-' + s[::-1] + else: + return s[::-1] diff --git a/old/sort_prior_after.c b/old/sort_prior_after.c new file mode 100644 index 0000000..f6bb0f0 --- /dev/null +++ b/old/sort_prior_after.c @@ -0,0 +1,80 @@ +#include +#include + +int of_prior(char ch) { return ch >= 'a' && ch <= 'z'; }; +int of_after(char ch) { return ch >= 'A' && ch <= 'Z'; }; + + +char *find_end(int (*of)(char), char *s, char *e) +{ + char *p = s; + int l = e - s; + while (l && of(*p)) l--, p++; + if (l) return p; + return e; +} + + +void shift(char *s, char *e) +{ + e -= 1; + for (; s < e; s++, e--) { + char t = *e; + *e = *s; + *s = t; + } +} + + +int sort_cap(char *s, char *e) +{ + s = find_end(of_prior, s, e); + if (s == e) return -1; + for (;;) { + char *m = find_end(of_after, s, e); + if (m == e) return 0; + char *n = find_end(of_prior, m, e); + shift(s, m); + shift(m, n); + shift(s, n); + s += n - m; + } +} + + +void direct_method(char *s, char *e) +{ + char *cap = e; + char *p = e - 1; + for (; p >= s; p--) { + if (of_after(*p)) { + char *q = p; + for (; q < cap - 1; q++) { + char t = *q; + *q = q[1]; + q[1] = t; + } + cap -= 1; + } + } +} + + +void dual_print(char *str, int len) +{ + int i; + for (i = 0; i < len; i++) if (of_prior(str[i])) putchar(str[i]); + for (i = 0; i < len; i++) if (of_after(str[i])) putchar(str[i]); + printf("\n"); +} + + +int main() +{ + char buf[] = "AklBiCeilD"; + //direct_method(buf, buf + strlen(buf)); + sort_cap(buf, buf + strlen(buf)); + printf("%s\n", buf); + //dual_print(buf, strlen(buf)); + return 0; +} diff --git a/old/sqlist.c b/old/sqlist.c new file mode 100644 index 0000000..9e2eea3 --- /dev/null +++ b/old/sqlist.c @@ -0,0 +1,171 @@ +#include + +#define MAXSIZE 30 + +struct _st_list { + int sqlist[MAXSIZE]; + int size; +}; + +struct _st_list a = {{0, }, 0}; + + +static inline int *_get_head(struct _st_list *p) +{ + return p->sqlist; +} + +static inline int *_get_tail(struct _st_list *p) +{ + return p->sqlist + p->size; +} + + +void show(struct _st_list *p) +{ + int *ptr = _get_head(p); + int *end = _get_tail(p); + printf("%d:\n", p->size); + for (; ptr < end; ptr++) { + printf("%d ", *ptr); + } + printf("\n"); +} + + +void insert(struct _st_list *p, int i, int x) +{ + if (p->size == MAXSIZE) return; + if (i < 0) return; + if (i > p->size) return; + int *ptr = _get_tail(p); + int *end = _get_head(p) + i; + for (; ptr > end; ptr--) { + *ptr = *(ptr - 1); + } + *ptr = x; + p->size += 1; +} + + +int delete(struct _st_list *p, int i) +{ + if (p->size == 0) return 0; + if (i < 0) return 0; + if (i >= p->size) return 0; + int *ptr = _get_head(p) + i; + int *end = _get_tail(p); + int del = *ptr; + for (; ptr < end; ptr++) { + *ptr = *(ptr + 1); + } + p->size -= 1; + return del; +} + + +static int _more_than(int a, int b) { return a > b; } +static int _less_than(int a, int b) { return a < b; } + +static int _trace_pick(struct _st_list *p, int (*match)(int, int)) +{ + if (!p->size) return 0; + int *ptr = _get_head(p); + int *end = _get_tail(p); + int fetch = *ptr++; + for (; ptr < end; ptr++) { + if (match(*ptr, fetch)) fetch = *ptr; + } + return fetch; +} + + +int min(struct _st_list *p) +{ + return _trace_pick(p, _less_than); +} + + +int max(struct _st_list *p) +{ + return _trace_pick(p, _more_than); +} + + +static int *_iter_bisearch(int *s, int *e, int x) +{ + if (e - s < 2) { + if (x > *s) return e; + else return s; + } + int *m = s + (e - s) / 2; + if (x > *m) return _iter_bisearch(m, e, x); + else return _iter_bisearch(s, m, x); +} + + +static int _bisearch(struct _st_list *p, int x) +{ + int *ptr = _get_head(p); + int *end = _get_tail(p); + return _iter_bisearch(ptr, end, x) - _get_head(p); +} + + +void biinsert(struct _st_list *p, int x) +{ + insert(p, _bisearch(p, x), x); +} + + +static void _iter_sort(int *s, int *e) +{ + if (e <= s) return; + + int v = *s; + int *p = s; + int *q = e; + for (;;) { + while ((p < q) && (*q >= v)) q -= 1; + *p++ = *q; + if (p >= q) { + p = q; + break; + } + while ((p < q) && (*p <= v)) p += 1; + *q-- = *p; + if (p >= q) break; + } + *p = v; + + _iter_sort(s, p - 1); + _iter_sort(p + 1, e); +} + + +void sort(struct _st_list *p) +{ + if (p->size < 2) return; + _iter_sort(_get_head(p), _get_tail(p) - 1); +} + + +int main() +{ + printf("========= start ==========\n"); + insert(&a, 0, 7); + insert(&a, 0, 6); + insert(&a, 0, 3); + insert(&a, 0, 9); + insert(&a, 0, 1); + insert(&a, 0, 4); + insert(&a, 0, 2); + insert(&a, 0, 8); + show(&a); + sort(&a); + show(&a); + biinsert(&a, 5); + show(&a); + printf("########## end ##########\n"); + return 0; +} diff --git a/old/stamp_crop.c b/old/stamp_crop.c new file mode 100644 index 0000000..abfe5d9 --- /dev/null +++ b/old/stamp_crop.c @@ -0,0 +1,100 @@ +#include +#include +#include + +#define NUM_TOTAL 12 +#define NUM_COLUMN 4 +#define NUM_CROP 5 + +typedef struct { + char crop[ NUM_CROP ]; + char flag[ NUM_CROP ]; +} st; + + +void chk( char *buf, int len ) +{ + for ( int i = 0; i < len; i++ ) { + printf( "%d ", *buf++ ); + } + printf( "\n" ); +} + + +bool next( st *g ) +{ + for ( int i = NUM_CROP - 1; i >= 0; i-- ) + if ( NUM_TOTAL - NUM_CROP > g->crop[ i ] - i ) { + g->crop[ i++ ] += 1; + + for ( ; i < NUM_CROP; i++ ) + g->crop[ i ] = g->crop[ i - 1 ] + 1; + + return true; + } + + return false; +} + + +char flag_set( st *g, int index, char target_flag ) +{ + char cur_flag = g->flag[ index ]; + g->flag[ index ] = target_flag; + if ( cur_flag ) + for ( int i = 0; i < index; i++ ) + if ( g->flag[ i ] == cur_flag ) + g->flag[ i ] = target_flag; + return 1; +} + + +bool continuity( st *g ) +{ + char base_flag = 1; + memset( g->flag, 0, NUM_CROP ); + + for ( int j = 0; j < NUM_CROP; j++ ) { + char connected = 0; + + for ( int i = 0; i < j; i++ ) { + switch ( g->crop[ j ] - g->crop[ i ] ) { + + case NUM_COLUMN: + connected += flag_set( g, j, g->flag[ i ] ); + break; + + case 1: + if ( g->crop[ j ] % NUM_COLUMN ) + connected += flag_set( g, j, g->flag[ i ] ); + break; + } + } + + if ( ! connected ) g->flag[ j ] = base_flag++; + } + + char cur_flag = g->flag[ 0 ]; + for ( int i = 1; i < NUM_CROP; i++ ) + if ( g->flag[ i ] != cur_flag ) + return false; + + return true; +} + + +int main() +{ + st g; + for ( int i = 0; i < NUM_CROP; i++ ) { + g.crop[ i ] = i; + } + + int count = 0; + do { + if ( continuity( &g ) ) count += 1; + } while ( next( &g ) ); + + printf( "%d\n", count ); + return 0; +}