B. Divisor Subtraction

本文详细解析了一种特殊算法,该算法通过不断减去输入整数n的最小素数因子直至n变为0,探讨了算法的执行过程,并提供了一种高效的方法来确定算法完全执行所需的步数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Divisor Subtraction

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an integer number n

. The following algorithm is applied to it:

  1. if n=0
  • , then end algorithm;
  • find the smallest prime divisor d
  • of n
  • ;
  • subtract d
  • from n and go to step 1

    Determine the number of subtrations the algorithm will make.

    Input

    The only line contains a single integer n

    (2≤n≤1010

    ).

    Output

    Print a single integer — the number of subtractions the algorithm will make.

    Examples

    Input

    Copy

    5
    
    Output

    Copy

    1
    
    Input

    Copy

    4
    
    Output

    Copy

    2
    

    Note

    In the first example 5

    is the smallest prime divisor, thus it gets subtracted right away to make a 0

    .

    In the second example 2

    is the smallest prime divisor at both steps.

    题意:给一个n,和一个算法,求这个算法能执行多少次。算法内容:

    1. 如果n等于0,算法结束。

    2. 找到n最小的素数因子d

    3. n-=d,之后回到步骤1

  • 分析:对于输入的n有3种情况:

    1. 素数-------------输出1

    2. 偶数-------------输出n/2

    3. 奇数或合数-------------减去最小的素因子,变为偶数,再执行偶数的计算方法并加1

  • #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    
    bool IsPrime(long long num)	//num为我们要判断的数
    {
    	for (long long i = 2; i <= sqrt(num); i++)		//最优化的判断方式
    	{
    		if (num%i == 0)
    		{
    			return false;
    		}
    	}
    	return true;
    }
    long long fi(long long n){
        for(long long i = 2;i < n;i++){
            if(n % i == 0)
                return i;
        }
    }
    
    int main()
    {
        long long n;
        while(cin>>n){
            long long t = 0;
            if(n % 2 == 0)
                cout<<n/2<<endl;
            else{
                if(IsPrime(n))
                    cout<<1<<endl;
                else{
                    t = n-fi(n);
                    cout<<t/2+1<<endl;
                }
            }
        }
        return 0;
    }
    

     

Create a class called Rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. For example, the fraction would be stored in the object as 1 in the numerator and 2 in the denominator. In order to compute the reduced form, you need to write a reduction function which uses the Euclidean algorithm to get the greatest common divisor (GCD) of the numerator and denominator first and then divides GCD to get the reduced numerator and denominator. Provide public member functions that perform each of the following tasks:(a) Subtract a Rational number from the other Rational number. The result should be stored in reduced form. (b) Divide a Rational number by the other Rational number. The result should be stored in reduced form. (c) Print Rational numbers in the form a/b, where a is the numerator and b is the denominator. (d) Compare two Rational numbers to make sure which one is smaller or they are equal. (1 for the first number, 2 for the second number and 0 if they are equal) Please also write a main function to prompt the user to input two Rational numbers . Subtract one number from the other from these two numbers using (a) and then print the result using (c). Divide one number from the other from these two numbers using (b) and then print the result using (c). Compare these two Rational numbers using (d) and indicate which one is smaller or they are equal.用c++ 寫出和上面不一樣的版本,要用using namespace std; 的版本,並且要有註解
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值