数天前与一个学校中的朋友闲聊,对方提到了使用Hostker的经历,涉及到了一项“按CPU时间”计费的功能。 个人来说,是挺欣赏这一项收费策略的,毕竟有多少个使用虚拟主机的用户,就有多少种不同资源需求量,按照PHP对CPU资源的使用情况来计费,不仅实现公平收费,还能逼那些让资源占用多的用户占得谨慎点,比用CloudLinux的那些逼格高得多哈! 既然如此,就自己来动手实现一个。 计算CPU时间,并不难实现,Unix Like有提供这一个系统调用,所以嘛,根本不需要你自己计算…… 我所知道相关的系统调用有两个,以下是他们的函数原型: [crayon-670c4b6950b020876 […]
Category: C/CPP
HDU #1004 Let the Balloon Rise
我只是不想遍历比较那个个字符串数组而已,内存占用可能比较多,不过我相信时间绝对比遍历比较字符串数组要短,CPU占用也少。 不好Free,所以就不手动Free了……
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
#include <stdio.h> #include <string.h> #include <stdlib.h> struct balloon_index { struct balloon_index *a; struct balloon_index *b; struct balloon_index *c; struct balloon_index *d; struct balloon_index *e; struct balloon_index *f; struct balloon_index *g; struct balloon_index *h; struct balloon_index *i; struct balloon_index *j; struct balloon_index *k; struct balloon_index *l; struct balloon_index *m; struct balloon_index *n; struct balloon_index *o; struct balloon_index *p; struct balloon_index *q; struct balloon_index *r; struct balloon_index *s; struct balloon_index *t; struct balloon_index *u; struct balloon_index *v; struct balloon_index *w; struct balloon_index *x; struct balloon_index *y; struct balloon_index *z; int count; }; struct balloon_index *struct_inital(); int main() { struct balloon_index *start, *pointer1, **pointer2; int amount, i, ii, len, max_count; char color[16], max[16]; while (1) { scanf("%d", &amount); if (amount == 0) break; start = struct_inital(); max_count = 0; for (i = 1; i <= amount; i++) { scanf("%s", color); pointer1 = start; len = strlen(color); for (ii = 0; ii <= len; ii++) { switch (color[ii]) { case 'a': pointer2 = &pointer1->a; pointer1 = pointer1->a; break; case 'b': pointer2 = &pointer1->b; pointer1 = pointer1->b; break; case 'c': pointer2 = &pointer1->c; pointer1 = pointer1->c; break; case 'd': pointer2 = &pointer1->d; pointer1 = pointer1->d; break; case 'e': pointer2 = &pointer1->e; pointer1 = pointer1->e; break; case 'f': pointer2 = &pointer1->f; pointer1 = pointer1->f; break; case 'g': pointer2 = &pointer1->g; pointer1 = pointer1->g; break; case 'h': pointer2 = &pointer1->h; pointer1 = pointer1->h; break; case 'i': pointer2 = &pointer1->i; pointer1 = pointer1->i; break; case 'j': pointer2 = &pointer1->j; pointer1 = pointer1->j; break; case 'k': pointer2 = &pointer1->k; pointer1 = pointer1->k; break; case 'l': pointer2 = &pointer1->l; pointer1 = pointer1->l; break; case 'm': pointer2 = &pointer1->m; pointer1 = pointer1->m; break; case 'n': pointer2 = &pointer1->n; pointer1 = pointer1->n; break; case 'o': pointer2 = &pointer1->o; pointer1 = pointer1->o; break; case 'p': pointer2 = &pointer1->p; pointer1 = pointer1->p; break; case 'q': pointer2 = &pointer1->q; pointer1 = pointer1->q; break; case 'r': pointer2 = &pointer1->r; pointer1 = pointer1->r; break; case 's': pointer2 = &pointer1->s; pointer1 = pointer1->s; break; case 't': pointer2 = &pointer1->t; pointer1 = pointer1->t; break; case 'u': pointer2 = &pointer1->u; pointer1 = pointer1->u; break; case 'v': pointer2 = &pointer1->v; pointer1 = pointer1->v; break; case 'w': pointer2 = &pointer1->w; pointer1 = pointer1->w; break; case 'x': pointer2 = &pointer1->x; pointer1 = pointer1->x; break; case 'y': pointer2 = &pointer1->y; pointer1 = pointer1->y; break; case 'z': pointer2 = &pointer1->z; pointer1 = pointer1->z; break; case '\0': pointer1->count++; if (pointer1->count >= max_count) { max_count = pointer1->count; strcpy(max, color); } } if (pointer1 == NULL) { *pointer2 = struct_inital(); pointer1 = *pointer2; } } } printf("%s\n", max); } return 0; } struct balloon_index *struct_inital() { struct balloon_index *index; index = (struct balloon_index *) malloc(sizeof(struct balloon_index)); index->a = index->b = index->c = index->d = index->e = index->f = index->g = index->h = index->i = index->j = index->k = index->l = index->m = index->n = index->o = index->p = index->q = index->r = index->s = index->t = index->u = index->v = index->w = index->x = index->y = index->z = NULL; index->count = 0; return index; } |
C float类型精确到N位小数的值的比较
博客更换域名后的第一篇文章。 本周,程序设计基础老师布置了一个上机作业,其中一项是一次生成两个零到九的随机数与加减乘除中的一个运算符号,输出两个数的运算式子,让用户输入运算结果,再把用户输入的值与程序运算的结果比较,从而判断用户运算的结果是否正确。 看到这项作业,我担心遇到无理数的时候如何处理,不可能让人也算到那么多位小数吧…… 奇葩的是,老师的给我们的演示截图中恰好没有出现除不尽的情况,不知道是不是特意的?不然我就可以看看老师是偷懒用了整数型还是有高明的算法用浮点型了。 首先想到的是:以数学解决问题 根据多年的人工计算经验,一般都是保留两位小数,因此要求用户输入的数字精确到二位 […]