Simple math problem

Check your "algorithm notes" for easy viewing.

First, the least common multiple of the greatest common divisor &
Euclid's theorem: Let a, b are positive integers, then the gcd (a, b) = gcd (b, a% b).
If, on the first switching Theorem a and b.

Note: 0 and a is any positive integer gcd is a.

//最大公约数
int gcd(int a,int b)
{
    return !b ? a : gcd(b,a % b);
}

Provided the common denominator for the res, that is the least common multiple lcm.

Second, the score
PAT A score 1088 is more classic handling problems, and find two scores, a difference, product, quotient output simplest form.

He said simplification, operation, output, code illustrates very clearly.

#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

ll gcd(ll a,ll b)
{
        return !b ? a : gcd(b,a % b);
}

struct Fraction{
        ll nume,deno;
};

Fraction reduction(Fraction a)
{
        if(a.deno < 0)
        {
                a.deno = -a.deno;
                a.nume = -a.nume;
        }
        if(a.nume == 0)
        {
                a.deno = 1;
        }
        else
        {
               int d = gcd(abs(a.nume),abs(a.deno));
               a.nume /= d;
               a.deno /= d;
        }
        return a;
}

Fraction add(Fraction a,Fraction b)
{
        Fraction res;
        res.deno = a.deno * b.deno;
        res.nume = a.deno * b.nume + a.nume * b.deno;
        return reduction(res);
}

Fraction sub(Fraction a,Fraction b)
{
        Fraction res;
        res.deno = a.deno * b.deno;
        res.nume = a.nume * b.deno - a.deno * b.nume;
        return reduction(res);
}

Fraction times(Fraction a,Fraction b)
{
        Fraction res;
        res.deno = a.deno * b.deno;
        res.nume = a.nume * b.nume;
        return reduction(res);
}

Fraction divide(Fraction a,Fraction b)
{
        Fraction res;
        res.deno = a.deno * b.nume;
        res.nume = a.nume * b.deno;
        return reduction(res);
}

void showFrac(Fraction a)
{
        a = reduction(a);
        if(a.nume < 0)
        {
                printf("(");
        }
        if(a.deno == 1)
        {
                printf("%lld",a.nume);
        }
        else if(abs(a.nume) > abs(a.deno))
        {
                printf("%lld %lld/%lld",a.nume / a.deno,abs(a.nume) % a.deno,a.deno);
        }
        else
        {
                printf("%lld/%lld",a.nume,a.deno);
        }
        if(a.nume < 0)
        {
                printf(")");
        }
}

int main()
{
        Fraction a,b;
        scanf("%lld/%lld%lld/%lld",&a.nume,&a.deno,&b.nume,&b.deno);

        showFrac(a);
        printf(" + ");
        showFrac(b);
        printf(" = ");
        showFrac(add(a,b));
        printf("\n");

        showFrac(a);
        printf(" - ");
        showFrac(b);
        printf(" = ");
        showFrac(sub(a,b));
        printf("\n");

        showFrac(a);
        printf(" * ");
        showFrac(b);
        printf(" = ");
        showFrac(times(a,b));
        printf("\n");

        showFrac(a);
        printf(" / ");
        showFrac(b);
        printf(" = ");
        if(b.nume == 0)
        {
                printf("Inf\n");
        }
        else
        {
                showFrac(divide(a,b));
                printf("\n");
        }

        return 0;
}

Third, prime numbers
1, determine prime numbers

bool isPrime(int a)
{
        if(a <= 1)   //1不是素数,也不是合数
                return false;
        int tmp = (int)sqrt(1.0 * a);
        for(int i = 2;i <= tmp;i++)
        {
                if(a % i == 0)
                        return false;
        }
        return true;
}

2, hitting a prime table

The first determination method is an enumeration.

const int maxn = 10010;
int prime[maxn],num = 0;

void Prime_table()
{
        for(int i = 2;i < maxn;i++)
        {
                if(isPrime(i))
                {
                        prime[num++] = i;
                }
        }
}

The second sieve is Eratosthenes, better than the complexity of the enumeration, the code is shorter.

const int maxn = 10010;
int prime[maxn],num = 0;
bool p[maxn] = {false};  //i为素数,p[i]为false

void Prime_table()
{
        for(int i = 2;i < maxn;i++)
        {
                if(p[i] == false)
                {
                        prime[num++] = i;
                        for(int j = i + i;j < maxn;j += i)
                        {
                                p[j] = true;
                        }
                }
        }
}

3, factorising
Note: 1 to Japanese sentence.

//存储
struct factor{
        int x,cnt; //x为质因子,cnt为该质因子个数
}fac[20];
int num = 0;  //记录不同因子个数
//枚举小于等于sqrt(n)内的所有质因子,判断哪个是n的因子
for(int i = 0;prime[i] <= sqrt(n);i++)
{
        if(n % prime[i] == 0)
        {
                fac[num].x = prime[i];
                fac[num].cnt = 0;
                while(n % prime[i] == 0)
                {
                        fac[num].cnt++;
                        n /= primep[i];
                }
                num++;
        }
}

//如果n仍然大于1,说明n有一个大于sqrt(n)的质因子
if(n != 1)
{
        fac[num].x = n;
        fac[num++].cnt = 1;
}

Guess you like

Origin www.cnblogs.com/EIMadrigal/p/12130450.html