Created
July 14, 2015 06:21
-
-
Save fddcddhdd/f61c110a4c5567cf5864 to your computer and use it in GitHub Desktop.
CodeIQ「ホリエモンからの挑戦状」の解説記事が公開されていたので、自分のソースも晒しておく。
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 <stdlib.h> | |
//降順ソート関数 | |
int compare_int(const void *b, const void *a) | |
{ | |
return *(int*)a - *(int*)b; | |
} | |
int main(void) | |
{ | |
int cnt=0; | |
int bar_sum=0; | |
int bar_kind=0; | |
int bar_len[5000]; | |
scanf("%d", &bar_sum); // 3本の合計の長さ | |
scanf("%d", &bar_kind); // 棒の数 | |
// 各棒の長さを入力 | |
int input; | |
for(input=0; input<bar_kind; input++){ | |
scanf("%d", &bar_len[input]); | |
} | |
//棒の長い順にソート | |
qsort(bar_len, bar_kind, sizeof(int), compare_int); | |
//合計値の1/3を算出しておく | |
int one_third = (int)(bar_sum/3)+1; | |
//一本目 | |
int i; | |
for(i=0; i<bar_kind; i++){ | |
//一本目が三本の合計値の1/3を下回っていたら計算する必要はない(降順ソートだから、それ以上大きな棒は出てこないので) | |
if(one_third > bar_len[i]){ | |
continue; | |
} | |
//二本目 | |
int j; | |
for(j=0; j<bar_kind; j++){ | |
// 三本目の長さは、計算で求める | |
int third_bar = bar_sum - (bar_len[i] + bar_len[j]); | |
// 一本目 > 二本目 > 三本目 じゃなかったら、スキップ | |
if( bar_len[i] <= bar_len[j] || bar_len[j] <= third_bar){ | |
continue; | |
} | |
//存在していたら、正しい組み合わせなのでカウントする | |
if(bsearch( &third_bar, bar_len, bar_kind, sizeof(int), compare_int ) == NULL){ | |
}else{ | |
// printf("%d = %d+%d+%d\n", bar_sum, bar_len[i], bar_len[j], third_bar); | |
cnt++; | |
} | |
} | |
} | |
printf("%d\n", cnt); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment