题意:将(w,h)的纸条折成(W,H),最少需几步。
思路:横竖互不干扰,然后最多可折int型一半,拿个函数判断两次比较即可,然后折不了的条件是需要的矩形大于给的矩形。
见代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int w,h,W,H,ans=0,sum; void fold(int h,int l,int x,int y) { if(h>=x&&l>=y) { if(h!=x) while(h-x>=h/2) { h-=h/2;ans++; if(h==x) break; } if(h!=x) ans++; if(l!=y) while(l-y>=l/2) { l-=l/2;ans++; if(l==y) break; } if(l!=y) { ans++; } } } int main() { freopen("folding.in","r",stdin); freopen("folding.out","w",stdout); cin>>w>>h>>W>>H; int a=w,b=h; fold(w,h,W,H); w=a; h=b; sum=ans; ans=0; fold(w,h,H,W); if(sum!=0&&ans!=0) ans=min(sum,ans); else { if(ans==0) ans=sum; } if((w==W&&h==H)||(w==H&&h==W)) { cout<<0<<endl; return 0; } if(ans==0) cout<<-1; else cout<<ans<<endl; return 0; }