解题思路:把要处理的数字全部都插入到字符串当中, 然后找规律, 进行输出。
注意事项:详情都写在代码里
参考代码:
#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 人评分
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:594 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:526 |
哥德巴赫曾猜测 (C语言代码)浏览:2316 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:468 |
The 3n + 1 problem (C语言代码)浏览:548 |
1157题解浏览:711 |
K-进制数 (C语言描述,蓝桥杯)浏览:924 |
简单的a+b (C语言代码)浏览:414 |
矩形面积交 (C++代码)浏览:1126 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:629 |