Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-06-20 0:33
CARN
#define NUM_ELEMS 10000000#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"
typedef struct node {
int n;
struct node *next;
} node;
node *new(int n) {
node *l;
l = malloc(sizeof(*l));
l->n = n;
l->next = NULL;
return l;
}
int main() {
int i;
unsigned long total;
node *head;
node *tail;
head = NULL;
for (i = 0; i < NUM_ELEMS; ++i) {
if (head) {
tail = tail->next = new(i);
} else {
tail = head = new(i);
}
}
total = 0;
tail = head;
while (tail) {
total += tail->n;
tail = tail->next;
}
printf("%lu\n", total);
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"
int main() {
int *a;
int i;
size_t n;
unsigned long total;
n = 1;
a = malloc(n * sizeof(*a));
for (i = 0; i < NUM_ELEMS; ++i) {
while (n <= i) {
n *= 2;
a = realloc(a, n * sizeof(*a));
}
a[i] = i;
}
while (--i)
total += a[i];
printf("%lu\n", total);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int main(void) {
char buf[BUFSIZ] = {0};
size_t n, i, j, k;
struct { size_t size, pos; } *m;
fread(buf, 1, BUFSIZ - 1, stdin);
n = strlen(buf);
for(i = j = 0; j < n;
j += strcspn(buf + j, "\n") + 1, i++);
assert(i && (m = malloc(sizeof *m * i)));
for(j = 0; j < i; j++) {
m[j].pos = (j > 0) ? m[j-1].pos + m[j-1].size + 1 : 0;
m[j].size = strcspn(buf + m[j].pos, "\n");
}
for(j = n = 0; j < i; j++)
n = (m[j].size > n) ? m[j].size : n;
for(j = 0; j < n*i; j++) {
k = j % i;
if(k == 0 && j != 0) putchar('\n');
if(m[k].size*i > j)
putchar(buf[m[k].pos + j/i]);
else
putchar(' ');
}
putchar('\n');
free(m);
return 0;
}
#!/usr/bin/env python
NUM_ELEMS = 10000000
print sum(range(NUM_ELEMS))printf("49999995000000")
==input==> [BLACK BOX 1] ==output==> == <==output== [BLACK BOX 2] <==input==
. . . . .. . . . . . . . . . . ,.-‘”. . . . . . . . . .``~.,
. . . . . . . .. . . . . .,.-”. . . . . . . . . . . . . . . . . .“-.,
. . . . .. . . . . . ..,/. . . . . . . . . . . . . . . . . . . . . . . ”:,
. . . . . . . .. .,?. . . . . . . . . . . . . . . . . . . . . . . . . . .\,
. . . . . . . . . /. . . . . . . . . . . . . . . . . . . . . . . . . . . . ,}
. . . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . . ,:`^`.}
. . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . ,:”. . . ./
. . . . . . .?. . . __. . . . . . . . . . . . . . . . . . . . :`. . . ./
. . . . . . . /__.(. . .“~-,_. . . . . . . . . . . . . . ,:`. . . .. ./
. . . . . . /(_. . ”~,_. . . ..“~,_. . . . . . . . . .,:`. . . . _/
. . . .. .{.._$;_. . .”=,_. . . .“-,_. . . ,.-~-,}, .~”; /. .. .}
. . .. . .((. . .*~_. . . .”=-._. . .“;,,./`. . /” . . . ./. .. ../
. . . .. . .\`~,. . ..“~.,. . . . . . . . . ..`. . .}. . . . . . ../
. . . . . .(. ..`=-,,. . . .`. . . . . . . . . . . ..(. . . ;_,,-”
. . . . . ../.`~,. . ..`-.. . . . . . . . . . . . . . ..\. . /\
. . . . . . \`~.*-,. . . . . . . . . . . . . . . . . ..|,./.....\,__
,,_. . . . . }.>-._\. . . . . . . . . . . . . . . . . .|. . . . . . ..`=~-,
. .. `=~-,_\_. . . `\,. . . . . . . . . . . . . . . . .\
. . . . . . . . . .`=~-,,.\,. . . . . . . . . . . . . . . .\
. . . . . . . . . . . . . . . . `:,, . . . . . . . . . . . . . `\. . . . . . ..__
. . . . . . . . . . . . . . . . . . .`=-,. . . . . . . . . .,%`>--==``
. . . . . . . . . . . . . . . . . . . . _\. . . . . ._,-%. . . ..`
free#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"
int main() {
int *a;
int i;
size_t n;
unsigned long total;
n = 1;
a = malloc(n * sizeof(*a));
for (i = 0; i < NUM_ELEMS; ++i) {
while (n <= i) {
n *= 2;
a = realloc(a, n * sizeof(*a));
}
a[i] = i;
}
while (--i)
total += a[i];
printf("%lu\n", total);
free(a);
return 0;
}free every node.#include <stdio.h>
#include <stdlib.h>
#include "numelems.h"
typedef struct node {
int n;
struct node *next;
} node;
node *new(int n) {
node *l;
l = malloc(sizeof(*l));
l->n = n;
l->next = NULL;
return l;
}
int main() {
int i;
unsigned long total;
node *head;
node *tail;
head = NULL;
for (i = 0; i < NUM_ELEMS; ++i) {
if (head) {
tail = tail->next = new(i);
} else {
tail = head = new(i);
}
}
total = 0;
tail = head;
while (tail) {
total += tail->n;
tail = tail->next;
}
printf("%lu\n", total);
while (head) {
tail = head->next;
free(head);
head = tail;
}
return 0;
}mallocs and frees isn't BLINDING FAST?! Who would have thought?#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
SIZE = 8192
};
size_t count(const char *s, int c)
{
size_t r;
for (r = 0; *s; r += *s++ == (char) c)
;
return r;
}
size_t max_strcspn(const char *s, int c)
{
size_t max, tmp;
for (max = tmp = 0; *s; s++) {
if (*s != (char) c) {
tmp++;
continue;
}
if (tmp > max)
max = tmp;
tmp = 0;
}
return max;
}
char *fill(char *to, size_t longest, size_t lines, const char *from)
{
char *save = to;
size_t n;
for (n = longest; lines; from++)
if (*from == '\n') {
memset(to, ' ', n);
to += n;
n = longest;
lines--;
} else {
*to++ = *from;
n--;
}
return save;
}
char *readall(FILE *iop)
{
char buf[SIZE];
size_t a = 0, n;
char *r, *tmp;
for (r = 0; (n = fread(buf, 1, SIZE, iop)) == SIZE; r = tmp) {
if (!(tmp = realloc(r, a + SIZE))) {
free(r);
return 0;
}
memcpy(tmp + SIZE, buf, SIZE);
a += SIZE;
}
if (!(tmp = realloc(r, a + n + 1))) {
free(r);
return 0;
}
memcpy(tmp + a, buf, n);
tmp[a + n] = '\0';
return tmp;
}
int main(void)
{
size_t lines, longest;
char *ptr, *s;
if (!(s = readall(stdin))
|| !(ptr = malloc((lines = count(s, '\n')) *
(longest = max_strcspn(s, '\n'))))) {
free(s);
return EXIT_FAILURE;
}
fill(ptr, longest, lines, s);
for (size_t i = 0; i < longest; i++) {
for (size_t n = 0; n < lines; n++)
putchar(ptr[longest * n + i]);
puts("");
}
free(s);
free(ptr);
}\n's and the lengths of sentences that come before them, which are stored in .pos and .size correspondingly. It's basically what >>40 described, using indices instead of pointers. I could've saved myself a line, and more importantly a needless loop, by having n = m[j].size > n ? m[j].size : n; inside the second loop.(char) cast is sheisty, me thinks. Also, it'd be fun if you'd cast [code]ptr[code] to be a variable length array and used it with its indices instead of multiplying. Props for nice code.