⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 魔方程序.cpp

📁 一个有趣的魔方程序。vc++实现。有点意思
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 15
void main()
{
	static int square[MAX_SIZE][MAX_SIZE];
    int i,j,row,column;
	int count;
	int size;
    printf("Enter the size of the square:");
	scanf("%d",&size);
	if(size<1 || size>MAX_SIZE+1)
	{
		fprintf(stderr,"Errot! size is out of range\n");
		exit (1);
	}
	if(!(size%2))
	{
		fprintf(stderr,"Errot! size is even\n");
		exit (1);
	}
	for(i=0;i<size;i++)
		for(j=0;j<size;j++)
		   square[i][j]=0;
	square[0][(size-1)/2]=1;
	i=0;
	j=(size-1)/2;
	for(count=2;count<=size*size;count++)
	{
		row=(i-1<0)?(size-1):(i-1);
		column=(j-1<0)?(size-1):(j-1);
     if(square[row][column])
		 i=(++i)%size;
	 else
		{
		 i=row;
		 j=(j-1<0)?(size-1):--j;
		}
		square[i][j]=count;
	}
	printf("Magic square of size %d: \n\n",size);
	for(i=0;i<size;i++)
	{
		for(j=0;j<size;j++)
			printf("%5d",square[i][j]);
		printf("\n");
	}
	printf("\n\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -