c ++ 重要内容

1.画菱形

#include <iostream>

using namespace std;

int main()
{
    int n;
    while (cin >> n)
    {
            for (int i = 0; i < n; i ++ )
        {
            for (int j = 0; j < n; j ++ )
            {
                if (abs(i - n / 2) + abs(j - n / 2) <= n / 2) printf("*");
                else printf(" ");
            }
                
            printf("\n");
        }
    }
    
    return 0;
}

画菱形重要公式

if (abs(i - n / 2) + abs(j - n / 2) <= n / 2) printf("*");
                else printf(" ");

2.月利率

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

// 定义普通函数计算剩余欠款
double remainAfterMonths(double a, int b, double c, double x) {
    double remain = a;
    for (int i = 0; i < b; i++) {
        remain = remain * (1.0 + x) - c;
    }
    return remain;
}

int main() {
    double a, c;
    int b;
    cin >> a >> b >> c;
    
    double left = 0.0, right = 1.0, mid;
    
    for (int i = 0; i < 100; i++) {
        mid = (left + right) / 2.0;
        double rem = remainAfterMonths(a, b, c, mid); // 调用普通函数
        if (rem > 0) {
            right = mid;
        } else {
            left = mid;
        }
    }
    
    cout << fixed << setprecision(3) << mid * 100 << "%" << endl;
    return 0;
}

重要知识:用二分法逐渐逼近

 for (int i = 0; i < 100; i++) {
        mid = (left + right) / 2.0;
        double rem = remainAfterMonths(a, b, c, mid); // 调用普通函数
        if (rem > 0) {
            right = mid;
        } else {
            left = mid;
        }
    }

3.大仙来了

#include <iostream>

using namespace std;

bool ruinian(int a)
{
    if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
    {
        return true;
    }
    else
    {
        return false;
    }
}


int main()
{
    int a, b, c;
    
    cin >> a >> b >> c;
    
    int arr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int brr[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    while (a != 0 || b != 0 || c != 0)
    {
        int day = 0;
        for (int i = 1965; i < a; i ++ )
        {
            if (ruinian(i))
            {
                day += 366;
            }
            else
            {
                day += 365;
            }
        }
        
        if (ruinian(a))
        {
            for (int i = 1; i < b; i ++ )
            {
                day += brr[i];
            }
            day += c;
        }
        else
        {
            for (int i = 1; i < b; i ++ )
            {
                day += arr[i];
            }
            day += c;
        }
        
        int ddd = (day + 5 - 1) % 7;
        
        if (ddd == 0)
        {
            cout << 7 << endl;
            cin >> a >> b >> c;
            continue;
        }
        else
        {
            cout << ddd << endl;
        }
        
        cin >> a >> b >> c;
    }
}

重要知识

判断闰年:闰年366,正常年365. 多的一天是2月份,闰年29天。

bool ruinian(int a)
{
    if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
    {
        return true;
    }
    else
    {
        return false;
    }
}

根据给定的1965年1月1日为周五来算1965之后的年月日算星期几。那就先算1965.1.1到目标总共的天数。 比如1965.1.7。显然是7天。根据(5 + 7 - 1)% 7 = 4。所以是星期五。

4.阶乘分解

#include <iostream>
#include <vector>


using namespace std;

int main()
{
    vector <bool> s(101, true);
    vector <int> t;
    s[0] = false;
    s[1] = false;
    
    for (int i = 2; i <= 100; i ++ )
    {
        if (s[i])
        {
            for (int j = i * i; j < 101; j += i)
            {
                s[j] = false;
            }
            t.push_back(i);
        }
    }
    int n;
    
    while (cin >> n)
    {
        int k = n;
        cout << n << "!= ";
        for (int i = 0; i < t.size(); i ++ )
        {
            int sum = 0;
            
            if (n < t[i])
                break;
            while (t[i] <= n)
            {
                n = n / t[i];
                sum += n;
            }
            n = k;
            if (i > 0)
                cout << ' ';
            cout << sum;
        }
        cout << endl;
    }
    
}

重要知识

求质数(素数):

vector <bool> s(101, true);
    s[0] = false;
    s[1] = false;
    
    for (int i = 2; i <= 100; i ++ )
    {
        if (s[i])
        {
            for (int j = i * i; j < 101; j += i)
            {
                s[j] = false;
            }
            t.push_back(i);
        }
    }

假设都是质数,根据质数性质,然后对后面的值赋false,最后把索引对应的是true的值,当成质数弄到另一个数组中就行。

求n阶乘中n以内每个质数的个数。

比如5!就有3个2,1个3,1个5.

while (cin >> n)
    {
        int k = n;
        cout << n << "!= ";
        for (int i = 0; i < t.size(); i ++ )
        {
            int sum = 0;
            
            if (n < t[i])
                break;
            while (t[i] <= n)
            {
                n = n / t[i];
                sum += n;
            }
            n = k;
            if (i > 0)
                cout << ' ';
            cout << sum;
        }
        cout << endl;
    }
  • 计算 n / t[i] 并累加:

    • 5 / 2 = 2 → sum = 2

    • 2 / 2 = 1 → sum = 2 + 1 = 3

    • 1 / 2 = 0(停止)

  • 最终 sum = 35! 包含 2^3

  • 恢复 n = 5

  • 计算 n / t[i]

    • 5 / 5 = 1 → sum = 1

    • 1 / 5 = 0(停止)

  • 最终 sum = 15! 包含 5^1

  • 恢复 n = 5

就这样的过程。

5.买水果

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int t;
    cin >> t;
    
    while (t -- )
    {
        int n;
        string v;
        cin >> n;
        int k;
        map <string, int> s;
        while (n -- )
        {
            cin >> v >> k;
            s[v] = k;
        }
        
        vector <pair<string, int>> vec(s.begin(), s.end());
        
        auto cmp = [](const pair<string, int>& a, const pair<string, int>& b) 
        {
            return a.second < b.second; // 按值升序排序
        };
        
        sort(vec.begin(), vec.end(), cmp);
        for (auto &u : vec)
        {
            cout << u.first << ' ' << u.second << endl;
        }
    }
}

重要知识:

<map>定义:

map <string, int> s;

将字典传入vector中

map <string, int> s;
while (n -- )
        {
            cin >> v >> k;
            s[v] = k;
        }

vector <pair<string, int>> vec(s.begin(), s.end());

用sort对vector <pair<string, int>>排序,要自己定义函数。sort在#include <algorithm>中。

bool cmp(const pair<string, int> &a, const pair<string, int> &b)
{
    return a.second < b.second;
}

sort(vec.begin(), vec.end(), cmp);

6.括号和括号

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;
    string s;
    
    while (n -- )
    {
        cin >> s;
        
        string g;
        
        if (s[0] == ')')
        {
            cout << "No" << endl;
            continue;
        }    
            
        g += s[0];
        
        for (int i = 1; i < s.size(); i ++ )
        {
            if (s[i] == ')' && !g.empty())
            {
                //g.erase(g.end() - 1); // 删除最后一个字符
                g.erase(g.size() - 1, 1); // 从位置 g.size() - 1 开始删除 1 个字符
            }
            else
            {
                g += s[i];
            }
        }
        
        if (g.empty())
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
}

重要知识

处理这问题的思想就是,将()成对删除。假如当前是 (( ,然后下一个是 ) ,那么删掉一个 ( ,继续往下读,不是则继续存起来。如果是成对的,最后肯定是空的字符串,不是空则不成对。

删除字符串中的字符:

g.erase(g.size() - 1, 1); // 从位置 g.size() - 1 开始删除 1 个字符

判断是否为空的字符串:

g.empty()

7.正反画画

#include <iostream>
using namespace std;

int main() {
    int T;
    cin >> T;

    while (T--) {
        int x1, y1, x2, y2, x3, y3;
        cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

        // 计算向量 AB 和 AC 的叉积
        int cross = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);

        // 判断顺序
        if (cross < 0) {
            cout << "Clockwise" << endl;
        } else if (cross > 0) {
            cout << "Counterclockwise" << endl;
        }
    }

    return 0;
}

重要知识

A(x1, y1) B(x2, y2) C(x3, y3)
AB向量(x2 - x1, y2 - y1) AC向量(x3 - x1, y3 - y1)
二维行列式
|x2 - x1 y2 - y1|
|x3 - x1 y3 - y1|
 // 计算向量 AB 和 AC 的叉积
        int cross = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);

8.有趣的进制数

#include <iostream>

using namespace std;

int main()
{
    string n;
    
    while (cin >> n && n != "0")
    {
        int sum = 0;
        for (auto &a : n)
        {
            sum += a - '0';
        }
        int sum1 = stoi(n);
        int k = 0;
        while (sum1 >= 16)
        {
            k += sum1 % 16;
            sum1 = sum1 / 16;
        }
        k += sum1;
        
        int sum2 = stoi(n);
        int kk = 0;
        while (sum2 >= 12)
        {
            kk += sum2 % 12;
            sum2 = sum2 / 12;
        }
        kk += sum2;
        
        if (kk == sum && sum == k)
        {
            cout << n << " is a GOD number."<< endl;
        }
        else
        {
            cout << n << " is not a GOD number."<< endl;
        }
    }
}

重要知识:

n进制转换

while (sum1 >= 16)
        {
            k += sum1 % 16;
            sum1 = sum1 / 16;
        }
        k += sum1;

数字字符串转数字

int sum2 = stoi(n);

stol long
stod double
stoll long long

9.输出Ascii码

#include <iostream>
#include <string>

using namespace std;


int main()
{
    string s = "I want to take the MS's rocket to live in the MS's ice castle.";
    
    //getline(cin, s);
    int i = 0;
    for (auto &t : s)
    {
        if (i > 0) cout << ' ';
        printf("%d", t);
        i ++ ;
    }
}

重要知识:

 printf("%d", t); // 直接打印Ascii
 printf("%c", t); // 直接打印字母

10.荧光屏编程

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



vector<vector<string>> s = 
{
    {"*****", "*---*", "*---*", "*---*", "*****"}, // 0
    {"----*", "----*", "----*", "----*", "----*"}, // 1
    {"*****", "----*", "*****", "*----", "*****"}, // 2
    {"*****", "----*", "*****", "----*", "*****"}, // 3
    {"*---*", "*---*", "*****", "----*", "----*"}, // 4
    {"*****", "*----", "*****", "----*", "*****"}, // 5
    {"*****", "*----", "*****", "*---*", "*****"}, // 6
    {"*****", "----*", "----*", "----*", "----*"}, // 7
    {"*****", "*---*", "*****", "*---*", "*****"}, // 8
    {"*****", "*---*", "*****", "----*", "*****"} // 9
};

int main()
{
    //string h;
    //cin >> h;
    
    int a, b, c, d;
    //int i = 0;
    while (cin >> a >> b >> c >> d)
    {
        //if (i > 0)
            //cout << endl;
        //cout << h << endl;
        for (int i = 0; i < 5; i ++ )
        {
            cout << s[a][i] << ' '
            << s[b][i] << ' '
            << s[c][i] << ' '
            << s[d][i] << endl;
        }
        //cin >> h;
        //i ++ ;
    }
}

重要知识:

直接定义二维向量:

vector<vector<string>> s = 
{
    {"*****", "*---*", "*---*", "*---*", "*****"}, // 0
    {"----*", "----*", "----*", "----*", "----*"}, // 1
    {"*****", "----*", "*****", "*----", "*****"}, // 2
    {"*****", "----*", "*****", "----*", "*****"}, // 3
    {"*---*", "*---*", "*****", "----*", "----*"}, // 4
    {"*****", "*----", "*****", "----*", "*****"}, // 5
    {"*****", "*----", "*****", "*---*", "*****"}, // 6
    {"*****", "----*", "----*", "----*", "----*"}, // 7
    {"*****", "*---*", "*****", "*---*", "*****"}, // 8
    {"*****", "*---*", "*****", "----*", "*****"} // 9
};

根据输入要打印的什么数字。

while (cin >> a >> b >> c >> d)
    {
        for (int i = 0; i < 5; i ++ )
        {
            cout << s[a][i] << ' '
            << s[b][i] << ' '
            << s[c][i] << ' '
            << s[d][i] << endl;
        }
    }

11.AB博弈

#include <iostream>
using namespace std;

int main() 
{
    long long n;
    while (cin >> n) 
    {
        // 题目中的正整数是 n+1
        long long  num = n + 1;
        if (num % 3 != 0) 
        {
            cout << "Brother Chao will get the gold nugget!" << endl;
        } 
        else 
        {
            cout << "PangPang will get the gold nugget!" << endl;
        }
    }
    return 0;
}

给出一个正整数N,A和B轮流可以对这个数进行操作,包括减1操作和减2操作,问最后谁把这个数减成了0。

num % 3 != 0 先手胜利。

12.特殊方程的正整数解

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int n;
    while (t -- )
    {
        cin >> n;
        int g = 1;
        int s;
        while (g * g * g <= n)
        {
            g ++ ;
        }
        if (n == 1)
            s = 1;
        else
            s = g - 1;
        
        bool found = true;
        for (int i = 1; i <= s; i++) {
            for (int j = i; j <= s; j++) { // 让 j >= i 避免重复解
                int a = i * i * i;
                int b = j * j * j;
                
                if (a + b == n) {  // 找到满足条件的解
                    cout << i << " " << j << endl;
                    found = false;
                }
            }
        }
            
        if (found)
        {
            cout << "No Solution" << endl;
        }
    }
}

重要知识:

pow存在误差,所以还是自己i * i * i,这样弄。

13.字符串减法

#include <iostream>
#include <string>
#include <unordered_set>


using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;

    // 使用 unordered_set 存储 s2 中的字符,忽略大小写
    unordered_set<char> s2_chars;
    for (char c : s2) {
        s2_chars.insert(tolower(c));  // 将字符转换为小写存储
    }

    // 用于存储最终结果
    string result;

    // 遍历 s1,对于不在 s2 中的字符加入 result
    for (char c : s1) {
        if (s2_chars.find(tolower(c)) == s2_chars.end()) {
            result += c;  // 如果没有找到对应字符,就加入 result
        }
    }

    // 如果 result 为空,输出 Blank
    if (result.empty()) {
        cout << "Blank" << endl;
    } else {
        cout << result << endl;
    }

    return 0;
}

重要知识:

<set>类型:

// 使用 unordered_set 存储 s2 中的字符,忽略大小写
    unordered_set<char> s2_chars;
    for (char c : s2) {
        s2_chars.insert(tolower(c));  // 将字符转换为小写存储
    }
// 遍历 s1,对于不在 s2 中的字符加入 result
    for (char c : s1) {
        if (s2_chars.find(tolower(c)) == s2_chars.end()) {
            result += c;  // 如果没有找到对应字符,就加入 result
        }
    }

14.数列去重复项

#include <unordered_set>
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

void quick_sort(int l, int r, vector<int>&s)
{
    if (l >= r) return;
    
    int i = l - 1, j = r + 1, x = s[(l + r) / 2];
    
    while (i < j)
    {
        do i ++ ; while (x > s[i]);
        do j -- ; while (x < s[j]);
        if (i < j) swap(s[i], s[j]);
    }
    
    quick_sort(l, j, s);
    quick_sort(j + 1, r, s);
}


int main()
{
    unordered_set <int> s;
    
    int n;
    
    while (scanf("%d", &n) != EOF)
    {
        s.insert(n);
    }
    int i = 0;
    
    vector <int> t(s.begin(), s.end());
    
    quick_sort(0, t.size() - 1, t);
    
    for (auto &u : t)
    {
        if (i > 0) printf(" ");
        printf("%d", u);
        i ++ ;
    }
}

重要知识:

<set> 和 <unordered_set>

<set> 元素自动排序 

<unordered_set> 遍历顺序与插入顺序无关,在平均情况下更快。

都可以用到vector中。

vector <int> t(s.begin(), s.end());

快速排序代码

void quick_sort(int l, int r, vector<int>&s)
{
    if (l >= r) return;
    
    int i = l - 1, j = r + 1, x = s[(l + r) / 2];
    
    while (i < j)
    {
        do i ++ ; while (x > s[i]);
        do j -- ; while (x < s[j]);
        if (i < j) swap(s[i], s[j]);
    }
    
    quick_sort(l, j, s);
    quick_sort(j + 1, r, s);
}

传入一定要这样写 

 quick_sort(0, t.size() - 1, t);

15.打印横向菱形

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

void print_rhombus(int n) {
    vector<vector<char>> grid(n, vector<char>(n, ' ')); // 初始化字符矩阵
    char ch = 'A';  // 记录当前填充的字母
    
    // 填充上边
    for (int i = 0; i < n; i++) {
        grid[0][i] = ch;
        ch = (ch == 'Z') ? 'A' : ch + 1;
    }
    // 填充右边
    for (int i = 1; i < n; i++) {
        grid[i][n - 1] = ch;
        ch = (ch == 'Z') ? 'A' : ch + 1;
    }
    // 填充下边
    for (int i = n - 2; i >= 0; i--) {
        grid[n - 1][i] = ch;
        ch = (ch == 'Z') ? 'A' : ch + 1;
    }
    // 填充左边
    for (int i = n - 2; i > 0; i--) {
        grid[i][0] = ch;
        ch = (ch == 'Z') ? 'A' : ch + 1;
    }
    bool s = true;
    // 输出矩阵
    int k = n - 1;
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
            if (s)
            {
                for (int l = 0; l < k; l ++ )
                {
                    cout << ' ';
                }
            }
            cout << grid[i][j];
            if (j < n - 1) cout << " "; // 保证相邻字母之间有空格e
            
            s = false;
        }
        k -- ;
        cout << endl;
        s = true;
    }
}

int main() {
    int n;
    cin >> n;
    print_rhombus(n);
    return 0;
}

先弄出矩形来,再在前头加空格,然后变成菱形

16.数组峰形特征

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

int main() {
    int n;
    cin >> n;

    while (n--) {
        long long k;
        cin >> k;

        vector<long long> s(k);
        long long maxVal = 0;
        long long maxPos = 0;

        // 读取序列并找到最大值及其位置
        for (long long i = 0; i < k; i++) {
            cin >> s[i];
            if (s[i] > maxVal) {
                maxVal = s[i];
                maxPos = i;
            }
        }

        bool isValid = true;
        
        if (maxPos + 1 == s.size())
            isValid = false;
        
        if (isValid)
        {
            // 检查前半部分是否严格递增
            for (long long i = 1; i <= maxPos; i++) {
                if (s[i] <= s[i - 1]) {
                    isValid = false;
                    break;
                }
            }
        }

        // 检查后半部分是否严格递减
        if (isValid) {
            for (long long i = maxPos + 1; i < k; i++) {
                if (s[i] >= s[i - 1]) {
                    isValid = false;
                    break;
                }
            }
        }

        // 输出结果
        if (isValid) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }

    return 0;
}

重要知识:

vector<long long> s(k);
直接定义一维向量为k大小。

17.数组的相似度

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

int main()
{
    int g;
    
    cin >> g;
    int k;
    map <int, int> s;
    vector <int> t, v;
    
    
    while (g -- )
    {
        cin >> k;
        int y;
        for (int i = 0; i < k; i ++ )
        {
            cin >> y;
            t.push_back(y);
        }
        
        for (int i = 0; i < k; i ++ )
        {
            cin >> y;
            v.push_back(y);
        }
        
        for (int i = 0; i < k; i ++ )
        {
            s[t[i] - v[i]] ++ ;
        }
        int max = 0;
        for (auto &[u, v] : s)
        {
            if (v > max)
            {
                max = v;
            }
        }
        cout << max << endl;
        
        t.clear();
        v.clear();
        s.clear();
    }
    
    
}

俩个数组的相似程度,相减存map里头。

如果不断的要处理数据,要看要不要用clear.

18.我爱浙商大

#include <iostream>
#include <string>


using namespace std;

int main()
{
    string s;
    
    getline(cin, s);
    
    string target = "ZJGSU";
    
    int pos = s.find(target);
    
    while (pos != string::npos)
    {
        s.erase(pos + 2, 1);
        pos = s.find(target);
    }
    
    cout << s;
}
  • 如果是 集合/映射,用 find() == end()

  • 如果是 字符串,用 find() != string::npos

与13的区别。

19.IPv6校园网

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


int main()
{
    string s, t;
    
    while (getline(cin, s))
    {
        for (auto &a : s)
        {
            if (a != ':' && a != '-')
            {
                t += a;
            }
        }
        bool biaozhi = true;
        t.erase(0, 4);
        
        for (char& ch : t) 
        {
            if (isalpha(ch)) 
            { // 如果是字母
                ch = tolower(ch); // 转换为小写
            }
        }
        
        cout << "2001:250:6401::";
        int i;
        for (i = 0; i < 2; i ++ )
        {
            if (i > 0)
            {
                if (biaozhi)
                    cout << ":";
                biaozhi = true;
            }
            int count = 0;
            for (int j = 0; j < t.size(); j ++ )
            {
                if (t[j] == '0')
                {
                    count ++ ;
                }
            }
            
            if (count == 8)
            {
                break;
            }
            
            if (t[0] != '0')
                {
                    cout << t[0] << t[1]  << t[2]  << t[3] ;
                    t.erase(0, 4);
                }
            else if (t[0] == '0' && t[1] == '0' && t[2] == '0' && t[3] == '0' && i == 1)
            {
                cout << '0';
                t.erase(0, 4);
            }
            else if (t[0] == '0' && t[1] == '0' && t[2] == '0' && t[3] == '0')
            {
                biaozhi = false;
                t.erase(0, 4);
            }
            else if (t[0] == '0' && t[1] == '0' && t[2] == '0')
            {
                cout << t[3] ;
                t.erase(0, 4);
            }
            else if (t[0] == '0' && t[1] == '0')
            {
                cout << t[2] << t[3] ;
                t.erase(0, 4);
            }
            else if (t[0] == '0')
            {
                cout << t[1] << t[2] << t[3] ;
                t.erase(0, 4);
            }
        }
        t.clear();
        cout << endl;
    }
    
    
    
}

重要知识;

getline(cin, s);

isalpha(ch);

20.学霸爱上课

#include <iostream>
#include <set>
#include <vector>
#include <string>
using namespace std;

const int DAYS = 7;
const int KE = 13;

int main()
{
    int n;
    cin >> n;
    
    set<string> s;
    
    vector<vector<bool>> schedule(DAYS, vector<bool>(KE, false));
    
    string kecheng;
    int a, b, c;
    while (n -- )
    {
        cin >> kecheng >> a >> b >> c;
        
        s.insert(kecheng);
        
        for (int i = b; i <= c; i ++ )
        {
            schedule[a][i] = true;
        }
    }
    
    cin >> n;
    vector<string> ss;
    
    while (n -- )
    {
        cin >> kecheng;
        cin >> a >> b >> c;
        
        if (s.count(kecheng))
        {
            continue;
        }
        
        bool conflict = false;
        for (int i = b; i <= c; i ++ )
        {
            if (schedule[a][i] == true)
            {
                conflict = true;
                break;
            }
        }
        
        if (!conflict)
        {
                for (int i = b; i <= c; i ++ )
            {
                schedule[a][i] = true;
            }
            ss.push_back(kecheng);
            s.insert(kecheng);
        }
    }
    
    cout << ss.size() << endl;
    
    for (auto &gg : ss)
    {
        cout << gg << endl;
    }
}

重要知识:

count()

二维数组定义

21.时间大师

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;
    string s, t;
    
    double shenzhen = 0.5;
    double fenzhen = 6.0;
    
    
    while (n -- )
    {
        cin >> s >> t;
        
        string g, gg;
        int a, b;
        int i;
        for (i = 0; i < s.size(); i ++ )
        {
            if (s[i] == ':')
                break;
            
            g += s[i];
            a = stoi(g);
            a = a % 12;
        }
        i ++ ;
        for (i; i < s.size(); i ++ )
        {
            
            gg += s[i];
            b = stoi(gg);
        }
        int dushu1 = (a * 60 + b) * shenzhen ;
        int dushu2 = b * fenzhen;
        int dushu3;
        if (dushu1 - dushu2 == 0)
        {
            dushu3 = 0;
        }
        else if (dushu1 - dushu2 < 0)
        {
            dushu3 = dushu2 - dushu1;
        }
        else if (dushu1 - dushu2 > 0)
        {
            dushu3 = dushu1 - dushu2;
        }
        
        if (dushu3 > 180)
            dushu3 = 360 - dushu3;
        g.clear();
        gg.clear();
        
        for (i = 0; i < t.size(); i ++ )
        {
            if (t[i] == ':')
                break;
            
            g += t[i];
            a = stoi(g);
            a = a % 12;
        }
        i ++ ;
        
        for (i; i < t.size(); i ++ )
        {
            
            gg += t[i];
            b = stoi(gg);
        }
        
        int dushu4 = (a * 60 + b) * shenzhen;
        int dushu5 = b * fenzhen;
        int dushu6;
        if (dushu4 - dushu5 == 0)
        {
            dushu6 = 0;
        }
        else if (dushu4 - dushu5 < 0)
        {
            dushu6 = dushu5 - dushu4;
        }
        else if (dushu4 - dushu5 > 0)
        {
            dushu6 = dushu4 - dushu5;
        }
        
        if (dushu6 > 180)
            dushu6 = 360 - dushu6;
        
        if (dushu3 <= dushu6)
            cout << s << endl;
        else
            cout << t << endl;
    }
}

时针和分针是会动的。

时针一分钟动 0.5度。

分钟一分钟动 6度。

因为要小于等于180度,算就是分针的从0开始的度数减去时针从0开始的度数,大于180就减180度。

22.单词统计

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

int main()
{
    string s;
    vector <string> t;
    string g;
    
    while (getline(cin, s))
    {
            for (int i = 0; i < s.size(); i ++ )
        {
            if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
            {
                g += s[i];
            }
            else
            {
                t.push_back(g);
                g.clear();
            }
        }
    }
    int count = 0;
    for(auto &a : t)
    {
        if (a != "")
        {
            count ++ ;
        }
    }
    
    cout << count;
    
    
}

重要知识:

因为这里面有除了空格以及字母以外的字符。所以就这样确定单词

while (getline(cin, s))
    {
            for (int i = 0; i < s.size(); i ++ )
        {
            if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
            {
                g += s[i];
            }
            else
            {
                t.push_back(g);
                g.clear();
            }
        }
    }

还是要确认一下里面的东西,因为,这个里头有空格

    for(auto &a : t)
    {
        if (a != "")
        {
            count ++ ;
        }
    }

23.两个人的游戏

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string name;
    long long a, b;
    for (int i = 0; i < n; i ++ )
    {
        cin >> a >> b >> name;
        
        if (a == b)
        {
            if (name == "gxl")
                cout << "xfs" << endl;
            else
                cout << "gxl" << endl;
        }
        else
        {
            cout << name << endl;
        }
    }
}

现在桌子上有2 堆物品,其中一堆有n件物品,另一堆有m件物品。xfs和gxl轮流从任意一堆中取出至少一件物品,看谁拿到桌子上的最后一件物品,谁就赢。

24.二十二进制加法器

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

// 将字符转换为对应的数字
int charToInt(char c) {
    if (c >= '0' && c <= '9') {
        return c - '0';
    } else if (c >= 'A' && c <= 'Z') {
        return c - 'A' + 10;
    }
    return 0; // 默认值,不会发生
}

int main() {
    string s, t;
    string arr[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};
    vector<int> zhuanhuan;

    while (cin >> s >> t) {
        string g, gg;
        reverse(s.begin(), s.end());
        reverse(t.begin(), t.end());

        if (s.size() < t.size()) {
            swap(s, t);
        }

        // 逐位相加
        for (size_t i = 0; i < t.size(); i++) {
            int a = charToInt(t[i]);
            int b = charToInt(s[i]);
            zhuanhuan.push_back(a + b);
        }

        // 处理 s 比 t 长的部分
        for (size_t i = t.size(); i < s.size(); i++) {
            int b = charToInt(s[i]);
            zhuanhuan.push_back(b);
        }

        // 处理进位
        for (size_t i = 0; i < zhuanhuan.size() - 1; i++) {
            if (zhuanhuan[i] >= 22) {
                zhuanhuan[i] %= 22;
                zhuanhuan[i + 1]++;
            }
        }

        // 处理最高位的进位
        if (zhuanhuan.back() >= 22) {
            zhuanhuan.back() %= 22;
            zhuanhuan.push_back(1);
        }

        // 构建结果字符串
        for (auto& aaa : zhuanhuan) {
            gg = arr[aaa] + gg;
        }

        // 去除高位多余的 '0'
        size_t pos = gg.find_first_not_of('0');
        if (pos == string::npos) {
            gg = "0"; // 如果全为 '0',则结果为 "0"
        } else {
            gg.erase(0, pos); // 去除高位多余的 '0'
        }

        cout << gg << endl;
        gg.clear();
        zhuanhuan.clear();
    }

    return 0;
}

重要知识:

把字符串翻转:

reverse(s.begin(), s.end());

000123

从左往右找,找到第一个非0数。

gg.find_first_not_of('0')

25.停车场收费系统

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;
    
    map <string, int> s;
    string status, car;
    int a, b;
    
    while (n -- )
    {
        cin >> status >> car;
        
        scanf("%d:%d", &a, &b);
        
        if (status == "IN")
        {
            a = a * 60;
            a = a + b;
            s[car] = a;
        }
        else
        {
            int time1;
            a = a * 60 + b;
            time1 = a - s[car];
            if (time1 <= 120)
            {
                cout << car << ' ' << 20 << endl;
            }
            else
            {
                if (time1 % 60 == 0)
                {
                    int num = time1 / 60;
                    cout << car << ' ' << 20 + (num - 2) * 15 << endl;
                }
                else
                {
                    int num = time1 / 60 - 1;
                    cout << car << ' ' << 20 + num * 15 << endl;
                }
            }
        }
    }
}

scanf("%d:%d", &a, &b);

因为没有隔夜,就直接先判断 IN是就用map记录车牌和时间,直接换成分钟

遇到 OUT 直接将时间换成分钟,在用车牌找到入库的时间。

26.简单找中位数

#include <iostream>


using namespace std;

int arr[256];

int main()
{
    int n;
    cin >> n;
    
    int k;
    
    for (int i = 0; i < n * n; i ++ )
    {
        cin >> k;
        arr[k] ++ ;
    }
    
    int count = 0;
    for (int i = 0; i < 256; i ++ )
    {
        count += arr[i];
        
        if (count >= n * n / 2 + 1)
        {
            cout << i << endl;
            break;
        }
    }
    
}

重要知识:

像素值0到255,因此可以用打点法来记录

统计arr[256]的数,达到一半就输出就行了。

27.学长爱吃点心

#include <iostream>
using namespace std;

int main() 
{
    ios::sync_with_stdio(false); // 关闭与 C I/O 库的同步,加速 cin/cout
    cin.tie(0); // 解除 cin 和 cout 绑定,加速输入输出

    long long n, sum = 0;
    cin >> n;
    int k = n;
    while (n--) 
    {
        long long max_val = 0, nn;
        for (long long i = 0; i < k; i++) 
        {
            cin >> nn;
            if (nn > max_val) max_val = nn;
        }
        sum += max_val;
    }

    cout << sum; // '\n' 比 endl 更快,因为 endl 还会刷新缓冲区
    return 0;
}

重要知识:

    ios::sync_with_stdio(false); // 关闭与 C I/O 库的同步,加速 cin/cout
    cin.tie(0); // 解除 cin 和 cout 绑定,加速输入输出

28.成绩管理系统

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct student
{
	string name;
	double chinese;
	double math;
	double english;
	double zongfen;
};


bool cmp(const student &a, const student &b)
{
	if (a.zongfen != b.zongfen) return a.zongfen > b.zongfen;
	if (a.english != b.english) return a.english > b.english;
	if (a.math != b.math) return a.math > b.math;
	return a.name < b.name;
}



int main()
{
	vector<student> s;
	
	int n;
	
	cin >> n;
	string name;
	double a, b, c;
	for (int i = 0; i < n; i ++ )
	{
		cin >> name >> a >> b >> c;
		
		s.push_back({name, a, b, c, a + b + c});
		
	}
	sort(s.begin(), s.end(), cmp);
	
	for (int i = 0; i < s.size(); i ++ )
	{
		cout << i + 1 << ' ' <<  s[i].name << ' ' << s[i].chinese << ' ' << s[i].math << ' ' << s[i].english << ' ' << s[i].zongfen << endl;
	}
} 

重要知识:

定义结构体:

struct student
{
	string name;
	double chinese;
	double math;
	double english;
	double zongfen;
};

结构体类型用sort:

bool cmp(const student &a, const student &b)
{
	if (a.zongfen != b.zongfen) return a.zongfen > b.zongfen;
	if (a.english != b.english) return a.english > b.english;
	if (a.math != b.math) return a.math > b.math;
	return a.name < b.name;
}

vector<student> s:添加数据

for (int i = 0; i < n; i ++ )
	{
		cin >> name >> a >> b >> c;
		
		s.push_back({name, a, b, c, a + b + c});
		
	}

vector<student> s:打印数据

for (int i = 0; i < s.size(); i ++ )
	{
		cout << i + 1 << ' ' <<  s[i].name << ' ' << s[i].chinese << ' ' << s[i].math << ' ' << s[i].english << ' ' << s[i].zongfen << endl;
	}

29.国王揪毒酒

#include <iostream>

using namespace std;

int main()
{
    int n;
    
    while(cin >> n, n != 0)
	{
		int k = 0;
		
		if (n <= 2)
		{
			cout << 1 << endl;
			continue;
		}
		
		while (n > 0)
		{
			n >>= 1;
			k ++ ;
		}
		cout << k << endl;
	} 
}

有 N 桶酒,其中 1 桶有毒,用最少的侍卫在半小时内找出有毒的那一桶。

  • 每个侍卫的生死状态可以看作一个二进制位(0=存活,1=死亡),k 个侍卫可以表示 2^k 种状态。

  • 找到最小的 k,使得 2^k >= N

30.到底谁是弟弟

#include <iostream>
using namespace std;

int main() {
    long long n, m;
    while (cin >> n >> m) {
        if (n == -1 && m == -1) break; // 结束条件

        // 判断先手是否必胜
        if (n % (m + 1) == 1) {
            cout << "fail" << endl;
        } else {
            cout << "win" << endl;
        }
    }
    return 0;
}

有一对双胞胎兄弟,他们俩人都想当哥哥,现在他们想通过一场游戏来决定谁是哥哥谁是弟弟。母亲给他们买了n粒花生,兄弟两人轮流吃花生,一次至少吃一粒,最多吃m粒,吃到最后一粒花生的人就是弟弟!

31.身份证校验器

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
using namespace std;
int main()
{
	int arr[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	char brr[] = {'1','0','X','9','8','7','6','5','4','3','2'};
	
	string sex, password;
	
	int count1 = 0;
	int count2 = 0;
	string line;
	vector<string> s;
	
	while (getline(cin, line))
	{
		if (line.empty()) break; // 如果输入为空行,结束输入

        stringstream ss(line);
        string sex, password;
        ss >> sex >> password; // 从行中提取性别和身份证号
		
		
		bool biaozhi = false;
		bool biaozhi1 = false;
		int sum = 0;
		if (sex == "boy")
		{
			int a = password[password.size() - 2] - '0';
			if (a % 2 != 1)
			{
				count1 ++ ;
				biaozhi = true;
			}
		}
		else
		{
			int a = password[password.size() - 2] - '0';
			if (a % 2 != 0)
			{
                count1 ++ ;
				biaozhi = true;
			}
		}
		
		for (int i = 0; i < 17; i ++ )
		{
			int y = (password[i] - '0') * arr[i];
			sum += y;
		}
		
		int sss = sum % 11;
		if (brr[sss] != password[password.size() - 1])
		{
			count2 ++ ;
			biaozhi1 = true;
		}
		
		if (biaozhi || biaozhi1)
		{
			s.push_back(password);
		}
		
	}
	
	cout << count1 << ' ' << count2 << endl;
	for (int i = 0; i < s.size(); i ++ )
	{
		cout << s[i] << endl;
	}
	
}

重要知识:

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string s;
    
    getline(cin, s);
    
    stringstream ss(s);
    
    string t;
    
    while (ss >> t)
    {
        cout << t;
    }
}

这么处理可能对某些题目有特攻。

输出性别错误的个数n和校验码错误的个数m

如果要求是这样学着上面弄俩个标志来。

32.两数组最短距离

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	
	long long n, m;
	cin >> n >> m;
	vector<long long>s, t;
	
	long long k;
	
	for (long long i = 0; i < n; i ++ )
	{
		cin >> k;
		s.push_back(k);
	}
	
	for (long long i = 0; i < m; i ++ )
	{
		cin >> k;
		t.push_back(k);
	}
	
	long long i = 0, j = 0;
	long long min = 1000;
	while (i < n && j < m)
	{
		long long m = abs(s[i] - t[j]);
		if (min > m)
		{
			min = m;
		}
		if (s[i] < t[j])
		{
			i ++ ;
		}
		else
		{
			j ++ ;
		}
	}
	cout << min;
}

因为俩个数组是单调的。这么找没问题

while (i < n && j < m)
	{
		long long m = abs(s[i] - t[j]);
		if (min > m)
		{
			min = m;
		}
		if (s[i] < t[j])
		{
			i ++ ;
		}
		else
		{
			j ++ ;
		}
	}

33.移动吧字符串

#include <iostream>
#include <string>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	
	cin.tie(0);
	
	string s, t, x, y;
	
	int a, b, c;
	
	getline(cin, s);
	
	cin >> a >> b >> c;
	
	a -- ;
	c -- ;
	
	t = s.substr(a, b);
	s.erase(a, b);
	
	if (c > a)
	    c -= b;
	
	s.insert(c + 1, t);
	
	
	cout << s;
	
	
}

重要知识:

substr():提取子串

erase():删除子串

insert

34.whq逛酒吧

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int main()
{
    string s;
    int n;
    while (cin >> s >> n)
    {
        unordered_map<string, int> sss;
        for (int i = 0; i <= s.size() - n; i ++ )
        {
            string g = s.substr(i, n);
            sss[g] ++ ;
        }
        int max = 0;
        
        for (auto &ttt : sss)
        {
            if (max < ttt.second)
            {
                max = ttt.second;
            }
        }
        cout << max << endl;
    }
    
    
}

根据题目要求

unordered_map<string, int> sss;
        for (int i = 0; i <= s.size() - n; i ++ )
        {
            string g = s.substr(i, n);
            sss[g] ++ ;
        }

然后用unordered_map来统计。找到重影子像的最大份数。

35.组合数计算

#include <iostream>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int x;
	cin >> x;
	long long n, k;
	
	while (cin >> n >> k)
	{
		if (n - k < k)
		{
			k = n - k;
		}
		
		long long sum = 1;
		
		
		
		for (long long i = 1; i <= k; i ++ )
		{
			sum = (sum * (n - k + i)) / (i);
		}
		cout << sum << endl;
	}
}

36.旋转正方形

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int k = (n - 1) * 4;
	
	int arr[k];
	
	int num;
	int count = 0;
	int i;
	for (i = 0; i < n; i ++ )
	{
		cin >> num;
		arr[i] = num;
	} 
	count = i;
	
	int count1 = 1;
	for (int j = 0; j < n - 2; j ++ )
	{
		cin >> num;
		arr[k - count1] = num;
		cin >> num;
		arr[count] = num;
		
		count ++ ;
		count1 ++ ; 
	}
	
	
	for (int j = 0; j < n; j ++ )
	{
		cin >>  num;
		
		arr[k - count1 - j] = num;
	}
	
	string s;
	
	for (int j = 0; j < k; j ++ )
	{
		s = s + char('0' + arr[j]);
	}
	string sss;
	sss = s + s;
	
	int r;
	cin >> r;
	
	r = r % 4;
	
	int pos = k - r * (n - 1);
	string ssss = sss.substr(pos, k);
	
	int j;
	for (j = 0; j < n; j ++ )
	{
		if (j > 0) cout << ' ';
		cout << ssss[j];
	}
	cout << endl;
	int count333 = 1;
	for (int jj = 0; jj < n - 2; jj ++ )
	{
		cout << ssss[s.size() - count333];
		
		for (int jjj = 0; jjj < 3 + (n - 3) * 2; jjj ++ )
		{
			
			cout << ' ';
		}
		
		cout << ssss[j] << endl;
		j ++ ;
		count333 ++ ;
	}
	
	for (int jjjj = 0; jjjj < n; jjjj ++ )
	{
		if (jjjj > 0) cout << ' ';
		cout << ssss[s.size() - count333];
		count333 ++ ;
	}
	
}

没必要创建二维数组,用一维数组就能解决问题,可看成起点是左上角的一个顺时针
方向存入一维数组。

要循环处理可以这样。比如s = "123456789",拼成s = s + s; s = "123456789123456789";

再用substr截取出来。

数字转字符串

string s;
	
	for (int j = 0; j < k; j ++ )
	{
		s = s + char('0' + arr[j]);
	}

37.生日快乐

#include <iostream>
#include <string>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int arr[366] = {0};
	int brr[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	int crr[12] = {0};
	
	for (int i = 1; i < 12; i ++ )
	{
		crr[i] = crr[i - 1] + brr[i - 1];
	}
	string str;
	int n;
	cin >> n;
	int mm, dd;
	for (int i = 0; i < n; i ++ )
	{
		cin >> str >> str;
		
		mm = (str[4] - '0') * 10 + (str[5] - '0');
		dd = (str[6] - '0') * 10 + (str[7] - '0');
		arr[crr[mm - 1] + dd - 1] ++ ;
		
	}
	
	int p = 0, max = 0;
	for (int i = 0; i < 12; i ++ )
	{
		int _count = 0;
		for (int j = 0; j < brr[i]; j ++ )
		{
			if (arr[p] > 1)
			{
				_count ++ ;
				if (arr[p] > max)
				{
					max = arr[p];
					mm = i + 1;
					dd = j + 1;
				}
			}
			p ++ ;
		}
		
		
		if (i == 0)
		{
			cout << _count;
		}
		else
            cout << ',' << _count;
	}
	
	cout << endl;
	if (mm < 10)
	{
		cout << 0 << mm << "-";
	}
	else
	{
		cout << mm << "-";
	}
	
	if (dd < 10)
	{
		cout << 0 << dd;
	}
	else
	{
		cout  << dd;
	}
} 

打点法;算上闰年一年366天。

38.新的应聘方式

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

int main()
{
    long long n;
    
    
    
    vector <long long> s;
    
    s.push_back(1);
    s.push_back(1);
    
    for (long long i = 2; i < 50; i ++ )
    {
        s.push_back(s[i - 1] + s[i - 2]);
    }
    
    
    while (cin >> n)
    {
        if (find(s.begin(), s.end(), n) != s.end())
    {
        cout << "admit the second one" << endl;
    }
    else
    {
        cout << "admit the first one" << endl;
    }
    }
    
}

find 用法在vector上。

39.间断变连续

#include <iostream>
#include <vector>
#include <set>

using namespace std;

int main()
{
    int v;
    
    cin >> v;
    
    set <int> s;
    vector <pair<int, int>> t;
    while (cin >> v)
    {
        s.insert(v);
        t.push_back({v, 0});
    }
    int count = 0;
    for (auto &sum : s)
    {
        for (auto &h : t)
        {
            if (h.first == sum)
            {
                h.second = count;
            }
        }
        count ++ ;
    }
    
    for (auto &h : t)
        {
            cout << h.second << endl;
        }
}

40.三角消消看

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

int main() {
    int n;
    cin >> n; // 读取测试用例的数量

    int equalCount = 0; // 记录相等的三角形数量
    int unequalCount = 0; // 记录不相等的三角形数量

    while (n--) {
        int x1, y1, x2, y2, x3, y3;
        int x4, y4, x5, y5, x6, y6;

        // 读取第一个三角形的坐标
        cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
        // 读取第二个三角形的坐标
        cin >> x4 >> y4 >> x5 >> y5 >> x6 >> y6;

        // 计算第一个三角形的三边长度平方
        int sum1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        int sum2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
        int sum3 = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1);

        // 计算第二个三角形的三边长度平方
        int sum4 = (x4 - x5) * (x4 - x5) + (y4 - y5) * (y4 - y5);
        int sum5 = (x5 - x6) * (x5 - x6) + (y5 - y6) * (y5 - y6);
        int sum6 = (x6 - x4) * (x6 - x4) + (y6 - y4) * (y6 - y4);

        // 将边长平方存入向量
        vector<int> s = {sum1, sum2, sum3};
        vector<int> t = {sum4, sum5, sum6};

        // 对边长进行排序
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());

        // 比较两个三角形的边长是否相等
        if (s == t) {
            equalCount++; // 相等三角形数量加 1
        } else {
            unequalCount++; // 不相等三角形数量加 1
        }
    }

    // 输出结果
    if (equalCount > 0) {
        for (int i = 0; i < equalCount; i++) {
            cout << "\\"; // 输出相等的标记
        }
        cout << endl;
    }
    else
    {
        cout << "NULL" << endl;
    }
    
    if (unequalCount > 0) {
        for (int i = 0; i < unequalCount; i++) {
            cout << "%"; // 输出不相等的标记
        }
        cout << endl;
    }
    else
    {   
        cout << "NULL";
    }

    return 0;
}

用排序看看是否全等三角形

单个   '\'输出

41.最活跃的M个数

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

bool cmp(const pair<int, int> &s, const pair<int, int> &t)
{
    if (s.second != t.second)
    {
        return s.second > t.second;
    }
    else
    {
        return s.first < t.first;
    }
}


int main() {
    int m;
    scanf("m=%d", &m); // 输入需要输出的前 m 个最活跃数


    ios::sync_with_stdio(false);
    cin.tie(0);
    // 假设输入的数字范围是 [0, 5000]
    const int MAX_VALUE = 5001; // 数字范围最大是 5000
    vector<int> count(MAX_VALUE, 0); // 用来存储每个数字的出现次数

    int n;
    // 使用 cin 读取所有的数字,直到文件结束(EOF)
    while (cin >> n) 
    {
        count[n]++; // 每读到一个数字,增加它的计数
    }

    // 将数字和出现次数存储到一个 pair 数组中
    vector<pair<int, int>> freq;
    for (int i = 0; i < MAX_VALUE; i++) 
    {
        if (count[i] > 0) { // 只考虑出现过的数字
            freq.push_back({i, count[i]});
        }
    }

    // 按照出现次数降序,次数相同则按数字升序排序
    
    int size = min(m, int(freq.size()));
    
    partial_sort(freq.begin(),freq.begin() + size, freq.end(), cmp);
    // 输出前 m 个最活跃的数及其出现次数
    for (int i = 0; i < m && i < freq.size(); i++) 
    {
        cout << freq[i].first << "," << freq[i].second << endl;
    }

    return 0;
}

42.到底几个零

#include <iostream>

using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n;
	int sum = 0;
	while (cin >> n)
	{
		while (n != 0)
		{
			sum = n / 5 + sum;
			n = n / 5;
		}
		cout << sum << endl;
		sum = 0;
	}
}

43.哥德巴赫猜想

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n;
	
	vector <bool> s(10000000, true);
	vector <int> t;
	s[0] = false;
	s[1] = false;
	for (int i = 2; i * i <= 10000000; i ++ )
	{
		if (s[i])
		{
			for (int j = i * i; j <= 10000000; j += i)
			{
				s[j] = false;
			}
		}
	}
	
	for (int i = 0; i <= 10000000; i ++ )
	{
		if (s[i])
			t.push_back(i);
	}
	
	
	
	
	while (cin >> n && n > 6)
	{
		int count = 0;
		while (n > t[count])
		{
			count ++ ;
		}
		count -- ;
		
		int count1 = 0;
		for (int i = 0, j = count; i < j;)
		{
		    
		    if (t[i] + t[j] == n)
		    {
		        if (count1 > 0) cout << ' ';
		        cout << n << '=' << t[i] << '+' << t[j];
		        count1 = (count1 + 1) % 5;
		        i ++ ;
		        j -- ;
		        if (count1 == 0) cout << endl;
		    }
		    else if (t[i] + t[j] > n)
		    {
		        j -- ;
		    }
		    else
		    {
		        i ++ ;
		    }
		    
		    
		}
		if (count1 != 0)
		    cout << endl;
	}
}

44.字符串的和

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	
	getline(cin, s);
	
	int sum = 0;
	
	for (int i = 0; i < s.size(); i ++ )
	{
		if (isalpha(s[i]))
			sum += (i + 1) * (s[i] - 'A' + 1);
		else
			sum += (i + 1) * 0;
	}
	
	cout << sum;
}

45.数字矩阵

#include <iostream>

using namespace std;

int main()
{
    int n;
    
    cin >> n;
    
    int arr[n][n];
    int sum = 1;
    
    if (n % 2 == 1)
    {
	    	for (int i = 0; i < n; i ++ )
	    {
	    	if (i % 2 == 0)
	    	{
	    		for (int j = 0; j <= i; j ++ )
	    		{
	    			arr[j][i - j] = sum ++ ;
				}
			}
			else
			{
				for (int j = i; j >= 0; j -- )
				{
					arr[j][i - j] = sum ++ ;
				}
			}
		}
	}
	else
	{
			for (int i = 0; i < n; i ++ )
	    {
	    	if (i % 2 == 1)
	    	{
	    		for (int j = 0; j <= i; j ++ )
	    		{
	    			arr[j][i - j] = sum ++ ;
				}
			}
			else
			{
				for (int j = i; j >= 0; j -- )
				{
					arr[j][i - j] = sum ++ ;
				}
			}
		}
	}
    
	
	
	for (int i = 0; i < n; i ++ )
	{
		for (int j = 0; j < n - i; j ++ )
		{
			printf("%5d", arr[i][j]);
		}
		cout << endl;
	}
}

46.字符串种类统计

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(const pair<string, int> &a, const pair<string, int> &b)
{
    return a.second < b.second;
}


int main()
{
	int n, m;
	
	cin >> n >> m;
	
	map<string, int> s;
	
	string t;
	int nn = n;
	while (nn -- )
	{
		cin >> t;
		s[t] ++ ;
	}
	
	vector<pair<string, int>> p(s.begin(), s.end());
	
	sort(p.begin(), p.end(), cmp);
	
	for (int i = 1; i < n + 1; i ++ )
	{
	    int count = 0;
	    for (auto &ttt : p)
	    {
	        if (ttt.second == i)
	        {
	            count ++ ;
	        }
	    }
	    cout << count << endl;
	}
	
}

47.字符串是否合法

#include <iostream>
#include <string>

using namespace std;

bool islower(char c)
{
	return c >= 'a' && c <= 'z';
}

bool isupper(char c)
{
	return c >= 'A' && c <= 'Z';
}

char getnextlower(char c)
{
	if (c == 'z')
		return 'b';
	if (c == 'y')
		return 'a';
	
	return c + 2;
}

char getprevupper(char c)
{
	if (c == 'A')
		return 'Y';
	if (c == 'B')
		return 'Z';
	return c - 2; 
}

bool isvaild(const string &s)
{
	for (int i = 0; i < (int)s.size() - 1; i ++ )
	{
		char current = s[i];
		char next = s[i + 1];
		if (islower(current))
		{
			char n1 = toupper(current);
			char n2 = getnextlower(current);
			
			if (n1 != next && n2 != next)
			{
				return false;
			}
		}
		else if (isupper(current))
		{
			char n1 = tolower(current);
			char n2 = getprevupper(current);
			
			if (n1 != next && n2 != next)
			{
				return false;
			}
		}
		else
		{
			return false;
		}
		
	}
	
	return true;
}


int main()
{
	int n;
	
	cin >> n;
	
	while (n -- )
	{
		string s;
		cin >> s;
		if (isvaild(s))
		{
			cout << "Y" << endl;
		}
		else
		{
			cout << "N" << endl;
		}
		
	}
}

48.有多少个点在直线上

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	
	int n;
	
	cin >> n;
	
	int count = 0;
	while (n -- )
	{
		double a, b;
		
		cin >> a >> b;
		
		if (x1 - x2 != 0 && y2 - y1 != 0)
		{
			double k = (y1 - y2) / (x1 - x2);
			double yy = k * (a - x1) + y1;
			if (abs(yy - b)<= 1e-9)
			{
				count ++ ;
			}
		}
		else if (y2 == y1 && x1 - x2 != 0)
		{
			if (b == y2)
				count ++ ;
		}
		else if (x1 == x2)
		{
			if (a ==  x1)
				count ++ ;	
		}
		
	
	}
	
	cout << count;	
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值