0
点赞
收藏
分享

微信扫一扫

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;
}



举报

相关推荐

0 条评论