Skip to content

Instantly share code, notes, and snippets.

@lyohe
Last active July 24, 2020 07:09
Show Gist options
  • Select an option

  • Save lyohe/40f1f89222bf3e3d8eae4b176032a9d7 to your computer and use it in GitHub Desktop.

Select an option

Save lyohe/40f1f89222bf3e3d8eae4b176032a9d7 to your computer and use it in GitHub Desktop.
2007年 東京大学 数学(文系) 第3問
#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);
}
@lyohe
Copy link
Copy Markdown
Author

lyohe commented Apr 19, 2020

#define _countof(array) (sizeof(array) / sizeof(array[0]))

のようにマクロを定義しておくと、 qsort 関数に渡す配列の要素数をベタ書きしなくてよくなる。
https://twitter.com/shmorimo/status/1251709565978030082?s=20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment