Advertisement
anechka_ne_plach

7-2

Apr 14th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. 1 │ #include <stdio.h>
  2. 2 │ #include <stdlib.h>
  3. 3 │ #include <unistd.h>
  4. 4 │ #include <sys/types.h>
  5. 5 │ #include <sys/stat.h>
  6. 6 │ #include <sys/wait.h>
  7. 7 │ #include <fcntl.h>
  8. 8 │ #include <errno.h>
  9. 9 │ #include <string.h>
  10. 10 │ #include <time.h>
  11. 11 │
  12. 12 │ void solve(char* value) {
  13. 13 │ int64_t v = strtoll(value, NULL, 10);
  14. 14 │ int ans = 0;
  15. 15 │ while (v != 0) {
  16. 16 │ int d = v % 4;
  17. 17 │ ans += abs(d);
  18. 18 │ v -= d;
  19. 19 │ v /= 4;
  20. 20 │ }
  21. 21 │ exit(ans);
  22. 22 │ }
  23. 23 │
  24. 24 │ int main(int argc, char* argv[]) {
  25. 25 │ int ans[argc], order[argc];
  26. 26 │ for (int i = 1; i < argc; i++) {
  27. 27 │ int pid = fork();
  28. 28 │ if (!pid) {
  29. 29 │ solve(argv[i]);
  30. 30 │ } else {
  31. 31 │ order[i] = pid;
  32. 32 │ }
  33. 33 │ }
  34. 34 │ for (int i = 1; i < argc; ++i) {
  35. 35 │ int status;
  36. 36 │ int pid = wait(&status);
  37. 37 │ for (int j = 1; j < argc; ++j) {
  38. 38 │ if (order[j] == pid) {
  39. 39 │ ans[j] = WEXITSTATUS(status);
  40. 40 │ }
  41. 41 │ }
  42. 42 │ }
  43. 43 │ for (int i = 1; i < argc; i++) {
  44. 44 │ printf("%d\n", ans[i]);
  45. 45 │ }
  46. 46 │ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement