zd


私信TA

用户名:uq_55080596819

访问量:1855

签 名:

等  级
排  名 2165
经  验 2309
参赛次数 0
文章发表 7
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:把要处理的数字全部都插入到字符串当中, 然后找规律, 进行输出。

注意事项:详情都写在代码里

参考代码:

#includeusing namespace std;

typedef long long ll;
const int N = 1011;
string res = "", ans = "";//ans串没用到,忽略
int n, num;
int main() {
	cin >> n;
	int bot = 2 * n - 3;三角形底部的字符数,除了最边上的两个
	int top = 2 * n - 1;三角形左右边的字符数
	for (int i = 1; i <= 3 * n; i++) res += to_string(i);//处理数字转为string类型,为什么是3 * n, 因为3*n肯定比三角形总字符数大啦,其实选什么数字都可
	int num = bot + top; //总数
	for (int i = 1; i <= n-1; i++) cout << ".";第一行 先输出点
	cout << 1 <<endl;先把1输出
	int ww = 4 * n - 2, dd = 1;//比如n是5, 那么第二行就是[1] . [15], 三行就是[2] . . [14],发现和为16 ww是什么? 就是推导右侧边的数组索引的数字
	//找规律即可
	for (int i = 2; i <= n-1; i++){
		for (int j = 1; j <= n - i; j++) cout << ".";
		cout << res[i-1] ;//左侧边
		for (int j = 1; j <= dd; j++) cout << ".";
		cout << res[ww - i - 1] <<endl;//右侧边
		dd += 2;//dd是点的数量,每次循环都加2.
	}
	for (int i = n ; i <= ww-n; i++) cout << res[i - 1] ;两侧输出完毕, 输出底部, 取string串中间没输出的就行啦。
	return 0;
}


 

0.0分

3 人评分

  评论区