Created
May 2, 2018 17:00
-
-
Save 10nin/bf6d9f857b5bb0fa946cc0cf3cfae380 to your computer and use it in GitHub Desktop.
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0570 この問題に出てくるような、ジグザグ数を探すというのをC言語でやってみた感じのやつ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#define ZIG 1 | |
#define NONE 0 | |
#define ZAG -1 | |
bool is_zigzag(char* s) { | |
int mode = NONE; | |
char prev = '\0'; | |
for(int i = 0; s[i] != '\0'; i++) { | |
if(prev != '\0') { | |
if(prev < s[i] && mode != ZIG) { | |
mode = ZIG; | |
} else if(s[i] < prev && mode != ZAG) { | |
mode = ZAG; | |
} else { | |
return false; | |
} | |
} | |
prev = s[i]; | |
} | |
return true; // this number is zig-zag | |
} | |
int main(void) { | |
char num[80]; | |
printf("input number: "); | |
scanf("%s", num); | |
if(is_zigzag(num)) { | |
printf("this number is zig-zag number.\n"); | |
} else { | |
printf("this number is not zig-zag number.\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment