作者在 2020-02-06 04:01:46 发布以下内容
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剔除的输出
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++] = j + 1;
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;
}
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剔除的输出
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++] = j + 1;
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;
}