赋诗一首 2021-08-05 10:42 采纳率: 66.7%
浏览 45
已结题

求解for循环的应用

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-08-05 10:45
    关注

    如有帮助,望点击我回答右上角【采纳】按钮支持一下。
    c++实现效果图:
    img

    #include <iostream>
    using namespace std;
    int main() {
    int n, i, j;
    cout << "Input a number:" << endl;
    cin >> n;
    i = n - 1;
    j = 1;
    while (i >= 0 && j <= 2 * n - 1)
    {
        int w = i;
        int t = j;
        for (i = w; i > 0; i--)
            cout << ' ';
        for (j = 1; j <= 2 * t - 1; j++)
            cout << '*';
        for (i = w; i > 0; i--)
            cout << ' ';
        i = w;
        j = t;
        i--; j++;
        cout << endl;
    }
    i = 1;
    j = n - 1;
    while (i >= 0 && j >= 1)
    {
        int w = i;
        int t = j;
        for (i = w; i > 0; i--)
            cout << ' ';
        for (j = 1; j <= 2 * t - 1; j++)
            cout << '*';
        for (i = w; i > 0; i--)
            cout << ' ';
        i = w;
        j = t;
        i++; j--;
        cout << endl;
        }
    cout << endl;
    return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月13日
  • 已采纳回答 8月5日
  • 创建了问题 8月5日

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义