2019年北京青少年信息学科普日活动朝阳区选拔赛小学组参考答案

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一章数和数的运算 一概念 (一)整数 1整数的意义 自然数和0都是整数。 2自然数 我们在数物体的时候,用来表示...
    meychang阅读 2,665评论 0 5
  • 基于学生学习共同体培育语文生态课堂文化的研究 近年来,随着现代教育理念的不断深入与...
    火车头123阅读 2,060评论 0 8
  • 选择题部分 1.(),只有在发生短路事故时或者在负荷电流较大时,变流器中才会有足够的二次电流作为继电保护跳闸之用。...
    skystarwuwei阅读 13,486评论 0 7
  • 篇一 : 圆柱的侧面积和表面积计算教学设计 【教学目的】:1、使学生理解和掌握求圆柱的侧面积和表面积的计算方法。 ...
    桃李不言笑春风阅读 558评论 0 0
  • 周一,早班 一个总是让人很紧张的日子,提前的会议通知和会议记录报告 心有点累,早早下班回了 睡觉 散步 小娇 学习 总结
    从心印心阅读 124评论 0 0