// test_max.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include <iostream> using namespace std; #define N 8 int main() { int** A=(int **)malloc(N*sizeof(int*)); for(int i=0;i<N;i++) { A[i]=(int*)malloc(N*sizeof(int)); } int s=0; int temp=0; for(int i=0;i<N;i++) for(int j=0;j<N;j++) { if(i+j<=N-1) { s=i+j; temp=s*(s+1)/2; if(s%2==0) { A[i][j]=temp+j; } else { A[i][j]=temp-j+s; } } else { s=N-1-i+N-1-j; temp=s*(s+1)/2; if(s%2==0) { A[i][j]=N*N-1-temp-(N-1-j); } else { A[i][j]=N*N-1-temp-(N-1-i); } } } for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { cout<<A[i][j]<<" "; } cout<<endl; } system("pause"); return 0; }
C++生成zigzag矩阵
时间: 2024-12-15 07:12:14