codeforce 补题 #701

codeforce 补题#701

在这里插入图片描述

这道题其实不难的,但是我不知道怎么去递归,怎么去循环。后面看了别人的代码后,感觉其实挺容易的,这里总结一下。
法一:递归写法:
要么一直做a/b运算
要么先做b+1运算再做a/b运算
每次na除到0时,记录下ans的最小值

代码1:

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
template<class T>inline void checkmin(T &a,const T &b){if(b<a)a=b;}
template<class T>inline void checkmax(T &a,const T &b){if(b>a)a=b;}
int T;
ll a,b;
int ans=0;
void dfs(ll na,ll nb,int step){
	if(na==0){
		ans=min(ans,step);
		return;
	}
	if(step==ans)return;
	dfs(na/nb,nb,step+1);
	dfs(na,nb+1,step+1);
}
void solve(){
	ans=0;
	cin>>a>>b;
	int t=0;
	if(b==1)ans++,b++,t++;
	ll nowa=a;
	while(nowa)nowa/=b,ans++;
	dfs(a,b,t);
	cout<<ans<<endl;
}
int main(){
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}

方法二:
循环求解,思路非常简单
代码如下:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i,a,n) for (int i=a; i<n; i++)

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	int t;
	cin>>t;
	while (t--){
		int a,b,res=1e18;
		cin>>a>>b;
		forn(i,0,1e4){
			if (b==1 && i==0) continue;//b等于1肯定要继续
			int cnt=i;
			int c=a,d=b+i;
			while (c){
				cnt++;
				c/=d;
			}
			res=min(res,cnt);
		}
		cout<<res<<endl;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦_赤子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值