user-0 提问: 圆覆盖矩形优化问题

回复:
user: user-0
content: 以下是相同的圆覆盖矩形的优化问题（但我调试了好几次总有错误） #include <windows.h> #include <math.h> #include <stdio.h> #define WINDOW_WIDTH 400 #define WINDOW_HEIGHT 400 char* g_szApplicationName="圆覆盖矩形（矩形覆盖）"; char* g_szWindowClassName="LinrenWindowClass"; struct  mypoint{ double x,y; }myp[200]; struct myrect{ double x,y; double x2,y2; double x3,y3; }myr[200]; /**只用修改这里******************************/ #define PARM_R 24.1 #define PARM_L 233.5 #define PARM_D 197.4 /******************************只用修改这里**/ int n,n2; double r=PARM_R,l=PARM_L,d=PARM_D; double at,sq2,x,y; double a,e; #undef PARM_R #undef PARM_L #undef PARM_D char str[100]; void fun(){ int cl,cd,i,j; x=(double(WINDOW_WIDTH)-l)/2.0; y=(double(WINDOW_HEIGHT)-d)/2.0; sq2=sqrt(2.0); a=sq2*r; at=(a-r)/sq2; cl=(int)(l/a); cd=(int)(d/a); n=0; for(i=0;i<cl;i++){ for(j=0;j<cd;j++){ myp[n].x=x+a*i; myp[n].y=y+a*j; n++; } } e=(int)(r+r+1e-15); n2=0; double t1,t2,t3,t4; int kd; t1=l-cl*a; t2=sqrt((r+r)*(r+r)-t1*t1); t3=d-cd*a; t4=sqrt((r+r)*(r+r)-t3*t3); if(t1<t4){ kd=(int)(l/t4); myr[n2].x=x+kd*t4; myr[n2].y=y+cd*a; myr[n2].x2=x+(kd+1)*t4; myr[n2].y2=y+d; myr[n2].x3=myr[n2].x-(r-t4/2.0); myr[n2].y3=myr[n2].y-(r-t3/2.0); n2++; }else if(t3<t2){ kd=(int)(d/t2); myr[n2].x=x+cl*a; myr[n2].y=y+kd*t2; myr[n2].x2=x+l; myr[n2].y2=y+(kd+1)*t2; myr[n2].x3=myr[n2].x-(r-t1/2.0); myr[n2].y3=myr[n2].y-(r-t2/2.0); n2++; }else{ kd=(int)(l/t4); myr[n2].x=x+kd*t4; myr[n2].y=y+cd*a; myr[n2].x2=x+(kd+1)*t4; myr[n2].y2=y+d; myr[n2].x3=myr[n2].x-(r-t4/2.0); myr[n2].y3=myr[n2].y-(r-t3/2.0); n2++; kd=(int)(d/t2); myr[n2].x=x+cl*a; myr[n2].y=y+kd*t2; myr[n2].x2=x+l; myr[n2].y2=y+(kd+1)*t2; myr[n2].x3=myr[n2].x-(r-t1/2.0); myr[n2].y3=myr[n2].y-(r-t2/2.0); n2++; } kd=(int)(d/t2); for(i=0;i<kd;i++){ myr[n2].x=x+cl*a; myr[n2].y=y+i*t2; myr[n2].x2=x+l; myr[n2].y2=y+(i+1)*t2; myr[n2].x3=myr[n2].x-(r-t1/2.0); myr[n2].y3=myr[n2].y-(r-t2/2.0); n2++; } kd=(int)(l/t4); for(i=0;i<kd;i++){ myr[n2].x=x+i*t4; myr[n2].y=y+cd*a; myr[n2].x2=x+(i+1)*t4; myr[n2].y2=y+d; myr[n2].x3=myr[n2].x-(r-t4/2.0); myr[n2].y3=myr[n2].y-(r-t3/2.0); n2++; } sprintf(str,"总数：%d",n+n2); } LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){ static HBRUSH hbrush=(HBRUSH)GetStockObject(NULL_BRUSH); static HPEN newpen=CreatePen(PS_SOLID,1,RGB(255,0,0)); static HPEN oldpen=NULL; switch(msg){ case WM_CREATE: { PAINTSTRUCT ps; int i; BeginPaint(hwnd,&ps); SelectObject(ps.hdc,hbrush); for(i=0;i<n;i++){ Ellipse(ps.hdc,(int)(myp[i].x-at+0.5),(int)(myp[i].y-at+0.5),(int)(myp[i].x-at+e+0.5),(int)(myp[i].y-at+e+0.5)); Rectangle(ps.hdc,(int)(myp[i].x+0.5),(int)(myp[i].y+0.5),(int)(myp[i].x+a+0.5),(int)(myp[i].y+a+0.5)); } for(i=0;i<n2;i++){ Ellipse(ps.hdc,(int)(myr[i].x3+0.5),(int)(myr[i].y3+0.5),(int)(myr[i].x3+e+0.5),(int)(myr[i].y3+e+0.5)); Rectangle(ps.hdc,(int)(myr[i].x+0.5),(int)(myr[i].y+0.5),(int)(myr[i].x2+0.5),(int)(myr[i].y2+0.5)); } oldpen=(HPEN)SelectObject(ps.hdc,newpen); Rectangle(ps.hdc,(int)(x+0.5),(int)(y+0.5),(int)(x+l+0.5),(int)(y+d+0.5)); SelectObject(ps.hdc,oldpen); TextOut(ps.hdc,20,20,str,strlen(str)); EndPaint(hwnd,&ps); } break; case WM_PAINT: { PAINTSTRUCT ps; int i; BeginPaint(hwnd,&ps); SelectObject(ps.hdc,hbrush); for(i=0;i<n;i++){ Ellipse(ps.hdc,(int)(myp[i].x-at+0.5),(int)(myp[i].y-at+0.5),(int)(myp[i].x-at+e+0.5),(int)(myp[i].y-at+e+0.5)); Rectangle(ps.hdc,(int)(myp[i].x+0.5),(int)(myp[i].y+0.5),(int)(myp[i].x+a+0.5),(int)(myp[i].y+a+0.5)); } for(i=0;i<n2;i++){ Ellipse(ps.hdc,(int)(myr[i].x3+0.5),(int)(myr[i].y3+0.5),(int)(myr[i].x3+e+0.5),(int)(myr[i].y3+e+0.5)); Rectangle(ps.hdc,(int)(myr[i].x+0.5),(int)(myr[i].y+0.5),(int)(myr[i].x2+0.5),(int)(myr[i].y2+0.5)); } oldpen=(HPEN)SelectObject(ps.hdc,newpen); Rectangle(ps.hdc,(int)(x+0.5),(int)(y+0.5),(int)(x+l+0.5),(int)(y+d+0.5)); SelectObject(ps.hdc,oldpen); TextOut(ps.hdc,20,20,str,strlen(str)); EndPaint(hwnd,&ps); } break; case WM_DESTROY: DeleteObject(hbrush);DeleteObject(newpen);DeleteObject(oldpen); PostQuitMessage(0);break; } return DefWindowProc(hwnd,msg,wParam,lParam); } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR szCmdLine,int iCmdShow){ HWND hWnd; WNDCLASSEX winclass; winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WindowProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hInstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = g_szWindowClassName; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&winclass)){ MessageBox(NULL,"RegisterClassEx Error!","error",0);return 0; } hWnd=CreateWindowEx(NULL, g_szWindowClassName, g_szApplicationName, WS_OVERLAPPEDWINDOW, GetSystemMetrics(SM_CXSCREEN)/2-WINDOW_WIDTH/2, GetSystemMetrics(SM_CYSCREEN)/2-WINDOW_HEIGHT/2, WINDOW_WIDTH, WINDOW_HEIGHT, NULL,NULL,hInstance,NULL); if(!hWnd){ MessageBox(NULL,"CreateWindowEx Error!","error",0);return 0; } fun(); ShowWindow(hWnd, iCmdShow);UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg,NULL,0,0)){ TranslateMessage(&msg);DispatchMessage(&msg); } return msg.wParam; }

user: user-1
content: 排料问题大牛:http://www2.stetson.edu/~efriedma/packing.html

user: user-0
content: 这是我在数学建模中遇到的问题

user: user-1
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。