Skip to content

Instantly share code, notes, and snippets.

@deltheil
Created March 23, 2013 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deltheil/5227942 to your computer and use it in GitHub Desktop.
Save deltheil/5227942 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const char *g_progname;
const char *g_name;
struct seq {
int *vals;
int size;
int alloc;
};
#define SEQ_PUSH(_SEQ, _VAL) do { \
if ((_SEQ)->size >= (_SEQ)->alloc) { \
(_SEQ)->alloc = 2 * (_SEQ)->alloc; \
(_SEQ)->vals = realloc((_SEQ)->vals, (_SEQ)->alloc * sizeof(int)); \
} \
(_SEQ)->vals[(_SEQ)->size++] = (_VAL); \
} while (0);
#define SEQ_TAIL(_SEQ) (_SEQ)->vals[(_SEQ)->size - 1]
static int tonum(const char *name);
static struct seq *toseq(int num);
static int alt_flight_time(const struct seq *s);
static int alt_max(const struct seq *s);
int main(int argc, char **argv) {
g_progname = argv[0];
if (argc != 2) {
fprintf(stderr, "usage: %s name\n", g_progname);
exit(1);
}
g_name = argv[1];
struct seq *s = toseq(tonum(g_name));
printf("%s:\n", g_name);
printf(" temps de vol: %d\n", s->size);
printf(" temps de vol en altitude: %d\n", alt_flight_time(s));
printf(" altitude max: %d\n", alt_max(s));
free(s->vals);
free(s);
return 0;
}
static int tonum(const char *name) {
int num = 0;
while (*name != '\0') num += *(name++);
return num;
}
static struct seq *toseq(int num) {
struct seq *s = malloc(sizeof(*s));
s->vals = malloc(32*sizeof(int));
s->alloc = 32;
s->size = 0;
SEQ_PUSH(s, num);
for (;;) {
num = SEQ_TAIL(s);
if (num <= 1) break;
SEQ_PUSH(s, num % 2 ? ((num * 3 ) + 1) : (num >> 1));
}
return s;
}
static int alt_max(const struct seq *s) {
int i, max = s->vals[0];
for (i = 1; i < s->size; i++) {
if (s->vals[i] > max) max = s->vals[i];
}
return max;
}
static int alt_flight_time(const struct seq *s) {
int i, time = 1;
for (i = 1; i < s->size; i++) {
if (s->vals[i] >= s->vals[0]) time++;
else break;
}
return time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment