P7036 [NWRRC2016] Folding
题目简述
有两个矩形,大小分别是 \(W \times Y\) 和 $ w \times y$。现在我们要通过折叠将两个矩阵变成一样。
思路
part1
已知一条边折叠一次会变成 \(\frac{x}{2}\) 到 \(x\) 之间的一个数,所以我们把大矩形折叠到两条边都小于小矩形的两边,然后每次折叠记录一个 \(ans\) 最后输出就行了。(用这种方式需要用 \(float\))
part2
或者我们反着来,每次把小矩形的边乘二,直到都大于大矩形的两条边。
and
这道题没有保证 \(W\) 和 \(H\) 一定大于 \(w\) 和 \(h\),所以我们还需要加一个特判。
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,n1,m1;
float x,y,xx,yy;
int ans=0;
signed main(){
cin>>n>>m>>n1>>m1;
x=min(n,m),y=max(n,m);
xx=min(n1,m1),yy=max(n1,m1);
if(x<xx||y<yy){cout<<-1<<endl;return 0;}
while(xx<x||yy<y){
if(y>yy) y/=2;
else x/=2;
if(x>y)swap(x,y);
ans++;
}
cout<<ans<<endl;
return 0;
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,n1,m1;
int x,y,xx,yy;
int ans=0;
signed main(){
cin>>n>>m>>n1>>m1;
x=max(n,m),y=min(n,m);
xx=max(n1,m1),yy=min(n1,m1);
if(x<xx||y<yy){cout<<-1<<endl;return 0;}
while(xx<x||yy<y){
if(yy<y) yy*=2;
else xx*=2;
if(xx<yy) swap(xx,yy);
ans++;
}
cout<<ans<<endl;
return 0;
}
本文来自博客园,作者:Arthur_Douglas,转载请注明原文链接:https://www.cnblogs.com/wenzhihao2023/p/17986436

浙公网安备 33010602011771号