今天教了贪心,是节省时间复杂度的算法
时间复杂度仅有o(n)
打起来也不难
以下是贪心的一道题解
include
using namespace std;
int main() {
freopen("dis.in","r",stdin);
freopen("dis.out","w",stdout);
int a,b,s=0;
cin>>a>>b;
while(b>a) {
if(b%21) {
s++;
b–;
} else if(b/a<2) {
s+=b-a;
break;
} else {
b=b/2;
s++;
}
}
cout<<s;
return 0;
}