codeforces #685 div2 Subtract or Divide(水题)

A. Subtract or Divide
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ridbit starts with an integer n.

In one move, he can perform one of the following operations:

divide n by one of its proper divisors, or
subtract 1 from n if n is greater than 1.
A proper divisor is a divisor of a number, excluding itself. For example, 1, 2, 4, 5, and 10 are proper divisors of 20, but 20 itself is not.

What is the minimum number of moves Ridbit is required to make to reduce n to 1?

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains a single integer n (1≤n≤109).

Output
For each test case, output the minimum number of moves required to reduce n to 1.

Example
inputCopy
6
1
2
3
4
6
9
outputCopy
0
1
2
2
2
3
Note
For the test cases in the example, n may be reduced to 1 using the following operations in sequence

1
2→1
3→2→1
4→2→1
6→2→1
9→3→2→1
题意:任何一个数,最少需要多少步就能从自身变到1,。
此题的问题规模很大,一般就是要找到问题答案的规律即可。
n=1,2,3,此时分别需要0,1,2,此时以后的数只要分析其规律即可,奇数先化成偶数,偶数直接变成2,再变成1,此时问题就解决了。奇数为3,偶数为2
代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
   int t;
   int n;
   cin>>t;
   while(t--)
   {
      cin>>n;
      if(n==1)
         printf("0\n");
       else if(n==2)
          printf("1\n");
       else if(n==3||n%2==0)
           printf("2\n");
        else   printf("3\n");
     }
     return 0;
}

本题的样例中的解释可能会有一定的导向性,会严重指向素数筛这个思路,希望此题能做以警惕

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值