結果
問題 |
No.4 おもりと天秤
|
ユーザー |
![]() |
提出日時 | 2019-11-01 00:40:57 |
言語 | C (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,078 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 30,080 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-09-14 22:13:47 |
合計ジャッジ時間 | 6,912 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 TLE * 1 -- * 4 |
コンパイルメッセージ
main.c: In function 'main': main.c:39:9: warning: implicit declaration of function 'isPossible' [-Wimplicit-function-declaration] 39 | if (isPossible(sum/2-W[0], 1)) | ^~~~~~~~~~
ソースコード
#include <stdio.h> int N; int W[100]; int main() { scanf("%d", &N); for (int i=0; i<N; i++) { scanf("%d", &(W[i])); } int sum = 0; for (int i=0; i<N; i++) { sum += W[i]; } if ((sum % 2) == 1) { printf("impossible\n"); return 0; } // bubble sort int flag = 0; while (1) { flag = 0; for (int i=0; i<N-1; i++) { if (W[i] < W[i+1]) { int tmp = W[i]; W[i] = W[i+1]; W[i+1] = tmp; flag = 1; } } if (flag == 0) break; } if (isPossible(sum/2-W[0], 1)) printf("possible\n"); else printf("impossible\n"); } int isPossible(int target, int pos) { if (target == 0) return 1; if (pos == N) return 0; if (target < W[pos]) return isPossible(target, pos+1); if (isPossible(target-W[pos], pos+1) > 0) return 1; if (isPossible(target, pos+1) > 0) return 1; return 0; }