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 1 year has passed since last update.

【C++】球や直方体を収納出来るボックスを選ぶプログラム

Last updated at Posted at 2022-11-06

収納するボックスは、縦・横・高さを持つ直方体とします。この中に、球または直方体を入れる事を考えます。入れる物体の候補には3種類を挙げ、それぞれについて収納可能かどうかを調べます。

まず、物体を1つに絞ってプログラムを作りました。

球形の場合

球について考える際は、入れる物体の入力情報は半径の1つのみです。
以下、3種類のボックスをそれぞれA、B、Cと呼ぶ事にします。

作成したプログラム
#include <iostream>
using namespace std;
#define N 3

int main(){
	
	int r, box[100][3];   //r:入れる物体の半径, box[][]1個目の[]:A, B, Cのそれぞれ, box[][]2個目の[]:縦・横・高さのそれぞれ
	cin >> r;
	
	for(int k = 0; k < N; k++){
		for(int m = 0; m < 3; m++){
			cin >> box[k][m];
		}
	}
	
	for(int i = 0; i < N; i++){
		for(int j = 0; j < 3; j++){
			cout << i + 1 << "番目の物体について:" << box[i][j] << endl;
			if(box[i][j] < r){
				cout << i + 1 << "番目の物体に長さ" << box[i][j] << "の辺があり、収納不可" << endl;
				break;
			}else{
				
			}
			if(j == 2){
				cout << i + 1 << "番目の物体→収納可能" << endl;
			}
		}
	}
	
}

標準入力を次のように設定して実行すると

5
1 4 8
8 3 9
7 7 6

Screenshot 2022_11_05 15_11_16.png

というように、正しく動きました。

直方体の場合

3辺の長さがそれぞれ異なる場合のある直方体は、入れる向き次第で、入らなかった物体も入るようになる事があります。

sides_boxとbox[f]それぞれの配列は小さい順にソートされたところまできたとします。
そこで「まずsides_boxとbox[f]の一番小さいもの同士、次にsides_boxとbox[f]の2番目に小さいもの同士……」といった感じで、二つの同じN番目の要素を比較していって、一回でも sides_box > box[f] になればアウトにするプログラムにしてみます。

		for(int i = 0; i < 3; i++){
			if(sides_box[i] > box[f][i]){
				break;
			}else{
				
			}
			if(i == 3-1){
				cout << f + 1 << "番目の物体→収納可能" << endl;
			}
			
		}

まとめ

ここまでの内容をもとに、(直方体の方を) 1つのプログラムにまとめます。

完成プログラム
#include <iostream>
using namespace std;
#define N 3

int main(){
	
	int box[100][3], sides_box[3];   //sides_box:入れる物体の3辺, box[][]1個目の[]:A, B, Cのそれぞれ, box[][]2個目の[]:A, B, Cのそれぞれの縦・横・高さ
	
	for(int a = 0; a < N; a++){
		cin >> sides_box[a];
	}
	for(int b = 0; b < N; b++){
		for(int c = 0; c < 3; c++){
			cin >> box[b][c];
		}
	}
	
	int d = 0;
	while(d < 3){   //sides_boxの中で大小比較を行い、最も短い辺の長さをsides_box[0]に持ってくる
		int e = 0;
		while(e < 3-1){
			if(sides_box[e] > sides_box[e+1]){
				int value;
				value = sides_box[e];
				sides_box[e] = sides_box[e+1];
				sides_box[e+1] = value;
			}
			e++;
		}
		d++;
	}
	
	for(int f = 0; f < N; f++){   //「ボックスの3辺の大小比較を行い、ボックスそれぞれの最も短い辺の長さを配列の先頭に持ってきて、都度sides_box[0]と比べる」のを1作業として、その作業をボックスの個数分行う
		
		int g = 0;
		while(g < 3){   //box[f]の中で大小比較を行い、最も短い辺の長さをbox[f][0]に持ってくる
			int h =0;
			while(h < 3-1){
				if(box[f][h] > box[f][h+1]){
					int val;
					val = box[f][h];
					box[f][h] = box[f][h+1];
					box[f][h+1] = val;
				}
				h++;
			}
			g++;
		}
		
		
		for(int i = 0; i < 3; i++){
			if(sides_box[i] > box[f][i]){
				break;
			}else{
				
			}
			if(i == 3-1){
				cout << f + 1 << "番目の物体→収納可能" << endl;
			}
			
		}
		
	}
	
}

以上

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