Problem Description
As chief engineer of the Starship Interprize, the task of repairing the hyperstellar, cubic, transwarped-out software has fallen on your shoulders. Simply put, you must compute the volume of the intersection of anywhere from 2 to 1000 cubes.
Input
The input data file consists of several sets of cubes for which the volume of their intersections must be computed. The first line of the data file contains a number (from 2 to 1000) which indicates the number of cubes which follow, one cube per line. Each line which describes a cube contains four integers. The first three integers are the x, y, and z coordinates of the corner of a cube, and the fourth integer is the positive distance which the cube extends in each of the three directions (parallel to the x, y, and z axes) from that corner.
Output
Following the data for the first set of cubes will be a number which indicates how many cubes are in a second set, followed by the cube descriptions for the second set, again one per line. Following this will be a third set, and so on. Your program should continue to process sets of cubes, outputting the volume of their intersections to the output file, one set per line, until a zero is read for the number of cubes.
Note that the data file will always contain at least one set of cubes, and every set will contain at least 2 and at most 1000 cubes. For any given set of cubes, the volume of their intersections will not exceed 1,000,000 units.
这个题题意看懂了,然后明白其中的道理就很好写。一开始我就很懵逼,不知道从何下手。画了画之后就明白了。这种思路很常用,一边求最小值一边求最大值。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
const int maxx=1e4+10;
struct node{
int x;
int y;
int z;
int l;
}p[maxx];
int n;
int main()
{
while(cin>>n,n)
{
for(int i=0;i<n;i++)
cin>>p[i].x>>p[i].y>>p[i].z>>p[i].l;
int x1=-inf;
int x2=inf;
int y1=-inf;
int y2=inf;
int z1=-inf;
int z2=inf;
for(int i=0;i<n;i++)
{
x1=max(x1,p[i].x);//求最小值x
x2=min(x2,p[i].x+p[i].l);//求最大值x
y1=max(y1,p[i].y);//求最小值y
y2=min(y2,p[i].y+p[i].l);//求最大值y
z1=max(z1,p[i].z);//求最小值z
z2=min(z2,p[i].z+p[i].l);//求最大值z
}
if(x1>=x2||y1>=y2||z1>=z2) cout<<"0"<<endl;
else cout<<(x2-x1)*(y2-y1)*(z2-z1)<<endl;
}
return 0;
}
注明一下,这里所说的求最大值x(y,z),是要求一开始的时候x(y,z)的最大值,因为是求重合立方体的体积。求最小值的时候也是如此。画一下图就可以了。
努力加油a题啊,ヾ(◍°∇°◍)ノ゙