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;
}

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

猜你喜欢

转载自blog.csdn.net/weixin_46006714/article/details/109913596
今日推荐