ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1001. Обратный корень

C Accepted
Послано vzhanm 28 апр 2024 12:09
/* https://acm.timus.ru/problem.aspx?space=1&num=1001 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* #include <sys/queue.h> */
/*
 * Singly-linked List definitions.
 */
#define    SLIST_HEAD(name, type)                        \
struct name {                                \
    struct type *slh_first;    /* first element */            \
}

#define    SLIST_HEAD_INITIALIZER(head)                    \
    { NULL }

#define    SLIST_ENTRY(type)                        \
struct {                                \
    struct type *sle_next;    /* next element */            \
}

/*
 * Singly-linked List functions.
 */
#define    SLIST_INIT(head) do {                        \
    (head)->slh_first = NULL;                    \
} while (/*CONSTCOND*/0)

#define    SLIST_INSERT_AFTER(slistelm, elm, field) do {            \
    (elm)->field.sle_next = (slistelm)->field.sle_next;        \
    (slistelm)->field.sle_next = (elm);                \
} while (/*CONSTCOND*/0)

#define    SLIST_INSERT_HEAD(head, elm, field) do {            \
    (elm)->field.sle_next = (head)->slh_first;            \
    (head)->slh_first = (elm);                    \
} while (/*CONSTCOND*/0)

#define    SLIST_REMOVE_HEAD(head, field) do {                \
    (head)->slh_first = (head)->slh_first->field.sle_next;        \
} while (/*CONSTCOND*/0)

#define    SLIST_REMOVE(head, elm, type, field) do {            \
    if ((head)->slh_first == (elm)) {                \
        SLIST_REMOVE_HEAD((head), field);            \
    }                                \
    else {                                \
        struct type *curelm = (head)->slh_first;        \
        while(curelm->field.sle_next != (elm))            \
            curelm = curelm->field.sle_next;        \
        curelm->field.sle_next =                \
            curelm->field.sle_next->field.sle_next;        \
    }                                \
} while (/*CONSTCOND*/0)

#define    SLIST_FOREACH(var, head, field)                    \
    for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)

/*
 * Singly-linked List access methods.
 */
#define    SLIST_EMPTY(head)    ((head)->slh_first == NULL)
#define    SLIST_FIRST(head)    ((head)->slh_first)
#define    SLIST_NEXT(elm, field)    ((elm)->field.sle_next)

struct num_entry {
    double num;
    SLIST_ENTRY(num_entry) entries;
};
SLIST_HEAD(tq_num, num_entry);

#define my_abs(x) ((x)>0?(x):-(x))

double my_sqrt(double c) {
    double err = 1e-15;
    double t = c;
    if (c < 0) {
        return -1;
    }
    while (my_abs(t- c/t) > err * t) {
        t = (c/t + t) / 2.0;
    }
    return t;
}

int main() {
    struct tq_num head;
    struct num_entry *np;
    unsigned long long m;

    SLIST_INIT(&head);
    /* https://acm.timus.ru/help.aspx?topic=cpp */
    while (scanf("%llu ", &m) != EOF) {
#ifndef ONLINE_JUDGE
        printf("m=%llu\n", m);
#endif
        np = (struct num_entry *)malloc(sizeof(struct num_entry));
        np->num = (double)m;
        SLIST_INSERT_HEAD(&head, np, entries);
    }
    while (!SLIST_EMPTY(&head)) {
        np = SLIST_FIRST(&head);
        printf("%.4f\n", my_sqrt(np->num));
        SLIST_REMOVE(&head, np, num_entry, entries);
        free(np);
    }
    return 0;
}

Edited by author 28.04.2024 12:11