| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3399 人关注过本帖
标题:士兵队列训练问题
只看楼主 加入收藏
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
结帖率:100%
收藏
 问题点数:0 回复次数:4 
士兵队列训练问题
某部队进行新兵队列训练,将新兵从 11 开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数…以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。

输入格式

一个整数 nn 表示新兵人数

输出格式

三个数字,表示剩下的新兵编号

数据范围

3 leq n leq 50003≤n≤5000

例子
Input
40
Output
1 19 37
例子
Input
20
Output
1 7 19
搜索更多相关主题的帖子: 报数 格式 表示 队列 
2020-01-28 11:59
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
收藏
得分:0 
1审阅题目后决定采用固定数组来处理,动态数组在之后自己去实现
2看2个示例先写一个100的数组,同时进行初始化,并赋值为输入的数量后输出,观察程序是否按意图正常运行:
int aa[100]{0},n=0,j=0;
cout << "输入数量" << endl;cin>>n;
while(j<n)aa[j]=1+j++;
j=0;while(aa[j])cout<<aa[j++]<<ends;cout<<endl;
3依据题目条件先写一个报数2剔除的,把凡是报2的数组赋值为0即可
int bs=0;j=0;
while(j<n){if(aa[j])++bs;if(bs==2){aa[j]=0;bs=0;}++j;}
4剔除报2的数组后输出看看
j=0;while(j<n)if(aa[j])cout<<aa[j++]<<ends;else ++j;cout<<endl;
5接着按上面的写报3的剔除
看看报2剔除的输出
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2020-2-6 00:15编辑过]

2020-02-06 00:13
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
收藏
得分:0 
1审阅题目后决定采用固定数组来处理,动态数组在之后自己去实现
2看2个示例先写一个100的数组并赋值为输入的数量,同时进行初始化后输出,观察程序是否按意图正常运行:
int aa[100]{0},n=0,j=0;
cout << "输入数量" << endl;cin>>n;
while(j<n)aa[j]=1+j++;
j=0;while(aa[j])cout<<aa[j++]<<ends;cout<<endl;
3依据题目条件先写一个报数2剔除的,把凡是报2的数组赋值为0即可
int bs=0;j=0;
while(j<n){if(aa[j])++bs;if(bs==2){aa[j]=0;bs=0;}++j;}
4剔除报2的数组后输出看看
j=0;while(j<n)if(aa[j])cout<<aa[j++]<<ends;else ++j;cout<<endl;
5接着按上面的写报3的剔除
看看报2剔除的输出
图片附件: 游客没有浏览图片的权限,请 登录注册
2020-02-06 00:13
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
收藏
得分:0 
5接着按上面的写报3的剔除,复制代码修改即可。
    bs = j = 0;
    while (j<n){ if (aa[j])++bs; if (bs == 3){ aa[j] = 0; bs = 0; }++j; }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
输出查看是否正常运行结果

6写一个结束变量用于按题目结束剔除。
int js=0;
while(1)//把上面2个剔除循环复制过来,写一个结束判断。
{
    bs = j =js= 0;
    while (j<n){ if (aa[j])++bs,++js; if (bs == 2){ aa[j] = 0; bs = 0; --js;}++j; }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
    if (js <= 3)break;
    bs = j =js= 0;
    while (j<n){ if (aa[j])++bs,++js; if (bs == 3){ aa[j] = 0; bs = 0; --js;}++j; }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
    if (js <= 3)break;
}
观察输出是否正确运行
图片附件: 游客没有浏览图片的权限,请 登录注册

7最后整理一下代码就可以结束本题的编程了。
void 士兵队列训练()
{//缘由https://bbs.bccn.net/redirect.php?tid=499026&goto=lastpost#lastpost
    int aa[100]{0}, n = 0, j = 0, bs = 0, js = n;
    cout << "输入数量" << endl; cin >> n;
    while (j < n)aa[j] = 1 + j++;
    j = 0; while (aa[j])cout << aa[j++] << ends; cout << endl;
    while (1)
    {
        bs = j = js = 0;
        while (j < n){ if (aa[j])++bs, ++js; if (bs == 2){ aa[j] = 0; bs = 0; --js; }++j; }
        if (js <= 3)break;
        bs = j = js = 0;
        while (j<n){ if (aa[j])++bs, ++js; if (bs == 3){ aa[j] = 0; bs = 0; --js; }++j; }
        if (js <= 3)break;
    }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
}
8优化代码
void 士兵队列训练()
{//缘由https://bbs.bccn.net/redirect.php?tid=499026&goto=lastpost#lastpost
    int aa[100]{0}, n = 0, j = 0, bs = 0, js = n; bool k = true;
    cout << "输入数量" << endl; cin >> n;
    while (j < n)aa[j] = 1 + j++;
    j = 0; while (aa[j])cout << aa[j++] << ends; cout << endl;
    while (1)
    {
        bs = j = js = 0;
        while (j<n){ if (aa[j])++bs, ++js; if (bs == (k ? 2 : 3)){ aa[j] = 0; bs = 0; --js; }++j; }
        if (js <= 3)break;
        if (k) k = false; else k = true;
    }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
}
2020-02-06 04:04
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
收藏
得分:0 
5接着按上面的写报3的剔除,复制代码修改即可。
    bs = j = 0;
    while (j<n){ if (aa[j])++bs; if (bs == 3){ aa[j] = 0; bs = 0; }++j; }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
输出查看是否正常运行结果

6写一个结束变量用于按题目结束剔除。
int js=0;
while(1)//把上面2个剔除循环复制过来,写一个结束判断。
{
    bs = j =js= 0;
    while (j<n){ if (aa[j])++bs,++js; if (bs == 2){ aa[j] = 0; bs = 0; --js;}++j; }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
    if (js <= 3)break;
    bs = j =js= 0;
    while (j<n){ if (aa[j])++bs,++js; if (bs == 3){ aa[j] = 0; bs = 0; --js;}++j; }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
    if (js <= 3)break;
}
观察输出是否正确运行
图片附件: 游客没有浏览图片的权限,请 登录注册

7最后整理一下代码就可以结束本题的编程了。
void 士兵队列训练()
{//缘由https://bbs.bccn.net/redirect.php?tid=499026&goto=lastpost#lastpost
    int aa[100]{0}, n = 0, j = 0, bs = 0, js = n;
    cout << "输入数量" << endl; cin >> n;
    while (j < n)aa[j] = 1 + j++;
    j = 0; while (aa[j])cout << aa[j++] << ends; cout << endl;
    while (1)
    {
        bs = j = js = 0;
        while (j < n){ if (aa[j])++bs, ++js; if (bs == 2){ aa[j] = 0; bs = 0; --js; }++j; }
        if (js <= 3)break;
        bs = j = js = 0;
        while (j<n){ if (aa[j])++bs, ++js; if (bs == 3){ aa[j] = 0; bs = 0; --js; }++j; }
        if (js <= 3)break;
    }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
}
8优化代码
void 士兵队列训练()
{//缘由https://bbs.bccn.net/redirect.php?tid=499026&goto=lastpost#lastpost
    int aa[100]{0}, n = 0, j = 0, bs = 0, js = n; bool k = true;
    cout << "输入数量" << endl; cin >> n;
    while (j < n)aa[j] = 1 + j++;
    j = 0; while (aa[j])cout << aa[j++] << ends; cout << endl;
    while (1)
    {
        bs = j = js = 0;
        while (j<n){ if (aa[j])++bs, ++js; if (bs == (k ? 2 : 3)){ aa[j] = 0; bs = 0; --js; }++j; }
        if (js <= 3)break;
        if (k) k = false; else k = true;
    }
    j = 0; while (j < n)if (aa[j])cout << aa[j++] << ends; else ++j; cout << endl;
}
2020-02-06 04:04
快速回复:士兵队列训练问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017152 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved