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