codeforces -958(div.2)A. Split the Multiset

[958(div.2)A. Split the Multiset](Problem - A - Codeforces)

每次把数拆分成K-1个1和第K个数(设为x),然后再去判断x是拆分几次。弄了个循环。

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int cnt=0;
int n,k;
void slove(int x)
{
	while(x>k)		//每次把该数拆分成k-1个1和第K个数x。
	{
		x-=k-1;
		cnt++;		//拆分次数累加。
	}
	cnt++;		//循环出来时一定是x<=k;该数拆分1次就行,所以cnt累加1次。
}
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		cnt=0;		//统计拆分次数
		cin>>n>>k;
		if(n==1)
		{
			cout<<0<<endl;		//如果n为1则不用拆分
			continue;
		}else if(n>1&&n<=k)
		{
			cout<<1<<endl;			//如果n<=k,则拆分1次就都是1的组合了。
			continue;
		}else{
			slove(n);		//只有当n>k时才需要判断几次。
		}
		cout<<cnt<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值