Created
March 26, 2012 11:53
-
-
Save hishidama/2204616 to your computer and use it in GitHub Desktop.
変数を1つだけ使って1から100までの合計を算出するプログラム(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
// 1から100までの和を表示するプログラムを作れ | |
// http://togetter.com/li/278731 | |
#include <stdio.h> | |
// 実行する際は引数を99個指定すること。 | |
// 例)./a.out $(seq 1 99); echo $? | |
// ただし、終了コードが8bitしかない環境では正確な値は得られないorz | |
int main(int x) | |
{ | |
switch(x) { | |
case 1: | |
return x; | |
default: | |
return x + main(x - 1); | |
} | |
} |
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
// 1から100までの和を表示するプログラムを作れ | |
// http://togetter.com/li/278731 | |
#include <stdio.h> | |
// 実行する際は引数を99個指定すること。 | |
// 例)./a.out $(seq 1 99) | |
int main(int x) | |
{ | |
switch(x) { | |
case 100: | |
printf("%d\n", x + main(x - 1)); | |
return 0; | |
case 1: | |
return x; | |
default: | |
return x + main(x - 1); | |
} | |
} |
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
// 1から100までの和を表示するプログラムを作れ | |
// http://togetter.com/li/278731 | |
#include <stdio.h> | |
// 実行する際は引数を指定しないこと。 | |
// 例)./a.out | |
int main(int x) | |
{ | |
switch(x) { | |
case 1: | |
printf("%d\n", main(100)); | |
return 0; | |
case 2: | |
return x + (x - 1); | |
default: | |
return x + main(x - 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment