init with old from practice

This commit is contained in:
xw_y_am 2019-01-15 23:58:49 +08:00
commit bcd520ef12
11 changed files with 743 additions and 0 deletions

View File

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

17
old/cpp_class/main.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "test.h"
#include <iostream>
using namespace std;
int main()
{
test t( 3 );
cout << t.getval() << endl;
t.setval( 5 );
cout << t.getval() << endl;
return 0;
}

20
old/cpp_class/test.cpp Normal file
View File

@ -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;
}

13
old/cpp_class/test.h Normal file
View File

@ -0,0 +1,13 @@
class test {
public:
test( int val );
void setval( int val );
int getval();
private:
int val;
};

55
old/pc.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
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;
}

49
old/pow_recr.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
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;
}

View File

@ -0,0 +1,96 @@
#include <stdio.h>
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;
}

39
old/reverse_number.py Normal file
View File

@ -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]

80
old/sort_prior_after.c Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
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;
}

171
old/sqlist.c Normal file
View File

@ -0,0 +1,171 @@
#include <stdio.h>
#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;
}

100
old/stamp_crop.c Normal file
View File

@ -0,0 +1,100 @@
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#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;
}