Skip to content

Instantly share code, notes, and snippets.

@pallove
pallove / another_plus_puzzle.c
Last active October 28, 2017 00:55
计算123456789,所有3位数加法的组合。看了Milo Yip大神的知乎答案,赶忙来补充一下他的,以前写的可真烂。
#include <stdio.h>
static int num_ten[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 100000000
};
int get_num(n, s, e) {
int d = num_ten[9 - e];
int r = num_ten[e - s + 1];
return n / d % r;
@pallove
pallove / string_split_replace.c
Created August 16, 2014 08:17
字符窜分割与替换函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *replace(char *dst, const char *src, const char *rep_from, const char *rep_by)
{
//*dst = 0;
const char *start = src;
char *end;
size_t len = strlen(rep_from);
@pallove
pallove / strbuf.c
Created August 16, 2014 08:38 — forked from leeonix/strbuf.c
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
@pallove
pallove / pass_river_puzzle.c
Last active May 2, 2017 03:29
过河问题,可撑船三人:老人,丈夫,妻子; 乘客五位:两男孩,两女孩,一只狗; 条件:A.一条可乘两人的船; B.狗没有老人看管的情况下会咬人, C:妻子不在场,丈夫会打女孩;D.丈夫不在场,妻子会打男孩;问题:请问船过河多少次可以全员由河的左岸到达右岸?
#include <string.h>
#include <stdio.h>
typedef enum live {
placeholder,
oldman,
dog,
husband,
wife,
boy,
@pallove
pallove / print_utf8_range.c
Created August 26, 2014 03:15
打印一段utf-8字符
#include <stdio.h>
#include <string.h>
void write_value(char *c, int value, int head, int tail, int len)
{
int high = value >> 8;
int low = (1 << 8) - 1 & value;
if (len == 1) {
c[0] = low | head;
}
@pallove
pallove / helloworld.groovy
Last active September 20, 2016 06:00
groovy language exercise
import groovy.transform.Field;
@Field x = 1..5
def hello(obj) {
groovy_lang()
println "what the fuck $obj.a, $obj.b, $obj.c, $x.from, $x.to"
}
def groovy_lang() {
@pallove
pallove / minstack.c
Last active April 27, 2017 13:06
min value get in stack
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tval {
int v;
int i;
};
struct minstack {
@pallove
pallove / my_free.c
Created September 13, 2017 03:59
safe malloc and free with tag
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAGIC_TAG 0x11223344
inline static void* my_malloc(int size) {
void* ptr = malloc(size + sizeof(MAGIC_TAG));
int *p = (int *) ptr;
*p = MAGIC_TAG;
@pallove
pallove / fit_number.lua
Last active September 21, 2018 11:16
随机填充25个数到9*9格子,并且保证每行不少于一个数
local function gen()
local arr = {}
for i = 1, 9 * 9 do
arr[i] = i > 25 and 0 or i
end
return arr
end
local function pr(arr)
local r = {}
@pallove
pallove / TimerEx.ts
Created September 29, 2018 07:40
a timer class withe TypeScript
export interface TimerObj {
id : number;
func : (...params) => void;
interval : number;
loop : number;
forever : boolean;
params : Array<any>;
count : number;
finished : boolean;
call_time : number;