0
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?

More than 3 years have passed since last update.

C++で配列を並び替えて逆順にする方法

Last updated at Posted at 2021-05-16

C++で配列を昇順に並び替え、降順に並び替えて、3人の身長の差を求めるプログラムです。

# include <bits/stdc++.h>
using namespace std;

int main() {
  //3人の身長を定義するフィールド
  int A, B, C;
  //身長の差を計算するフィールド
  int ans = 0;
  //3人の身長を入力する
  cin >> A >> B >> C;
  //配列vecの定義
  vector<int> vec(3);
  //3人の身長データを配列に格納する
  vec={A,B,C};
  //昇順で並び替える
  sort(vec.begin(),vec.end());
  //降順に並び替える
  reverse(vec.begin(),vec.end());
  //最大身長差を求め、標準出力に出力する。
  ans = vec.at(0) - vec.at(2);
  cout << ans <<endl;
  return 0;
}

配列とsort、reverse関数を使うと簡単に並び替えできます。是非マスターしてください。

0
0
2

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
0
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?