⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 错误思想的代码.cpp

📁 集装箱的装箱问题 给定一个集装箱
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 100 //表示木材的最大个数
#define INFINITY 10000
	double pos[MAX][2],r[MAX];			//pos[i][0]表示第i个木材的x坐标,pos[i][1]表示第i个木材的y坐标
	double tmp[MAX][2];			//这个用于判断当前块该放在哪里
	int close[MAX][MAX]={0};			//close[i]表示与i个木材相邻的木材号
	double x[MAX],y[MAX];
	int i,j,k,n,pid;
	double w,h,l;
	char str[50];


bool cmp(const double &a,const double &b);

void compute(int i,int j,int pid,double r3){
	double x1,y1,x2,y2,r1,r2,arc1,arc2;
	x1=pos[i][0];	y1=pos[i][1];
	x2=pos[j][0];	y2=pos[j][1];
	r1=r[i];	r2=r[j];
	arc1=atan(y2-y1)/(x2-x1);
	arc2=acos(((r1+r2)*(r1+r2)+(r1+r3)*(r1+r3)-(r2+r3)*(r2+r3))/(2*(r1+r3)*(r1+r2)));
	x[pid]=x1+(r1+r3)*cos(arc1+arc2);
	y[pid]=y1+(r1+r3)*sin(arc1+arc2);
	printf("pid xpid y pid %lf %lf\n",x[pid],y[pid]);
}

int CheckOK(int pid,int i){
	int j;
	double x11,y11;
	x11=x[pid];
	y11=y[pid];
	if(x11>w||y11>h) return 0;
	for(j=1;j<i;j++){
		if((x11-pos[j][0])*(x11-pos[j][0])+(y11-pos[j][0])*(y11-pos[j][0])+0.1<(r[j]+r[i])*(r[j]+r[i]))		//加0.1是防止浮点数的计算误差
			return 0;
	}
	return 1;
}
int main(){
	int flag,l;
	printf("输入文件名:\n");
	scanf("%s",str);
	if(freopen(str,"r",stdin)==NULL){
		printf("文件无法打开\n");
		exit(1);
	}

	scanf("%d %lf %lf %lf",&n,&w,&h,&l);
	for(i=1;i<=n;i++){
		scanf("%lf ",&r[i]);
//		printf("%lf ",r[i]);
	}


	sort(r+1,r+n+1,cmp);
	pos[0][0]=0;
	pos[0][1]=0;
	pos[1][0]=r[1];
	pos[1][1]=r[1];
	close[1][0]=1;
	for(i=1;i<=n;i++){
		pid=1;
		flag=0;					//用于判
		for(j=1;j<i;j++){		//这一段用来判断该放在哪个地方
			for(k=0;k<j;k++)
				if(j!=k&&close[j][k]==1)	{
					compute(j,k,pid,r[i]);
					if(CheckOK(pid,i)!=1){
						x[pid]=INFINITY;
						y[pid]=INFINITY;
						flag=1;		//该点不能使用
					}
					if(flag==0) pid++;
				}
			for(l=1;l<pid;l++)
				
		}

	}
	return 0;
}

bool cmp(const double &a,const double &b){
	return a>b;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -