Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MILLION *1000000
- #define ANT_SPEED 1
- #define START_DISTANCE 100
- #define MAX_STEP_COUNT 55 MILLION
- float map(float value, float from_low, float from_high, float to_low, float to_high) {
- return to_low + (value - from_low) * (to_high - to_low) / (from_high - from_low);
- }
- int main(void) {
- system("chcp 1251 > NULL");
- double ant_position = 0.0; // Расстояние от точки 0.0
- double total_ant_path = 0.0; // Соотношение пройденного пути с предстоящим на данный момент дистанцией
- long long i = 1; // Номер итерации
- double total_length = START_DISTANCE; // Общий длина пути на данный момент
- while (1) {
- // Муравей шагает, а после нить растягивается
- ant_position += ANT_SPEED;
- ant_position = map(ant_position, 0, total_length, 0, total_length + START_DISTANCE);
- total_length += START_DISTANCE; // Подсчитывается ныняшня длина пути с учётом расяжения
- total_ant_path = ant_position / total_length; // Отношение пройденного пути пересчитывается
- if (ant_position >= total_length) {
- printf("Муравей прошёл всё расстояние за %lld шаг(ов), всего %f сантиметров\n", i, ant_position);
- break;
- } else if (i >= MAX_STEP_COUNT) {
- printf("%lld шага(ов) не хватило, что бы добраться до конца \nбыло пройдено %f сантиметров, осталось ещё %f\n", i, ant_position, total_length - ant_position);
- printf("в процентном соотношении муравей прошёл %.2f %% пути\n", 100 * (total_ant_path) );
- break;
- }
- i++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement