Last active
July 24, 2020 07:09
-
-
Save lyohe/40f1f89222bf3e3d8eae4b176032a9d7 to your computer and use it in GitHub Desktop.
2007年 東京大学 数学(文系) 第3問
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 <stdio.h> | |
| #include <math.h> | |
| #include <stdlib.h> | |
| // qsort に関数ポインタとして渡す関数 | |
| int compare_base(const void *b, const void *f) { | |
| return (*(int *)b - *(int *)f); | |
| } | |
| int main(void) { | |
| int answer[100]; | |
| int m; | |
| // 下 2 桁の 4 乗を総当りで計算する | |
| m = 0; | |
| for (int m = 0; m < 100; m++) { | |
| answer[m] = (5 * (int)pow(m, 4)) % 100; | |
| } | |
| // 昇順で並べ替える | |
| qsort(answer, 100, sizeof(int), compare_base); | |
| // 重複を排除して結果を出力する | |
| for (int i = 0; i < 100; i++) { | |
| if (i == 0 || (i >= 1 && answer[i - 1] != answer[i])) { | |
| printf("%d\n", answer[i]); | |
| } | |
| } | |
| return (0); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#define _countof(array) (sizeof(array) / sizeof(array[0]))のようにマクロを定義しておくと、 qsort 関数に渡す配列の要素数をベタ書きしなくてよくなる。
https://twitter.com/shmorimo/status/1251709565978030082?s=20