PAT甲级刷题记录-(AcWing)-Day09数学(8题)

PAT甲级刷题记录-(AcWing)-Day08图论(8题)

课程来源AcWing
其中AcWing中的题目为翻译好的中文题目

1019 General Palindromic Number

AcWing链接
PAT链接

注意点
PAT上没有关于0的判断, 其他的思路不难, 先转换进制然后再判断一下输出就好

#include <iostream>
#include <vector>

using namespace std;
vector<int> res;

bool check(int n, int b) {
    
    
    if (n == 0) {
    
    
        res.push_back(0);
        return true;
    }
    while (n) res.push_back(n % b), n = n / b;
    for (int i = 0, j = res.size() - 1; i < j; ++i, --j) {
    
    
        if (res[i] != res[j])
            return false;
    }
    return true;
}

int main() {
    
    
    int n, b;
    cin >> n >> b;
    if (check(n, b)) puts("Yes");
    else puts("No");
    cout << res[res.size() - 1];
    for (int i = res.size() - 2; i >= 0; i--) {
    
    
        cout << " " << res[i];
    }
    return 0;
}

1049 Counting Ones

AcWing链接
PAT链接

英语单词

解析
具体可以看<编程之美>的2.4节内容
编程之美链接

链接:https://pan.baidu.com/s/1bIRMzUfSCyIqFAkwI4qEug?pwd=sbbi
提取码:sbbi
–来自百度网盘超级会员V3的分享

注意点

#include <iostream>
#include <vector>

using namespace std;

int main() {
    
    
    int n;
    cin >> n;
    vector<int> num;
    int res = 0;
    // 使用vector记录下数字n的每一位
    while (n) num.push_back(n % 10), n = n / 10;
    // 计算每一位中1的个数
    for (int i = num.size()-1; i >= 0; i--) {
    
    
        int d = num[i];
        int high = 0, low = 0, power = 1; // high记录d左边高位的个数,low记录d右边低位的个数,power来记录要乘10的几次
        for (int j = num.size() - 1; j > i; j--) {
    
    
            // 算高位high有几个
            high = high * 10 + num[j];
        }
        for (int j = i - 1; j >= 0; j--) {
    
    
            // 算低位有几个,并记录位数(10的几次)
            low = low * 10 + num[j];
            power *= 10;
        }
        if (d == 0) res += high * power;
        else if (d == 1) res += high * power + low + 1;
        else res += (high + 1) * power;
    }
    cout << res << endl;
    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int cal(int n) {
    
    
    int res = 0;
    vector<int> num;
    for (auto &c:to_string(n)) {
    
    
        num.push_back(c - '0');
    }
    for (int i = 0; i < num.size(); i++) {
    
    
        int d = num[i];
        int left = 0, right = 0, power = 1;
        for (int j = 0; j < i; j++) {
    
    
            left = left * 10 + num[j];
        }
        for (int j = i + 1; j <num.size(); ++j) {
    
    
            right = right * 10 + num[j];
            power *= 10;
        }
        if (d == 0) res += left * power;
        else if (d == 1) res += left * power + right + 1;
        else res += (left + 1) * power;
    }
    return res;
}

int main() {
    
    
    int n;
    cin >> n;
    cout << cal(n) << endl;
    return 0;
}

1059 Prime Factors

AcWing链接
PAT链接

注意点
要注意最后结束循环的时候剩下的那个数也是质数,要一起输出

#include <iostream>

using namespace std;

int main() {
    
    
    int n;
    cin >> n;
    cout << n << "=";
    bool is_first = true;
    for (int i = 2; i <= n / i; ++i) {
    
    
        if (n % i == 0) {
    
    
            int k = 0;
            while (n % i == 0) n = n / i, k++;
            if (is_first) is_first = false;
            else cout << "*";
            if (k > 1)
                cout << i << "^" << k;
            else
                cout << i;
        }
    }
    if (is_first) cout << n << endl;
    else if (n != 1) cout << "*" << n << endl;
    return 0;
}

1081 Rational Sum

AcWing链接
PAT链接

英语单词

  • numerators and denominators 分子和分母
  • rational numbers 有理数

注意点
用long long来存储,然后在运算的过程中多求约分

#include <iostream>

using namespace std;
typedef long long LL;
const int N = 110;

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

int main() {
    
    
    int n;
    scanf("%d", &n);
    LL a = 0, b = 1, t;
    for (int i = 0; i < n; ++i) {
    
    
        LL c, d;
        scanf("%lld/%lld", &c, &d);
        t = gcd(b, d);
        a = d / t * a + b / t * c;
        b = b / t * d;
        t = gcd(a, b);
        a /= t, b /= t;
    }
    if (b == 1) cout << a;
    else {
    
    
        if (a >= b) printf("%lld ", a / b);
        printf("%lld/%lld", a % b, b);
    }
    return 0;
}

1096 Consecutive Factors

AcWing链接
PAT链接

英语单词

解析

注意点
这里要注意到

#include <iostream>
#include <vector>

using namespace std;

int main() {
    
    
    int n;
    cin >> n;
    vector<int> res;
    for (int i = 2; i <= n / i; ++i) {
    
    
        vector<int> seq;
        if (n % i == 0) {
    
    
            int t = n;
            int j = i;
            while (t % j == 0) {
    
    
                seq.push_back(j);
                t = t / j++;
            }
            if (seq.size() > res.size()) res = seq;
        }
    }
    if (res.empty()) {
    
    
        res.push_back(n);
    }
    cout << res.size() << endl;
    cout << res[0];
    for (int i = 1; i < res.size(); ++i) {
    
    
        cout << "*" << res[i];
    }
    return 0;
}

1103 Integer Factorization

AcWing链接
PAT链接

英语单词

  • factorization 因式分解

解析

注意点
这题用DP做,还没学,今天先空着


1104 Sum of Number Segments

AcWing链接
PAT链接

英语单词

  • consecutive 连续的

解析
找规律,找每个点会被加几次

注意点
double的精度不太够,long double就可以了

#include<iostream>
#include <cstdio>

using namespace std;

int main() {
    
    
    int n;
    cin >> n;
    long double res = 0;
    for (int i = 1; i <= n; ++i) {
    
    
        long double x;
        cin >> x;

        res += x * i * (n - i + 1);
    }
    printf("%.2Lf\n", res);
    return 0;
}

1088 Rational Arithmetic

AcWing链接
PAT链接

注意点

#include <iostream>
#include <cstring>

typedef long long LL;
using namespace std;
LL a, b, c, d;

LL gcd(LL x, LL y) {
    
    
    return y ? gcd(y, x % y) : x;
}

void print(LL res_a, LL res_b) {
    
    
    if (res_b < 0) res_a *= -1, res_b *= -1;
    LL t = gcd(abs(res_a), res_b);
    res_a /= t, res_b /= t;
    if (res_a < 0) printf("(");
    if (res_b == 0) printf("Inf");
    else if (res_a == 0) printf("0");
    else if (res_b == 1) printf("%lld", res_a);
    else if (abs(res_a) > res_b) printf("%lld %lld/%lld", res_a / res_b, abs(res_a) % res_b, res_b);
    else printf("%lld/%lld", res_a, res_b);
    if (res_a < 0) printf(")");
}

//void print_one(int num1,int num2) {
    
    
//    if (num2 == 0) printf("Inf\n");
//    else if (num1 == 0) printf("0\n");
//    else if (num2 == 1) printf("%lld\n", num1);
//    else if (num1 > num2) printf("%lld %lld/%lld\n", num1 / num2, num1 % num2, num2);
//    else printf("%lld/%lld\n", num1, num2);
//
//}

void add() {
    
    
    print(a, b);
    printf(" + ");
    print(c, d);
    printf(" = ");
    LL res_a = 0, res_b = 0;
    LL t = gcd(b, d);
    res_a = d / t * a + b / t * c;
    res_b = b / t * d;
    print(res_a, res_b);
    printf("\n");
}

void sub() {
    
    
    print(a, b);
    printf(" - ");
    print(c, d);
    printf(" = ");
    LL res_a = 0, res_b = 0;
    LL t = gcd(b, d);
    res_a = d / t * a - b / t * c;
    res_b = b / t * d;
    print(res_a, res_b);
    printf("\n");
}

void mul() {
    
    
    print(a, b);
    printf(" * ");
    print(c, d);
    printf(" = ");
    LL res_a = 0, res_b = 0;
    res_a = a * c;
    res_b = b * d;
    print(res_a, res_b);
    printf("\n");
}

void div() {
    
    
    print(a, b);
    printf(" / ");
    print(c, d);
    printf(" = ");
    LL res_a = 0, res_b = 0;
    res_a = a * d;
    res_b = b * c;
    print(res_a, res_b);
    printf("\n");
}

int main() {
    
    
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
    add();
    sub();
    mul();
    div();
    return 0;
}

模板

AcWing链接
PAT链接

英语单词

解析

注意点


猜你喜欢

转载自blog.csdn.net/Weary_PJ/article/details/124983493