Pagini recente » Cod sursa (job #3155294) | Cod sursa (job #1358951) | Cod sursa (job #2505848) | Cod sursa (job #1628480) | Cod sursa (job #1923757)
#include <stdio.h>
#include <stdlib.h>
typedef struct Nod
{
int value;
struct Nod* next;
} List;
void insert(List** p, int x)
{
List* nou = (List*) malloc(sizeof(List));
nou->value = x;
nou->next = NULL;
if (*p == NULL)
{
*p = nou;
return;
}
List* q;
for(q = *p; q->next != NULL; q = q->next);
q->next = nou;
}
void delete(List** p, int x)
{
// primul element(2) nu va fi niciodata eliminat deoarece este prim
List* q, * del;
for (q = *p; q->next->value != x; q = q->next);
del = q->next;
q->next = q->next->next;
del->next = NULL;
free(del);
}
int eratostene(List** p)
{
List* par, * q;
int k = 0;
par = *p;
while (par != NULL)
{
k++;
for (q = par; q->next != NULL; )
if (q->next->value % par->value == 0)
{
delete(p, q->next->value);
}
else q = q->next;
par = par->next;
}
return k;
}
int main()
{
int n, i;
FILE * in, * out;
in = fopen("ciur.in", "rt");
fscanf(in, "%d", &n);
fclose(in);
List* p = NULL;
for (i = 2; i <= n; i++)
insert(&p, i);
out = fopen("ciur.out", "wt");
fprintf(out, "%d", eratostene(&p));
return 0;
}