mr-element 2023-12-17 10:51 采纳率: 60%
浏览 9
已结题

C++代码输出数组出现问题

C++代码的输出问题。
目前我想要用c++代码输出以下图案(那个符号代表空格)

img

我自己写的代码输出

img

我不知道应该如何修改


#include <iostream>
#include <string>

void under(int s) {
    std::string top_bottom_row(2*s, '*');
    std::string bottom_row(s, '*');
    std::cout << top_bottom_row << std::endl;
    int i;
    for ( i = 1; i < s - 1; ++i) {
        std::string middle_row = "*" + std::string(2*s - 2*i, ' ') + "*";
        std::string spaces(i-1, ' ');
        std::cout << spaces << middle_row << std::endl;
    }

    std::string spaces(i, ' ');
    std::cout << spaces << bottom_row << std::endl;
}

int main() {
    under(4); 
    under(6);
    under(10);
    return 0;
}

  • 写回答

4条回答 默认 最新

  • chuifengde 2023-12-17 12:01
    关注
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int pho(int s){
        for(int i = 1;i <= s;i++){
            if(i==1){
                for(int j = 1;j <= s*2;j++) cout <<"*";
            }else if(i<=s/2){
                for(int j = 1;j <=s*2;j++){
                    if(j==1 || j==s*2) cout <<"*" ;
                    else cout << " ";
                }
            }else{
                for(int j = 1;j<=s*2;j++){
                    if(i!=s){
                        if(j==abs(s/2-i)+1|| j==2*s-abs(s/2-i)){
                            cout << "*";
                        }else{
                            cout << " ";
                        }
                    }else{
                        if(j>=abs(s/2-i)+1 && j<=2*s-abs(s/2-i)){
                            cout << "*";
                        }else{
                            cout << " ";
                        }
                    }
                }
    
            }
            cout << endl;
        }
    }
    
    int main(){
        pho(4);
        pho(6);
        pho(10);
        return 0;
    }
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月17日

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题