1 求长方形的周长和面积

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

int main()
{
    freopen("rectangle.in", "r", stdin);
    freopen("rectangle.out", "w", stdout);

    int length, width;
    cin >> length >> width;
    cout << 2 * (length + width) << ' ' << length * width << endl;

    return 0;
}

2 因式分解

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

int n, m, a;

bool check(int n)
{
    for(int i = 2; i <= n; i++)
    {
        while(n)
        {
            if(0 == n % i)
            {
                // i为质因数
                if(i > a)
                {
                    return false;
                }
                n /= i;
            }
            else
            {
                break;
            }
        }
    }

    return true;
}

int main()
{
    freopen("factorization.in", "r", stdin);
    freopen("factorization.out", "w", stdout);

    cin >> n >> m >> a;
    int ans = 0;
    for(int i = n; i <= n + m; i++)
    {
        if(check(i))
        {
            ans++;
        }
    }

    cout << ans << endl;
    return 0;
}

3 字符串

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

multiset<string> mSet;

void SplitString(const string& s, const char& c)
{
    mSet.clear();
    string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;

    while(string::npos != pos2)
    {
        mSet.insert(s.substr(pos1, pos2 - pos1));
        pos1 = pos2 + 1;
        pos2 = s.find(c, pos1); // search c from position pos1
    }

    if(pos1 != s.length())
    {
        mSet.insert(s.substr(pos1)); // from pos1 to the last position
    }
}

int main()
{
    freopen("word.in", "r", stdin);
    freopen("word.out", "w", stdout);

    string s;
    while(getline(cin, s))
    {
        SplitString(s, ' ');
        cout << mSet.size();

        multiset<string>::iterator it;
        for(it = mSet.begin(); it != mSet.end(); it++)
        {
            cout << ' ' << *it;
        }
        cout << endl;
    }

    return 0;
}

完整答案请加微信307591841或QQ307591841

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐