题目选自洛谷P1259
递归字符串倒序输出?我们先分析一下样例
ooooooo*******--oooooo--******o* oooooo******--o*ooooo--*****o*o* ooooo*****--o*o*oooo--****o*o*o* oooo****--o*o*o*ooo--***o*o*o*o*ooo*o**--*o*o*o* o--*o**oo*o*o*o* o*o*o*--o*o*o*o* --o*o*o*o*o*o*o*
每两行分一组的时候,可以很明显地看到规律.
- 中间的 "o*" 与 "--" 交换
- 最左边的 "**" 与 "--" 交换
然而后四行我并没有发现什么规律,于是无耻地打了一个表.
打表原理:
ooo*o**--* o*o*o* o--*o**oo* o*o*o* o*o*o*--o* o*o*o* --o*o*o*o* o*o*o* 后6个字符为固定的"o*o*o*" 于是只要记录ooo*o**--*,o--*o**oo*,o*o*o*--o*,--o*o*o*o*
orz
解题代码:
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
char ch[205];//存储棋子的状态
void swap(char &a, char &b)//交换函数
{char t = a;a = b;b = t;
}
void output(){//输出for (int i = 0; i < 2 * n + 2; i++)putchar(ch[i]);putchar('\n');
}
void movechess(int start, int end)
{//移动棋子swap(ch[start], ch[end]);swap(ch[start + 1], ch[end + 1]);output();
}
string out[4] = {"ooo*o**--*", "o--*o**oo*", "o*o*o*--o*", "--o*o*o*o*"};
//打表qwq
int main()
{scanf("%d", &n);for (int i = 0; i < n; i++)ch[i] = 'o';for (int i = n; i < 2 * n; i++)ch[i] = '*';ch[2 * n] = '-';ch[2 * n + 1] = '-';//打印初始状态output();int len = n;//需要移动的黑/白棋子while (true){movechess(len - 1, 2 * len);//中间的 "o*" 与 "--" 交换len--;if (len == 3)//不符合上述规律,开始输出打表内容break;movechess(len, 2 * len);//最左边的 "**" 与 "--" 交换}string ss;for (int i = 0; i < n - 4; i++)ss += "o*";for (int i = 0; i < 4; i++)cout << out[i] << ss << endl;
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态