Advertisement
Guest User

Untitled

a guest
Jan 15th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MILLION *1000000
  5.  
  6. #define ANT_SPEED 1
  7. #define START_DISTANCE 100
  8. #define MAX_STEP_COUNT 55 MILLION
  9.  
  10. float map(float value, float from_low, float from_high, float to_low, float to_high) {
  11. return to_low + (value - from_low) * (to_high - to_low) / (from_high - from_low);
  12. }
  13.  
  14. int main(void) {
  15. system("chcp 1251 > NULL");
  16.  
  17. double ant_position = 0.0; // Расстояние от точки 0.0
  18. double total_ant_path = 0.0; // Соотношение пройденного пути с предстоящим на данный момент дистанцией
  19. long long i = 1; // Номер итерации
  20. double total_length = START_DISTANCE; // Общий длина пути на данный момент
  21.  
  22.  
  23. while (1) {
  24.  
  25.  
  26. // Муравей шагает, а после нить растягивается
  27. ant_position += ANT_SPEED;
  28. ant_position = map(ant_position, 0, total_length, 0, total_length + START_DISTANCE);
  29.  
  30.  
  31. total_length += START_DISTANCE; // Подсчитывается ныняшня длина пути с учётом расяжения
  32. total_ant_path = ant_position / total_length; // Отношение пройденного пути пересчитывается
  33.  
  34. if (ant_position >= total_length) {
  35.  
  36. printf("Муравей прошёл всё расстояние за %lld шаг(ов), всего %f сантиметров\n", i, ant_position);
  37. break;
  38.  
  39. } else if (i >= MAX_STEP_COUNT) {
  40.  
  41. printf("%lld шага(ов) не хватило, что бы добраться до конца \nбыло пройдено %f сантиметров, осталось ещё %f\n", i, ant_position, total_length - ant_position);
  42. printf("в процентном соотношении муравей прошёл %.2f %% пути\n", 100 * (total_ant_path) );
  43.  
  44. break;
  45. }
  46. i++;
  47. }
  48.  
  49.  
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement