【C++板块提升计划】第26期 小船修复
本帖最后由 sfqxx 于 2023-2-25 19:05 编辑上一关:https://fishc.com.cn/thread-224646-1-1.html
你们在欢快的聊天,突然,
砰!
你们的小船受到了撞击,现在需要修复
谁知那个不要脸的{:10_256:} 肇事人说:如果你们能答对我给的题目,我就免费给你们修船
一、有以下代码,请观察之后回答,切勿上机!本题同时也考察你们的诚信(40分)
(1)#include <iostream>
using namespace std;
const int SIZE = 20;
int data;
int n, i, h, ans;
void merge()
{
data = data + data;
h--;
ans++;
}
int main()
{
cin>>n;
h = 1;
data = 1;
ans = 0;
for (i = 2; i <= n; i++)
{
h++;
data = 1;
while (h > 1 && data == data)
merge();
}
cout<<ans<<endl;
}
输入:8
输出:_________(10分)
输入:2012
输出:_________(10分)
(2)
#include <iostream>
#include <string>
using namespace std;
int lefts, rights, father;
string s1, s2, s3;
int n, ans;
void calc(int x, int dep)
{
ans = ans + dep*(s1 - 'A' + 1);
if (lefts >= 0) calc(lefts, dep+1);
if (rights >= 0) calc(rights, dep+1);
}
void check(int x)
{
if (lefts >= 0) check(lefts);
s3 = s3 + s1;
if (rights >= 0) check(rights);
}
void dfs(int x, int th)
{
if (th == n)
{
s3 = "";
check(0);
if (s3 == s2)
{
ans = 0;
calc(0, 1);
cout<<ans<<endl;
}
return;
}
if (lefts == -1 && rights == -1)
{
lefts = th;
father = x;
dfs(th, th+1);
father = -1;
lefts = -1;
}
if (rights == -1)
{
rights = th;
father = x;
dfs(th, th+1);
father = -1;
rights = -1;
}
if (father >= 0)
dfs(father, th);
}
int main()
{
cin>>s1;
cin>>s2;
n = s1.size();
memset(lefts, -1, sizeof(lefts));
memset(rights, -1, sizeof(rights));
memset(father, -1, sizeof(father));
dfs(0, 1);
}
输入:
ABCDEF
BCAEDF
输出:__________(20分)
二、完形填空题(60分)
1.(排列数)输入两个正整数 n, m (1≤n≤20,1≤m≤n),在 1~n 中任取 m 个数,按字典序从小到大输出所有这样的排列。例如:
输入:3 2
输出:1 2 1 3 2 1 2 3 3 1 3 2
#include <iostream>
#include <cstring>
using namespace std;
const int SIZE = 25;
bool used;
int data;
int n, m, i, j, k;
bool flag;
int main()
{
cin >> n >> m;
memset( used, false, sizeof(used) );
for ( i = 1; i <= m; i++ )
{
data = i;
used = true;
}
flag = true;
while ( flag )
{
for ( i = 1; i <= m - 1; i++ )
cout << data << " ";
cout << data << endl;
flag = ①;
for ( i = m; i >= 1; i-- )
{
②;
for ( j = data + 1; j <= n; j++ )
if ( !used )
{
used = true;
data = ③;
flag = true;
break;
}
if ( flag )
{
for ( k = i + 1; k <= m; k++ )
for ( j = 1; j <= ④; j++ )
if ( !used )
{
data = j;
used = true;
break;
}
⑤;
}
}
}
}
1.(12分)
2.(12分)
3.(12分)
4.(12分)
5.(12分)
为保证诚信,将设置回帖仅作者可见
版本日志:
v1.0:初始版本
v1.1:@ExiaGN001 指出看不到我的回复,改成撤销置顶(奖励1荣誉)
v1.2:增加题目条件,以及修改了代码(奖励1荣誉,1贡献)
置顶楼层为作者答案,不用管
loading……
页:
[1]