题意:给一张长宽W*H的纸, 求最少多少次可以折成长宽 w*h,每次只能平行边缘折
思路:每次对折是最优的,最后一次不一定对折,但是只计算次数,可以不用管最后一次到底怎么折的,只要第一次折到W<=w H<=h,即可,一共有2种情况:1、W->w , H->h,2、W->h, H->w; 2种情况都要考虑
AC代码:
#include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #pragma comment(linker, "/STACK:102400000,102400000") #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a,x) memset(a,x,sizeof(a)) #define step(x) fixed<< setprecision(x)<< #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define ll long long #define endl (" ") #define ft first #define sd second #define lrt (rt<<1) #define rrt (rt<<1|1) using namespace std; const ll mod=1e9+7; const ll INF = 1e18+1LL; const int inf = 1e9+1e8; const double PI=acos(-1.0); const double eps=1e-9; const int N=1e5+100; int main(){ freopen("folding.in","r",stdin); freopen("folding.out","w",stdout); double W,H,w,h; int ans1=0,ans2=0; cin>>W>>H>>w>>h; double W1=W,H1=H; if(W<H) swap(W,H); if(w<h) swap(w,h); if(W<w || H<h){ cout<<"-1 "; return 0; } while(W-w>eps){ W/=2; ans1++; } while(H-h>eps){ H/=2; ans1++; } if(W1<H1) swap(W1,H1); if(w>h) swap(w,h); if(W1<w || H1<h) ans2=inf; while(W1-w>eps){ W1/=2; ans2++; } while(H1-h>eps){ H1/=2; ans2++; } int ans=min(ans1,ans2); cout<<ans<<endl; return 0; }