Created
April 17, 2014 01:40
-
-
Save ktnyt/10947312 to your computer and use it in GitHub Desktop.
つるかめ算 with C
This file contains hidden or 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 <ライブラリ名.h> ってやるとライブラリを使える。 | |
** stdioとstdlibがあれば大抵できるけど他にもほしくなるケースもあるよ。 | |
** int main() っていうのも定型文で、メインの処理をここに書くよ。 | |
** ※だからmainっていう名前なのね。 | |
** int argcとchar** argvはコマンドライン引数って言うよ。 | |
** コンパイルしたあとに実行するときに | |
** ./hoge 10 20 みたいにするとスペースで区切られた文字列が入ってるよ。 | |
** テンプレここから *********************************************************** | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char** argv) { | |
return 0; | |
} | |
** ここまで ******************************************************************* | |
*/ | |
// というわけでここからコード... | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char** argv) { | |
int head, foot, crane, turtle; | |
// 引数が3つ以下 (追加の引数が2つ以下) だと足りないので終了。 | |
if(argc < 3) { | |
printf("引数が足りません。\n"); | |
return 1; | |
} | |
// 一個目の引数を頭とする | |
head = atoi(argv[1]); | |
// 二個目の引数を足とする | |
foot = atoi(argv[2]); | |
// 例外処理 | |
if(foot < head * 2) { | |
printf("足の数が少なすぎます。\n"); | |
} | |
if(foot > head * 4) { | |
printf("足の数が多すぎます。\n"); | |
} | |
if(foot % 2 != 0) { | |
printf("足の数が奇数です\n"); | |
return 1; | |
} | |
turtle = (foot - 2 * head) / 2; | |
crane = head - turtle; | |
printf("ツル: %d\nカメ: %d\n", crane, turtle); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment