Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <errno.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int isnum(char *s) {
- for (size_t i = 0; s[i] != 0; ++i) {
- if (s[i] != '+' && s[i] != '-' && !isdigit(s[i])) {
- return 0;
- }
- }
- return 1;
- }
- int solve(int64_t a) {
- int8_t i8 = (int8_t)a;
- if (i8 == a) {
- return 1;
- }
- int16_t i16 = (int16_t)a;
- if (i16 == a) {
- return 2;
- }
- int32_t i32 = (int32_t)a;
- if (i32 == a) {
- return 4;
- }
- return -1;
- }
- int main(int argc, char *argv[]) {
- int64_t a;
- char *end;
- for (int i = 1; i < argc; ++i) {
- if (argv[i][0] == 0) {
- puts("-1");
- continue;
- }
- errno = 0;
- a = strtol(argv[i], &end, 10);
- if (errno == 0) {
- if (!isnum(argv[i]) || (*end != 0)) {
- puts("-1");
- } else {
- printf("%d\n", solve(a));
- }
- } else {
- puts("-1");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement