Commit c78a4b77 authored by xingqiao's avatar xingqiao
Browse files

helloworld basic code files

parent d9c004c6
#include <stdio.h>
// ADD YOUR CODES HERE
#include <stdlib.h>
typedef struct vector {
size_t size;
size_t capacity;
int *data;
} vector_t;
vector_t vector_create(void) {
vector_t a;
a.size = 0;
a.capacity = 3;
a.data = malloc(sizeof(*(a.data))*a.capacity);
return a;
}
void vector_append(vector_t *vec, int value) {
if(vec->capacity <= vec->size) {
vec->capacity *= 2;
vec->data = realloc(vec->data,sizeof(*(vec->data))*vec->capacity);
}
vec->data[vec->size] = value;
vec->size += 1;
}
void vector_destroy(vector_t *vec) {
free(vec->data);
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <max golomb number>\n", argv[0]);
return 1;
}
vector_t v = vector_create();
int max_g = atoi(argv[1]);
vector_append(&v,1);
vector_append(&v,2);
vector_append(&v,2);
if (max_g > 2)
{
for (int i = 3; i <= max_g; i++)
{
for (int j = 0; j < v.data[i-1]; j++)
{
vector_append(&v,i);
}
}
}
for(int k = v.size - 1; k >= 0; k--) {
if (v.data[k] > max_g)
{
continue;
}
printf("%d\n", v.data[k]);
}
vector_destroy(&v);
// ADD YOUR CODES HERE
return 0;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment