Task 4 and 6-7 are wrong

This commit is contained in:
AZEN-SGG 2025-05-20 22:06:06 +03:00
parent ed505ffc17
commit 1e17a5b2c2
3 changed files with 104 additions and 36 deletions

View file

@ -5,29 +5,48 @@
int t3_solve (node *head)
{
node *next = NULL;
node *last, *next = NULL;
int count = 0, local = 0;
if (!head)
return 0;
last = head;
head = last->next;
if (!head)
return 0;
for (next = head->next; next; next = head->next)
{
int cmp = strcmp(next->string, head->string);
if (cmp < 0) {
if (local) {
count += local;
local = 0;
}
} else if (cmp == 0) {
if (local)
local++;
} else
local = 1;
int cmp_last = strcmp(head->string, last->string);
int cmp_next = strcmp(head->string, next->string);
if (cmp_last > 0) {
local = 1;
} else if (cmp_last == 0) {
if (local) {
if (cmp_next < 0) {
count += local;
local = 0;
} else
local++;
}
} else {
count += local;
local = 0;
}
last = head;
head = next;
}
if (strcmp(last->string, head->string) >= 0)
count += local;
else
if (local >= 1)
count += local - 1;
return count;
}

View file

@ -5,36 +5,66 @@
int t4_solve (node *head)
{
int maximum = 0, count = 0, local = 0, strict = 0;
node *last, *next;
last = head;
head = last->next;
for (next = head->next; next; next = head->next)
{
int last_cmp = strcmp(head->string, last->string);
if (last_cmp == 0) {
local += (local > 0);
} else if (last_cmp < 0) {
if (strict) {
if (maximum < count)
maximum = count;
count = 0;
local = 1;
} else
local += (local > 0);
strict = 0;
} else {
count += local;
local = (local > 0);
strict = 1;
}
last = head;
head = next;
}
return maximum;
}
/*
char *last = head->string;
int maximum = 0, count = 0, local = 1, strict = 1;
int maximum = 0, count = 0, local = 0, strict = 0;
for (head = head->next; head; head = head->next)
{
int cmp = strcmp(head->string, last);
if (cmp > 0) {
count += local;
if (maximum < count)
maximum = count;
count = 0;
local = 1;
strict = 1;
} else if (cmp < 0) {
if (!strict)
count += local;
if (maximum < count)
maximum = count;
count = 0;
local = 1;
strict = 0;
} else {
local++;
if (local)
local++;
}
last = head->string;
}
if (!strict)
count += local;
if (maximum < count)
maximum = count;
return maximum;
}
*/

View file

@ -5,29 +5,48 @@
int t3_solve (node *head)
{
node *next = NULL;
node *last, *next = NULL;
int count = 0, local = 0;
if (!head)
return 0;
last = head;
head = last->next;
if (!head)
return 0;
for (next = head->next; next; next = head->next)
{
int cmp = strcmp(next->string, head->string);
if (cmp < 0) {
if (local) {
count += local;
local = 0;
}
} else if (cmp == 0) {
if (local)
local++;
} else
local = 1;
int cmp_last = strcmp(head->string, last->string);
int cmp_next = strcmp(head->string, next->string);
if (cmp_last > 0) {
local = 1;
} else if (cmp_last == 0) {
if (local) {
if (cmp_next < 0) {
count += local;
local = 0;
} else
local++;
}
} else {
count += local;
local = 0;
}
last = head;
head = next;
}
if (strcmp(last->string, head->string) >= 0)
count += local;
else
if (local >= 1)
count += local - 1;
return count;
}