2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza×Qiitaコラボキャンペーン ということで
本の整理 (paizaランク A 相当
をC言語でやってみました。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i, j, min_idx, count = 0, nbook, *books;

    /* 入力データ取得 */
    fscanf(stdin, "%d", &nbook);
    books = calloc(nbook, sizeof(int));
    for (i = 0; i < nbook; i++){
        fscanf(stdin, "%d", &books[i]);
    }

    /* 問題文通りのアルゴリズム */
    for (i = 0; i < nbook; i++){
        for (j = i, min_idx = i; j < nbook; j++){
            if (books[j] < books[min_idx]){
                min_idx = j;
            }
        }
        /* i ≠ min_idxなら、i番目の本と min_idx番目の本を入れ替える。*/
        if (i != min_idx){
            int tmp = books[i];
            books[i] = books[min_idx];
            books[min_idx] = tmp;
            count++;
        }
    }
    printf("%d\n", count);
}

問題文通りにそのまま実装しています。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?