Codeforces Round #685 (Div. 2) Problem - A. Subtract or Divide 题解

题目

题目链接

Codeforces Round #685 (Div. 2) Problem - A. Subtract or Divide

题目大意

给你一个整数n,你可以进行以下操作:

  • 将n除以一个能整除的数。
  • 将n减去1(如果n大于1)

求数量最小的操作数使得n变成1。

输入

第一行包含测试情况数量t(1<=t<=1000)。

每个测试情况只包含一个整数n(1<=n<=1e9)。

输出

对于每个输出情况,输出使得n变为1的最小操作数量。

样例输入

6
1
2
3
4
6
9

样例输出

0
1
2
2
2
3

题解

  • 如果n=1,结果为0
  • 如果n=2,2->1,结果为1
  • 如果n=3,3->2->1,结果为2
  • 如果n>3且n为偶数,n->2->1,结果为2
  • 如果n>3且n为奇数,n->n-1->2->1,结果为3

Then show the code.

// A. Subtract or Divide
#include <stdio.h>

long long n;
int main(){
    int t, cnt;
    scanf("%d", &t);
    while(t--){
        scanf("%I64d", &n);
        cnt = 0;
        if(n==2)
            cnt = 1;
        else if(n == 3)
            cnt = 2;
        else if(n%2 == 0 && n>3)
            cnt = 2;
        else if(n%2 && n>3)
            cnt = 3;
        printf("%d\n", cnt);
    }
}
posted @ 2020-11-22 21:29  1v7w  阅读(129)  评论(0编辑  收藏  举报