算法刷题笔记

刷题笔记 - November 2023

2024-01-03  本文已影响0人  Du1in9

1. 成绩分析, 知识点:数学、模拟

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int main() {
    int n, temp;
    cin >> n;
    vector<int> grade(n);
    for(int i = 0; i < n; i++) {
        cin >> grade[i];
    }
    for(int i = 0; i < n - 1; i++) {
        for(int j = i; j < n; j++) {
            if(grade[j] < grade[i]) {
                temp = grade[i];
                grade[i] = grade[j];
                grade[j] = temp;
            }
        }
    }
    cout << grade[n - 1] << endl << grade[0] << endl;
    
    double totalGrade = 0;
    for(int i = 0; i < n; i++) {
        totalGrade += grade[i];
    }
    double result = ((totalGrade / n) * 100) / 100;
    cout << fixed << setprecision(2) << result;
    return 0;
}

2. 合法日期, 知识点:数学、模拟

#include <iostream>

using namespace std;

int main() {
    int m, d;
    cin >> m >> d;
    if(m < 1 || m > 12 || d < 1 || d > 31) {
        cout << "no" << endl;
    }
    else if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
        cout << "yes" << endl;
    }
    else if(m == 4 || m == 6 || m == 9 || m == 11) {
        if(d > 30) {
            cout << "no" << endl;
        } else {
            cout << "yes" << endl;
        }       
    }
    else if(m == 2) {
        if(d > 28) {
            cout << "no" << endl;
        } else {
            cout << "yes" << endl;
        }
    }
    return 0;
}

3. 时间加法, 知识点:数学、模拟

#include <iostream>

using namespace std;

int main() {
    int a, b, t, hour, minute;
    cin >> a >> b >> t;
    
    hour = (b + t) / 60;
    minute = (b + t) % 60; 
    if(hour > 0) {
        if(a + hour > 23) {
            cout << (a + hour) % 24 << endl << minute << endl;
        } else {
            cout << a + hour << endl << minute << endl;
        }
    } else {
        cout << a << endl << b + t << endl;
    }
    return 0;
}

4. 扫雷, 知识点:搜索、模拟

#include <iostream>
#include <vector>

using namespace std;

int n, m;

int BoomNumber(vector<vector<int>> &Array, int i, int j);

int main() {
    cin >> n >> m;
    vector<vector<int>> Array(n, vector<int> (m, 0));
    vector<vector<int>> Flag(n, vector<int> (m, 0));
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> Array[i][j];
        }
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(Array[i][j] == 1) {
                Flag[i][j] = 9;
            } else {
                Flag[i][j] = BoomNumber(Array, i, j);
            }
        }
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cout << Flag[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

int BoomNumber(vector<vector<int>> &Array, int i, int j) {
    int flag = 0;
    if(i - 1 >= 0 && Array[i - 1][j])   flag += 1;
    if(i + 1 < n && Array[i + 1][j])    flag += 1;
    if(j - 1 >= 0 && Array[i][j - 1])   flag += 1;
    if(j + 1 < m && Array[i][j + 1])    flag += 1;
    if(i - 1 >= 0 && j - 1 >= 0 && Array[i - 1][j - 1]) flag += 1;
    if(i - 1 >= 0 && j + 1 < m && Array[i - 1][j + 1])  flag += 1;
    if(i + 1 < n && j - 1 >= 0 && Array[i + 1][j - 1])  flag += 1;
    if(i + 1 < n && j + 1 < m && Array[i + 1][j + 1])   flag += 1;
    return flag;
}

5. 大写, 知识点:字符串

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s;
    cin >> s;
    for(int i = 0; i < s.length(); i++) {
        if(s[i] >= 'a' && s[i] <= 'z') {
            s[i] -= 'a' - 'A';
        }
    }
    cout << s;
    return 0;
}

6. 标题统计, 知识点:字符串

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s;
    int blank = 0;
    
    getline(cin, s);
    for(int i = 0; i < s.length(); i++) {
        if(s[i] == ' ') {
            blank ++;
        }
    }
    cout << s.length() - blank;
    return 0;
}

7. 求和, 知识点:数学

#include <iostream>

using namespace std;

int main() {
    int count = 0;
    count += 1;
    count += 2;
    count += 9;
    for(int i = 10; i <= 99; i++) {
        if(i % 10 == 1 || i / 10 == 1) {
            count += i;
            continue;
        } else if(i % 10 == 2 || i / 10 == 2) {
            count += i;
            continue;
        } else if(i % 10 == 9 || i / 10 == 9) {
            count += i;
            continue;
        } else if(i % 10 == 0) {
            count += i;
            continue;           
        }
    }
    for(int i = 100; i <= 999; i++) {
        if(i % 10 == 1 || i % 100 - i % 10 == 10 || i / 100 == 1) {
            count += i;
            continue;
        } else if(i % 10 == 2 || i % 100 - i % 10 == 20 || i / 100 == 2) {
            count += i;
            continue;
        } else if(i % 10 == 9 || i % 100 - i % 10 == 90 || i / 100 == 9) {
            count += i;
            continue;
        } else if(i % 10 == 0 || i % 100 - i % 10 == 0) {
            count += i;
            continue;           
        }
    }
    for(int i = 1000; i <= 2019; i++) {
        count += i;
    }
    cout << count << endl;
    return 0;
}

8. 天数, 知识点:数学

#include <iostream>

using namespace std;

int main() {
    int m;
    cin >> m;
    if(m == 2) {
        cout << "28" << endl;
    } else if(m == 4 || m == 6 || m == 9 || m == 11) {
        cout << "30" << endl;
    } else {
        cout << "31" << endl;
    }
    return 0;
}

9. 最大间隙, 知识点:数学、排序

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> Array(n, -1);
    for(int i = 0; i < n; i++) {
        cin >> Array[i];
    }
    int max = Array[1] - Array[0];
    for(int i = 1; i < n - 1; i++) {
        if(Array[i + 1] - Array[i] > max) {
            max = Array[i + 1] - Array[i];
        }
    }
    cout << max << endl;
    return 0;
}

10. 金币, 知识点:数学

#include <iostream>

using namespace std;

int main() {
    int K, N, day;
    cin >> K;
    int count = 0, money = 0;
    int i = 1;
    for(int i = 1; ; i++) {
        count += i;
        if(count > K) {
            N = i - 1;
            day = K - (count - i);
            break;
        }
    }
    for(int i = 1; i <= N; i++) {
        money += i * i;
    }
    money += day * (N + 1);
    cout << money << endl;
    return 0;
}

11. 天干地支, 知识点:数学

#include <iostream>

using namespace std;

int main() {
    int year;
    cin >> year;
    int choice1 = year % 10;
    switch (choice1) {
        case 0:
            cout << "geng";
            break;
        case 1:
            cout << "xin";
            break;
        case 2:
            cout << "ren";
            break;
        case 3:
            cout << "gui";
            break;
        case 4:
            cout << "jia";
            break;
        case 5:
            cout << "yi";
            break;
        case 6:
            cout << "bing";
            break;
        case 7:
            cout << "ding";
            break;
        case 8:
            cout << "wu";
            break;
        case 9:
            cout << "ji";
            break;
    }
    int choice2 = year % 12;
    switch (choice2) {
        case 0:
            cout << "shen";
            break;
        case 1:
            cout << "you";
            break;
        case 2:
            cout << "xu";
            break;
        case 3:
            cout << "hai";
            break;
        case 4:
            cout << "zi";
            break;
        case 5:
            cout << "chou";
            break;
        case 6:
            cout << "yin";
            break;
        case 7:
            cout << "mao";
            break;
        case 8:
            cout << "chen";
            break;
        case 9:
            cout << "si";
            break;
        case 10:
            cout << "wu";
            break;
        case 11:
            cout << "wei";
            break;
    }
    return 0;
}

12. 明明的随机数, 知识点:排序

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

int main() {
    int n, temp;
    cin >> n;
    vector<int> Array(n, 0);
    
    for(int i = 0; i < n; i++) {
        Array[i] = rand() % 1000 + 1;
        cout << Array[i] << " ";
    }
    for(int i = 0; i < n - 1; i++) {
        for(int j = i; j < n; j++) {
            if(Array[j] < Array[i]) {
                temp = Array[i];
                Array[i] = Array[j];
                Array[j] = temp;
            }
        }
    }
    cout << endl;
    for(int i = 0; i < n; i++) {
        cout << Array[i] << " ";
    }
    return 0;
}

13. 灌溉, 知识点:图论、模拟

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, m, t, K;
    int i, j, k, r, c;
    cin >> n >> m;
    cin >> t;
    vector<vector<int>> Flower(n, vector<int> (m,0));
    vector<vector<int>> Flag(n, vector<int> (m,0));
    
    for(i = 0; i < t; i++) {
        cin >> r >> c;
        Flower[r - 1][c - 1] = 1;
    }
    cin >> K;
    
    Flag = Flower;
    // 更新灌溉情况 K 天
    for(k = 0; k < K; k++) {
        cout << "第" << k + 1 << "天" << endl;
        // 找出已灌溉,并更新相邻方格
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                if(Flag[i][j] == 1) {
                    if(i - 1 >= 0 && Flower[i - 1][j] == 0) {
                        Flower[i - 1][j] = 1;
                    }
                    if(i + 1 < n && Flower[i + 1][j] == 0) {
                        Flower[i + 1][j] = 1;
                    }
                    if(j - 1 >= 0 && Flower[i][j - 1] == 0) {
                        Flower[i][j - 1] = 1;
                    }   
                    if(j + 1 < m && Flower[i][j + 1] == 0) {
                        Flower[i][j + 1] = 1;
                    }   
                }
            }
        }
        Flag = Flower;
    }
    
//  for(i = 0; i < n; i++) {
//      for(j = 0; j < m; j++) {
//          cout << Flower[i][j] << " ";
//      }
//      cout << endl;
//  }
    int count = 0;
    for(i = 0; i < n; i++) {
        for(j = 0; j < m; j++) {
            if(Flower[i][j] == 1) {
                count++;
            }
        }
    }
    cout << count;
    return 0;
}

14. 特殊日期, 知识点:数学

#include <iostream>

using namespace std;

bool dayExist(int y, int m, int d) {
    if(m == 4 || m == 6 || m == 9 || m == 11) {
        if(d > 30) {
            return false;
        }
    }
    if(m == 2) {
        if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            if(d > 29) {
                return false;
            }
        } else {
            if(d > 28) {
                return true;
            }           
        }
    
    }
    return true;
}

int add(int x) {
    int result = 0;
    while(x != 0) {
        result += x % 10;
        x /= 10;
    }
    return result;
}

int main() {
    int count = 0;
    for(int y = 1900; y <= 9999; y++) {
        for(int m = 1; m <= 12; m++) {
            for(int d = 1; d <= 31; d++) {
                if(dayExist(y, m, d) && add(y) == add(m) + add(d)) {
                    count ++;
//                  cout << "特殊日期:" << y << "-" << m << "-" << d << endl;                   
                }
            }
        }
    }
    cout << count << endl;
    return 0;
}

15. 最大距离, 知识点:数学、贪心

#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> Array(n, 0);
    
    for(int i = 0; i < n; i++) {
        cin >> Array[i];
    }
    int max = 0;
    for(int i = 0; i < n - 1; i++) {
        for(int j = i; j < n; j++) {
            if(max < abs(i - j) + abs(Array[i] - Array[j])) {
                max = abs(i - j) + abs(Array[i] - Array[j]);
            }
        }
    }
    cout << max << endl;
    return 0;
}

16. 最长递增, 知识点:动态规划、贪心

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, temp = 1, max = 1;
    cin >> n;
    vector<int> Array(n, 0);
    
    for(int i = 0; i < n; i++) {
        cin >> Array[i];
    }
    for(int i = 0; i < n - 1; i++) {
        if(Array[i + 1] > Array[i]) {
            temp++;
            if(temp > max) {
                max = temp;
            }
        } else {
            temp = 1;
        }
    }
    cout << max << endl;
    return 0;
}

17. 串的处理, 知识点:字符串

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s;
    char c;
    getline(cin, s);
    
    // 先删除多余空格
    for(int i = 0; i < s.length() - 1; i++) {
        if(s[i] == ' ' && s[i + 1] == ' ') {
            s.erase(i, 1);
            i--;
        }
    }
    // 再插入下划线
    for(int i = 0; i < s.length() - 1; i++) {
        if(('a' <= s[i] && s[i] <= 'z' && '0' <= s[i + 1] && s[i + 1] <= '9') ||
            ('0' <= s[i] && s[i] <= '9' && 'a' <= s[i + 1] && s[i + 1] <= 'z')) {
            s.insert(i + 1, "_");
            i++;
        }
    }   
    // 最后首字母大写
    if('a' <= s[0] && s[0] <= 'z') {
        s[0] -= 'a' - 'A';
    }
    for(int i = 1; i < s.length(); i++) {
        if(s[i] == ' ' && 'a' <= s[i + 1] && s[i + 1] <= 'z') {
            s[i + 1] -= 'a' - 'A';
        }
    }
    cout << s;  
    return 0;
}

18. 幸运数字, 知识点:数学、枚举

#include <iostream>
#include <sstream>
#include <bitset>

using namespace std;

bool luckyNumber_0(int x) {
    int n = 0;
    int temp = x;
    while(temp != 0) {
        n += temp % 10;
        temp /= 10;
    }
    if(x % n != 0) {
        return false;
    }   
    return true;
}

bool luckyNumber_1(int x) {
    string s;
    ostringstream S;
    S << bitset<64>(x);
    s = S.str();
    for(int i = 0; i < s.length() - 1; i++) {
        if(s[i] == '0') {
            s.erase(i, 1);
            i--;
        } else {
            break;
        }
    }
    int n = 0;
    for(int i = 0; i < s.length(); i++) {
        n += s[i] - '0';
    }
    if(x % n != 0) {
        return false;
    }   
    return true;
}

bool luckyNumber_2(int x) {
    string s;
    ostringstream S;
    S << oct << x;
    s = S.str();
    int n = 0;
    for(int i = 0; i < s.length(); i++) {
        n += s[i] - '0';
    }
    if(x % n != 0) {
        return false;
    }   
    return true;
}

bool luckyNumber_3(int x) {
    string s;
    ostringstream S;
    S << hex << x;
    s = S.str();
    int n = 0;
    for(int i = 0; i < s.length(); i++){
        if(s[i] <= '9') {
            n += s[i] - '0';
        } else {
            n += s[i] - 'a' + 10;
        }
    }
    if(x % n != 0) {
        return false;
    }   
    return true;
}

int main() {
    int count = 0, i;
    for(i = 1; ; i++) {
        if(luckyNumber_0(i) && luckyNumber_1(i) && luckyNumber_2(i) && luckyNumber_3(i)) {
            count++;
//          cout << "第" << count << "个幸运数字:" << i << endl;
            if(count == 2023) {
                break;
            }
        }
    }
    cout << i--;
    return 0;
}

19. 缩位求和, 知识点:数学、模拟

#include <iostream>

using namespace std;

int main() {
    int x;
    cin >> x;
    int n = 9999;
    while(n >= 10) {
        n = 0;
        while(x != 0) {
            n += x % 10;
            x /= 10;
        }
        x = n;
        cout << "n = " << n << endl;
    }
    cout << n;
    return 0;
}

20. ISBN 号码, 知识点:数学、字符串

#include <iostream>

using namespace std;

int main() {
    string s, S;
    getline(cin, s);
    S = s;
    
    for(int i = 0; i < s.length(); i++) {
        if(s[i] == '-') {
            s.erase(i, 1);
            i--;
        }
    }
//  cout << s << endl;
    int count = 0;
    for(int i = 0; i < 9; i++) {
        count += (s[i] - '0') * (i + 1);
    }
//  cout << "count = " << count << ", count % 11 = " << count % 11 << endl;
    if(count % 11 == s[9] - '0') {
        cout << "Right" << endl;
    } else {
        S.erase(S.length() - 1, 1);
        S.append(1, count % 11 + '0');
        cout << S << endl;
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读