【算法笔记自学】第 5 章 入门篇(3)——数学问题

5.1简单数学

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;
bool cmp(int a,int b){
     return a>b;
}
void to_array(int n,int num[]){
    for(int i=0;i<4;i++){
        num[i]=n%10;
        n /=10;
     }
}
int to_number(int num[]){
    int sum=0;
    for(int i=0;i<4;i++){
        sum=sum*10+num[i];
    }
    return sum;
}
int main(){
    int n,MIN,MAX;
    scanf("%d",&n);
    int num[5];
    while(1){
       to_array(n,num);
       sort(num,num+4);
       MIN=to_number(num);
       sort(num,num+4,cmp);
       MAX=to_number(num);
       n=MAX-MIN;
       printf("%04d-%04d=%04d\n",MAX,MIN,n);
       if(n==0||n==6174)break;
    }
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <cmath>

int main() {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    int delta = b * b - 4 * a * c;
    if (delta < 0) {
        printf("No Solution");
    } else if (delta == 0) {
        printf("%.2f", -b / (2.0 * a));
    } else {
        printf("%.2f %.2f",  (-b - sqrt((double)delta)) / (2.0 * a), (-b + sqrt((double)delta)) / (2.0 * a));
    }
    return 0;
}

5.2最大公约数与最小公倍数

cpp 复制代码
#include <cstdio>
#include <cmath>
int gcd(int a,int b){
    if(b==0)return a;//求最大公约数的辗转相除法递归写法
    else return gcd(b,a%b);
}
int main() {
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF){
        printf("%d\n",gcd(m,n));
    }
    return 0;
}
cpp 复制代码
#include <cstdio>

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

int main() {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d", a / gcd(a, b) * b);
    return 0;
}

5.3分数的四则运算

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

struct Fraction {
    int up, down;
};

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

Fraction reduction(Fraction fraction) {
    if (fraction.down < 0) {
        fraction.up = -fraction.up;
        fraction.down = -fraction.down;
    }
    if (fraction.up == 0) {
        fraction.down = 1;
    } else {
        int d = gcd(abs(fraction.up), abs(fraction.down));
        fraction.up /= d;
        fraction.down /= d;
    }
    return fraction;
}

int main() {
    Fraction fraction;
    scanf("%d%d", &fraction.up, &fraction.down);
    Fraction result = reduction(fraction);
    if (result.down == 1) {
        printf("%d", result.up);
    } else {
        printf("%d %d", result.up, result.down);
    }
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

struct Fraction {
    int up, down;
};

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

Fraction reduction(Fraction fraction) {
    if (fraction.down < 0) {
        fraction.up = -fraction.up;
        fraction.down = -fraction.down;
    }
    if (fraction.up == 0) {
        fraction.down = 1;
    } else {
        int d = gcd(abs(fraction.up), abs(fraction.down));
        fraction.up /= d;
        fraction.down /= d;
    }
    return fraction;
}

Fraction add(Fraction f1, Fraction f2) {
    Fraction result;
    result.up = f1.up * f2.down + f2.up * f1.down;
    result.down = f1.down * f2.down;
    return reduction(result);
}

int main() {
    Fraction f1, f2;
    scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
    Fraction result = add(f1, f2);
    if (result.down == 1) {
        printf("%d", result.up);
    } else {
        printf("%d %d", result.up, result.down);
    }
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

struct Fraction {
    int up, down;
};

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

Fraction reduction(Fraction fraction) {
    if (fraction.down < 0) {
        fraction.up = -fraction.up;
        fraction.down = -fraction.down;
    }
    if (fraction.up == 0) {
        fraction.down = 1;
    } else {
        int d = gcd(abs(fraction.up), abs(fraction.down));
        fraction.up /= d;
        fraction.down /= d;
    }
    return fraction;
}

Fraction sub(Fraction f1, Fraction f2) {
    Fraction result;
    result.up = f1.up * f2.down - f2.up * f1.down;
    result.down = f1.down * f2.down;
    return reduction(result);
}

int main() {
    Fraction f1, f2;
    scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
    Fraction result = sub(f1, f2);
    if (result.down == 1) {
        printf("%d", result.up);
    } else {
        printf("%d %d", result.up, result.down);
    }
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

struct Fraction {
    int up, down;
};

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

Fraction reduction(Fraction fraction) {
    if (fraction.down < 0) {
        fraction.up = -fraction.up;
        fraction.down = -fraction.down;
    }
    if (fraction.up == 0) {
        fraction.down = 1;
    } else {
        int d = gcd(abs(fraction.up), abs(fraction.down));
        fraction.up /= d;
        fraction.down /= d;
    }
    return fraction;
}

Fraction multiply(Fraction f1, Fraction f2) {
    Fraction result;
    result.up = f1.up * f2.up;
    result.down = f1.down * f2.down;
    return reduction(result);
}

int main() {
    Fraction f1, f2;
    scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
    Fraction result = multiply(f1, f2);
    if (result.down == 1) {
        printf("%d", result.up);
    } else {
        printf("%d %d", result.up, result.down);
    }
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

struct Fraction {
    int up, down;
};

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

Fraction reduction(Fraction fraction) {
    if (fraction.down < 0) {
        fraction.up = -fraction.up;
        fraction.down = -fraction.down;
    }
    if (fraction.up == 0) {
        fraction.down = 1;
    } else {
        int d = gcd(abs(fraction.up), abs(fraction.down));
        fraction.up /= d;
        fraction.down /= d;
    }
    return fraction;
}

Fraction div(Fraction f1, Fraction f2) {
    Fraction result;
    result.up = f1.up * f2.down;
    result.down = f1.down * f2.up;
    return reduction(result);
}

int main() {
    Fraction f1, f2;
    scanf("%d%d%d%d", &f1.up, &f1.down, &f2.up, &f2.down);
    Fraction result = div(f1, f2);
    if(!f2.up){
    	printf("undefined");
	}
    else if (result.down == 1) {
        printf("%d", result.up);
    } else {
        printf("%d %d", result.up, result.down);
    }
    return 0;
}

5.4素数

cpp 复制代码
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPrime(int n){
    if(n<=1)return false;
    int sqr=(int)sqrt(1.0*n);
    for(int i=2;i<=sqr;i++){
        if(n%i==0)return false;
    }
    return true;

}
int main() {
    int n;
    scanf("%d",&n);
    if(isPrime(n))printf("Yes");
    else printf("No");
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPrime(int n){
    if(n<=1)return false;
    int sqr=(int)sqrt(1.0*n);
    for(int i=2;i<=sqr;i++){
        if(n%i==0)return false;
    }
    return true;

}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<n+1;i++)
    {
        if(isPrime(i))printf("%d\n",i);

    }
    return 0;
}

5.5质因子分解

cpp 复制代码
#include <cstdio>
int main() {
    int n;
    scanf("%d", &n);
    int counter = 0;
    while (n % 2 == 0) {
        counter++;
        n /= 2;
    }
    printf("%d", counter);
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;

const int MAXN = 1000 + 1;
bool isPrime[MAXN];
vector<int> primes;

void getPrimes(int n) {
    memset(isPrime, true, sizeof(isPrime));
    for (int i = 2; i <= n; i++) {
        if (isPrime[i]) {
            primes.push_back(i);
            for (int j = i + i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    getPrimes((int)sqrt(1.0 * n));
    for (int i = 0; i < primes.size() && n > 1; i++) {
        int counter = 0;
        while (n > 1 && n % primes[i] == 0) {
            counter++;
            n /= primes[i];
        }
        if (counter > 0) {
            printf("%d %d\n", primes[i], counter);
        }
    }
    if (n > 1) {
        printf("%d 1", n);
    }
    return 0;
}

5.6大整数运算

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<int> BigInt;

BigInt toBigInt(string nums) {
    BigInt result;
    for (int i = (int)nums.length() - 1; i >= 0; i--) {
        result.push_back(nums[i] - '0');
    }
    return result;
}

int compare(BigInt a, BigInt b) {
    if (a.size() > b.size()) {
        return 1;
    } else if (a.size() < b.size()) {
        return -1;
    } else {
        for (int i = (int)a.size() - 1; i >= 0; i--) {
            if (a[i] > b[i]) {
                return 1;
            } else if (a[i] < b[i]) {
                return -1;
            }
        }
        return 0;
    }
}

int main() {
    string nums1, nums2;
    cin >> nums1 >> nums2;
    BigInt a = toBigInt(nums1);
    BigInt b = toBigInt(nums2);
    int compareResult = compare(a, b);
    if (compareResult < 0) {
        printf("a < b");
    } else if (compareResult > 0) {
        printf("a > b");
    } else {
        printf("a = b");
    }
}
cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<int> BigInt;

BigInt toBigInt(string nums) {
    BigInt result;
    for (int i = (int)nums.length() - 1; i >= 0; i--) {
        result.push_back(nums[i] - '0');
    }
    return result;
}

BigInt add(BigInt a, BigInt b) {
    BigInt c;
    int carry = 0;
    for (int i = 0; i < a.size() || i < b.size(); i++) {
        int aDigit = i < a.size() ? a[i] : 0;
        int bDigit = i < b.size() ? b[i] : 0;
        int sum = aDigit + bDigit + carry;
        c.push_back(sum % 10);
        carry = sum / 10;
    }
    if (carry) {
        c.push_back(carry);
    }
    return c;
}

void print(BigInt a) {
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        cout << a[i];
    }
}

int main() {
    string nums1, nums2;
    cin >> nums1 >> nums2;
    BigInt a = toBigInt(nums1);
    BigInt b = toBigInt(nums2);
    print(add(a, b));
    return 0;
}
cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<int> BigInt;

BigInt toBigInt(string nums) {
    BigInt result;
    for (int i = (int)nums.length() - 1; i >= 0; i--) {
        result.push_back(nums[i] - '0');
    }
    return result;
}
int compare(BigInt a, BigInt b) {
    if (a.size() > b.size()) {
        return 1;
    } else if (a.size() < b.size()) {
        return -1;
    } else {
        for (int i = (int)a.size() - 1; i >= 0; i--) {
            if (a[i] > b[i]) {
                return 1;
            } else if (a[i] < b[i]) {
                return -1;
            }
        }
        return 0;
    }
}
BigInt sub(BigInt a, BigInt b) {
    BigInt c;
    for (int i = 0; i < a.size() || i < b.size(); i++) {
        int bDigit = i < b.size() ? b[i] : 0;
        if (a[i] < bDigit) {
            a[i + 1]--;
            a[i] += 10;
        }
        c.push_back(a[i] - bDigit);
    }
     while (c.size() > 1 && c.back() == 0) {
        c.pop_back();
    }
    return c;
}

void print(BigInt a) {
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        cout << a[i];
    }
}

int main() {
    string nums1, nums2;
    cin >> nums1 >> nums2;
    BigInt a = toBigInt(nums1);
    BigInt b = toBigInt(nums2);
     if (compare(a, b) >= 0) {
        print(sub(a, b));
    } else {
        cout << "-";
        print(sub(b, a));
    }
    return 0;
}
cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<int> BigInt;

BigInt toBigInt(string nums) {
    BigInt result;
    for (int i = (int)nums.length() - 1; i >= 0; i--) {
        result.push_back(nums[i] - '0');
    }
    return result;
}
int compare(BigInt a, BigInt b) {
    if (a.size() > b.size()) {
        return 1;
    } else if (a.size() < b.size()) {
        return -1;
    } else {
        for (int i = (int)a.size() - 1; i >= 0; i--) {
            if (a[i] > b[i]) {
                return 1;
            } else if (a[i] < b[i]) {
                return -1;
            }
        }
        return 0;
    }
}
BigInt mul(BigInt a, int b) {
    BigInt c;
    int carry=0;;
    for (int i = 0; i < a.size(); i++) {
        int temp=a[i]*b+carry;
        c.push_back(temp%10);
        carry=temp/10;
    }
    while(carry!=0){
        c.push_back(carry%10);
        carry/=10;
    }
    while (c.size() > 1 && c.back() == 0) {
        c.pop_back();
    }
    return c;
}

void print(BigInt a) {
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        cout << a[i];
    }
}

int main() {
     string nums;
    int b;
    cin >> nums >> b;
    BigInt a = toBigInt(nums);
    print(mul(a, b));
    return 0;
    return 0;
}
cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<int> BigInt;

BigInt toBigInt(string nums) {
    BigInt result;
    for (int i = (int)nums.length() - 1; i >= 0; i--) {
        result.push_back(nums[i] - '0');
    }
    return result;
}

BigInt mul(BigInt a, BigInt b) {
    BigInt c = BigInt(a.size() + b.size() + 1, 0);
    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < b.size(); j++) {
            c[i + j] += a[i] * b[j];
        }
    }
    for (int i = 0; i < a.size() + b.size(); i++) {
        if (c[i] >= 10) {
            c[i + 1] += c[i] / 10;
            c[i] = c[i] % 10;
        }
    }
    while (c.size() > 1 && c.back() == 0) {
        c.pop_back();
    }
    return c;
}

void print(BigInt a) {
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        cout << a[i];
    }
}

int main() {
    string nums1, nums2;
    cin >> nums1 >> nums2;
    BigInt a = toBigInt(nums1);
    BigInt b = toBigInt(nums2);
    print(mul(a, b));
    return 0;
}

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef vector<int> BigInt;

BigInt toBigInt(string nums) {
    BigInt result;
    for (int i = (int)nums.length() - 1; i >= 0; i--) {
        result.push_back(nums[i] - '0');
    }
    return result;
}

BigInt div(BigInt a, int b, int &r) {
    BigInt c;
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        r = r * 10 + a[i];
        c.push_back(r / b);
        r = r % b;
    }
    reverse(c.begin(), c.end());
    while (c.size() > 1 && c.back() == 0) {
        c.pop_back();
    }
    return c;
}

void print(BigInt a) {
    for (int i = (int)a.size() - 1; i >= 0; i--) {
        cout << a[i];
    }
}

int main() {
    string nums;
    int b, r = 0;
    cin >> nums >> b;
    if (b == 0) {
        cout << "undefined";
        return 0;
    }
    BigInt a = toBigInt(nums);
    BigInt q = div(a, b, r);
    print(q);
    cout << " " << r;
    return 0;
}

5.7扩展欧几里得算法

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

int main() {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf(c % gcd(a, b) == 0 ? "Yes" : "No");
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

int exGcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exGcd(b, a % b, x, y);
    int temp = x;
    x = y;
    y = temp - a / b * y;
    return d;
}

int main() {
    int a, b, x, y;
    scanf("%d%d", &a, &b);
    int d = exGcd(a, b, x, y);
    int step = b / d;
    int minX = (x % step + step) % step;
    printf("%d %d", minX, (d - a * minX) / b);
    return 0;
}

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

int exGcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exGcd(b, a % b, x, y);
    int temp = x;
    x = y;
    y = temp - a / b * y;
    return d;
}

int solve(int a, int b, int c) {
    int x, y;
    int d = exGcd(a, b, x, y);
    if (c % d) {
        return -1;
    } else {
        int step = abs(b / d);
        int minX = (c * x / d % step + step) % step;
        return minX;
    }
}

int main() {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    int minX = solve(a, b, c);
    if (minX == -1) {
        printf("No Solution");
    } else {
        printf("%d %d", minX, (c - a * minX) / b);
    }
    return 0;
}

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

int exGcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exGcd(b, a % b, x, y);
    int temp = x;
    x = y;
    y = temp - a / b * y;
    return d;
}

int solve(int a, int b, int c) {
    int x, y;
    int d = exGcd(a, b, x, y);
    if (c % d) {
        return -1;
    } else {
        int step = abs(b / d);
        int minX = (c * x / d % step + step) % step;
        return minX;
    }
}

int main() {
    int a, c, m, x, y;
    scanf("%d%d%d", &a, &c, &m);
    int minX = solve(a, m, c);
    if (minX == -1) {
        printf("No Solution");
    } else {
        printf("%d", minX);
    }
    return 0;
}

cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

int exGcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exGcd(b, a % b, x, y);
    int temp = x;
    x = y;
    y = temp - a / b * y;
    return d;
}

int invert(int a, int m) {
    int x, y;
    int d = exGcd(a, m, x, y);
    if (d != 1) {
        return -1;
    } else {
        return (x % m + m) % m;
    }
}

int main() {
    int a, m;
    scanf("%d%d", &a, &m);
    int result = invert(a, m);
    if (result == -1) {
        printf("No Solution");
    } else {
        printf("%d", result);
    }
    return 0;
}
cpp 复制代码
#include <cstdio>
#include <algorithm>
using namespace std;

int exGcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exGcd(b, a % b, x, y);
    int temp = x;
    x = y;
    y = temp - a / b * y;
    return d;
}

int invert(int a, int m) {
    int x, y;
    int d = exGcd(a, m, x, y);
    if (d != 1) {
        return -1;
    } else {
        return (x % m + m) % m;
    }
}

int main() {
    int n, a, m, b;
    scanf("%d%d%d", &n, &a, &m);
    int result = invert(abs(a), m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &b);
        result = (result * b) % m;
    }
    printf("%d", result);
    return 0;
}

5.8组合数

cpp 复制代码
#include <cstdio>
int cal(int n,int p)
{
    if(n<p)return 0;
    return n/p+cal(n/p,p);

}
int main() {
    int n,p=2;
    scanf("%d", &n);
    printf("%d", cal(n,p));
    return 0;
}
cpp 复制代码
#include <cstdio>

typedef long long LL;

LL C(LL n, LL m) {
    LL ans = 1;
    for (LL i = 1; i <= m; i++) {
        ans = ans * (n - m + i) / i;
    }
    return ans;
}

int main() {
    LL n, m;
    scanf("%lld%lld", &n, &m);
    printf("%lld", C(n, m));
    return 0;
}
相关推荐
爱吃生蚝的于勒2 分钟前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~5 分钟前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
王哈哈^_^31 分钟前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
星沁城33 分钟前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵
脉牛杂德1 小时前
多项式加法——C语言
数据结构·c++·算法
legend_jz1 小时前
STL--哈希
c++·算法·哈希算法
kingmax542120081 小时前
初三数学,最优解问题
算法
一直学习永不止步1 小时前
LeetCode题练习与总结:赎金信--383
java·数据结构·算法·leetcode·字符串·哈希表·计数
cuisidong19972 小时前
5G学习笔记三之物理层、数据链路层、RRC层协议
笔记·学习·5g
乌恩大侠2 小时前
5G周边知识笔记
笔记·5g