-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGQ.c
133 lines (103 loc) · 2.28 KB
/
GQ.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Morteza");
MODULE_DESCRIPTION("I just done this for free condoms :)");
#define GOAL 70000000
#define CHILDEVERYMONTH 2
#define MAXWORKINGTIME 6
#define REVENUETIME 4
#define CHILDMARGIN 6
///////////
long long pop=0;
long long richpop=0;
typedef struct cell{
char time;
char childnum;
struct cell * child[MAXWORKINGTIME*CHILDEVERYMONTH];
}cell;
cell *allcell;
bool memoryallocatecells(void)
{
if ( NULL == (allcell = vmalloc((GOAL)*sizeof(cell)) )) {
printk("Not Enought Memory\n");
return true;
}
return false;
}
cell* initCell(void)
{
pop++;
cell* pcell;
pcell = &allcell[pop-1];
pcell->time=0;
pcell->childnum=0;
return pcell;
}
bool reachGoal(void)
{
if (pop>=GOAL)
return true;
return false;
}
bool addChild(cell* pcell )
{
if(pcell->childnum==MAXWORKINGTIME*CHILDEVERYMONTH)
return false;
cell* ch = initCell();
if(ch==NULL)
return true;
pcell->child[pcell->childnum] = ch;
pcell->childnum++;
if(reachGoal()) return true; else return false;
}
bool act(cell* pcell)
{
int i =0;
for(i=0;i<CHILDEVERYMONTH;i++)
if(addChild(pcell)) return true;
if(pcell->childnum>=CHILDMARGIN)
pcell->time++;
return false;
}
bool recursive(cell* pcell)
{
int i=0;
for(i=0;i<MAXWORKINGTIME*CHILDEVERYMONTH;i++)
{
if(i<pcell->childnum)
{if(recursive(pcell->child[i])) return true;}
else
{
if(act(pcell)) return true;
break;
}
}
if(pcell->time>=REVENUETIME)
richpop++;
return false;
}
///////////
static int __init GQ_init(void)
{
if(memoryallocatecells())
return 0;
cell* pcell = initCell();
int i=0;
for(;;)
{
if(recursive(pcell)) break;
printk("after %d months -> Population: %d, rich ones: %d, ratio: %d%\n",i,(int)pop,(int)richpop,(int)(100*(float)richpop/(float)pop));
i++;
}
vfree(allcell);
printk("after %d months -> Population: %d, rich ones: %d, ratio: %d%\n",i,(int)pop,(int)richpop,(int)(100*(float)richpop/(float)pop));
return 0;
}
static void __exit GQ_cleanup(void)
{
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(GQ_init);
module_exit(GQ_cleanup);