結果
問題 |
No.366 ロボットソート
|
ユーザー |
![]() |
提出日時 | 2017-04-27 15:00:10 |
言語 | C90 (gcc 12.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 606 bytes |
コンパイル時間 | 320 ms |
コンパイル使用メモリ | 22,016 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-13 14:48:16 |
合計ジャッジ時間 | 1,267 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
コンパイルメッセージ
main.c: In function ‘run’: main.c:13:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 13 | scanf("%d%d",&n,&k); | ^~~~~~~~~~~~~~~~~~~ main.c:17:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 17 | scanf("%d",a+i); | ^~~~~~~~~~~~~~~
ソースコード
#include<stdio.h> #include<stdlib.h> void swap(int *a,int i,int j){ int t=a[i]; a[i]=a[j]; a[j]=t; return; } void run(void){ int n,k; scanf("%d%d",&n,&k); int *a=(int *)malloc(sizeof(int)*n); int i; for(i=0;i<n;i++){ scanf("%d",a+i); } int count=0; int flag=1; while(flag){ flag=0; for(i=0;i<n-k;i++){ if(a[i]>a[i+k]){ swap(a,i,i+k); count++; flag=1; } } } int can=1; for(i=0;i<n-1;i++){ if(a[i]>a[i+1]){ can=0; break; } } printf("%d\n",can?count:-1); free(a); return; } int main(void){ run(); return 0; }