其他分享
首页 > 其他分享> > 题解 CF1451A 【Subtract or Divide】

题解 CF1451A 【Subtract or Divide】

作者:互联网

题目大意

给你一个数 \(n\) ,你可以对他进行如下操作:

解法

对于这题,我们可以想想如何对数字进行转化。

首先,我们可以明显地看出,对数字 \(1\) 我们不需要转化,即答案为 \(0\) 。对于数字 \(2\) 我们只需要减一即可,答案为 \(1\) 。

其次,对于偶数(\(2\) 除外),显然必定有一个因子可以使它变为 \(2\) ,故答案必定为 \(2\) 。

然后,对于奇数(\(1\) 、\(3\) 除外),只要减一就会变为偶数,再变成 \(1\) ,故答案必定为 \(3\) 。

特别的,因为 \(3\) 减一后变为 \(2\) 所以此时答案为 \(2\)。

Code

#include <iostream>
#include <cstdio>
using namespace std;
int t;
int main()
{
	cin>>t;
	while(t--)
	{
		int k;
		cin>>k;
		if(k==2) cout<<1<<endl;
		else if(k==1) cout<<0<<endl;
		else if(k%2==0||k==3) cout<<2<<endl;
		else cout<<3<<endl;
	}
	return 0;
}

标签:Divide,int,题解,CF1451A,cin,减一,答案,除外,include
来源: https://www.cnblogs.com/-lala-/p/14072969.html