2019年北京青少年信息學(xué)科普日活動朝陽區(qū)選拔賽小學(xué)組參考答案

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為質(zhì)因數(shù)
                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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一章數(shù)和數(shù)的運算 一概念 (一)整數(shù) 1整數(shù)的意義 自然數(shù)和0都是整數(shù)。 2自然數(shù) 我們在數(shù)物體的時候,用來表示...
    meychang閱讀 2,840評論 0 5
  • 基于學(xué)生學(xué)習(xí)共同體培育語文生態(tài)課堂文化的研究 近年來,隨著現(xiàn)代教育理念的不斷深入與...
    火車頭123閱讀 2,277評論 0 8
  • 選擇題部分 1.(),只有在發(fā)生短路事故時或者在負(fù)荷電流較大時,變流器中才會有足夠的二次電流作為繼電保護跳閘之用。...
    skystarwuwei閱讀 14,361評論 0 7
  • 篇一 : 圓柱的側(cè)面積和表面積計算教學(xué)設(shè)計 【教學(xué)目的】:1、使學(xué)生理解和掌握求圓柱的側(cè)面積和表面積的計算方法。 ...
    桃李不言笑春風(fēng)閱讀 593評論 0 0
  • 周一,早班 一個總是讓人很緊張的日子,提前的會議通知和會議記錄報告 心有點累,早早下班回了 睡覺 散步 小嬌 學(xué)習(xí) 總結(jié)
    從心印心閱讀 132評論 0 0

友情鏈接更多精彩內(nèi)容