• 算法设计与分析课后习题3.5


        题目:编程打印如以下规律的n*n方阵

    1 1 1 1 1 1

    1 2 2 2 2 1

    1 2 3 3 2 1

    1 2 3 3 2 1

    1 2 2 2 2 1

    1 1 1 1 1 1

        思路:由外向内数,位于第一圈的元素值都是1,位于第N圈的值都是N

    //算法设计与分析180页第5题
    #include<iostream>
    #include<iomanip>
    using namespace std;

    int main()
    {
        int n;
        cout<<"Please enter your n:"<<endl;
        cin>>n;
        int a[n][n];
        for(int i=0;i!=n;++i)
        {
            for(int j=0;j!=n;++j)
            {
                for(int k=n/2;k!=-1;--k)
                    if(i==k||i==n-1-k||j==k||j==n-1-k)
                        a[i][j]=k+1;
                    cout<<setw(2)<<a[i][j];
            }
            cout<<endl;
        }
    }

  • 相关阅读:
    leetcode167 Two Sum II
    leetcode18 4Sum
    leetcode15 three sum
    leetcode-1-Two Sum
    SQL优化——select
    Hadoop 集群搭建
    虚拟机中的两台主机怎么相互拷贝文件
    doker5
    docker4
    docker3
  • 原文地址:https://www.cnblogs.com/mazhuang/p/1645800.html
Copyright © 2020-2023  润新知