zoukankan      html  css  js  c++  java
  • (模拟) codeVs1160 蛇形矩阵

    题目描述 Description

    小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1开始以逆时针方向绕行,逐圈扩大,直到n行n列填满数字,请输出该n行n列正方形矩阵以及其的对角线数字之和.

    输入描述 Input Description

    n(即n行n列)

    输出描述 Output Description

    n+1行,n行为组成的矩阵,最后一行为对角线数字之和

    样例输入 Sample Input

    3

    样例输出 Sample Output

    5 4 3
    6 1 2
    7 8 9
    25

     
    模拟题
    注意每次到了增加一圈时,用来判断是否到边界的数要增加2
    C++代码:
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int a[102][102];
    int main(){
        int n;
        cin>>n;
        int i = n/2;
        int j = n/2;
        int m = n*n;
        int cnt = 1;
        a[i][j] = cnt++;
        int f = 0;
        int ff;
        while(cnt<=m){
            f+=2;
            j++;
            ff = f;
            while(ff--){
                a[i][j] = cnt++;
                if(ff)i--;
            }
            ff = f;
            j--;
            while(ff--){
                a[i][j] = cnt++;
                if(ff)j--;
            }
            ff = f;
            i++;
            while(ff--){
                a[i][j] = cnt++;
                if(ff)i++;
            } 
            ff = f;
            j++;
            while(ff--){
                a[i][j] = cnt++;
                if(ff)j++;
            }
        }
        int sum = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j< n; j++){
                cout<<a[i][j]<<" ";
                if(i == j || i + j == n-1){
                    sum+=a[i][j];
                }
            }
            cout<<endl;
        }
        cout<<sum<<endl;
        return 0;
    }
  • 相关阅读:
    .sln是什么的格式
    VMware的四种网络连接方式
    Cisco 2950交换机 配置手册
    Framework2.0标识没有写访问权限的解决办法
    ASP.NET IIS 注册工具
    .suo介绍
    Uri In WPF
    BindingErrorListener In WPF
    WPF 详解模板
    .NET装饰器(Decorator)模式
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/10630263.html
Copyright © 2011-2022 走看看