c++编程题经典100例
c/c++编程题给,已经运行确认了
螺旋方阵(逆时针方向)
#include <stdio.h>
void main()
{
int x;
int ii,jj,n = 6;
int v=1;
int i0,i1,j0,j;
printf("Please enter n: n");
scanf("%d",&n);
i0=0; i1 = n -1;
for (j=0;j<=n/2;j++)
{
j0 = j;
for (ii=i0;ii<=i1;ii++) {x[j0][ii]=v; v++;};
for (jj=j0+1;jj<=i1;jj++) {x[jj][i1]=v; v++;};
for (ii=i1-1;ii >=i0;ii--) {x[i1][ii]=v; v++;};
for (jj=i1-1;jj >=i0+1;jj--) {x[jj][i0]=v; v++;};
i0 = i0+1;i1=i1-1;
}
for (ii=0;ii<n;ii++)
{
for (jj=0;jj<n;jj++) printf("%3d ",x[jj][ii]);
printf("n");
}
}
螺旋方阵(顺时针方向)
#include <stdio.h>
#include <stdlib.h>
void main( )
{
int i,j,r,l,b,d,pl,N;
int choose;
do
{
system("cls");
printf("tt************************************************n");
printf("tt********* 1. 阶数选择并打印 *****************n");
printf("tt********* 2. 退出 *****************n");
printf("tt************************************************n");
printf("ttPlease input your choose:");
fflush(stdin);
scanf("%d",&choose);
if(choose==2) break;
else if(choose==1)
{
printf("Input n:");
fflush(stdin);
scanf("%d",&N);
for (i=1;i<=N;i++)
{ for (j=1;j<=N;j++)
{ r=i;d=0;pl=j-r;
if (r>N-j+1) {r=N-j+1;d=1;pl=i-r;}
if (r>N-i+1) {r=N-i+1;d=2;pl=N-j+1-r;}
if (r>j) {r=j;d=3;pl=N-i+1-r;}
l=N-2*(r-1)-1;
b=(4*N-4)*(r-1)-4*(r-1)*(r-2)+1;
printf("%4d",b+l*d+pl);
}
printf("n");
}
system("pause");
}
else
{
printf("Error Input!n");
system("pause");
continue;
}
}while
(1);
}
蛇形方阵
#include <stdio.h>
#define MAX 20
int n,square[MAX][MAX];
int ok(int x,int y)
{
return (0<=x&&x<n&&0<=y&&y<n&&square[x][y]==0);
}
main()
{
int i,j,k,d;
printf("请输入螺旋方阵的阶数n:");
scanf("%d",&n);
memset(square,0,MAX*MAX*sizeof(int));
for(i=j=d=0,k=1;k<=n*n;++k)
{
square[i][j]=k;
switch(d%4)
{
case 0:if(ok(i,j+1)) ++j;
else ++d,++i;
break;
case 1:if(ok(i+1,j)) ++i;
else ++d,--j;
break;
case 2:if(ok(i,j-1)) --j;
else ++d,--i;
break;
case 3:if(ok(i-1,j)) --i;
else ++d,++j;
}
}
for(i=0;i<n;++i)
{
printf("n");
for(j=0;j<n;++j)
printf("%4d",square[i][j]);
printf("n");
}
printf("n");
} (随机推荐阅读本站500篇优秀文章点击前往:500篇优秀随机文章)
螺旋方阵(逆时针方向)
#include <stdio.h>
void main()
{
int x;
int ii,jj,n = 6;
int v=1;
int i0,i1,j0,j;
printf("Please enter n: n");
scanf("%d",&n);
i0=0; i1 = n -1;
for (j=0;j<=n/2;j++)
{
j0 = j;
for (ii=i0;ii<=i1;ii++) {x[j0][ii]=v; v++;};
for (jj=j0+1;jj<=i1;jj++) {x[jj][i1]=v; v++;};
for (ii=i1-1;ii >=i0;ii--) {x[i1][ii]=v; v++;};
for (jj=i1-1;jj >=i0+1;jj--) {x[jj][i0]=v; v++;};
i0 = i0+1;i1=i1-1;
}
for (ii=0;ii<n;ii++)
{
for (jj=0;jj<n;jj++) printf("%3d ",x[jj][ii]);
printf("n");
}
}
螺旋方阵(顺时针方向)
#include <stdio.h>
#include <stdlib.h>
void main( )
{
int i,j,r,l,b,d,pl,N;
int choose;
do
{
system("cls");
printf("tt************************************************n");
printf("tt********* 1. 阶数选择并打印 *****************n");
printf("tt********* 2. 退出 *****************n");
printf("tt************************************************n");
printf("ttPlease input your choose:");
fflush(stdin);
scanf("%d",&choose);
if(choose==2) break;
else if(choose==1)
{
printf("Input n:");
fflush(stdin);
scanf("%d",&N);
for (i=1;i<=N;i++)
{ for (j=1;j<=N;j++)
{ r=i;d=0;pl=j-r;
if (r>N-j+1) {r=N-j+1;d=1;pl=i-r;}
if (r>N-i+1) {r=N-i+1;d=2;pl=N-j+1-r;}
if (r>j) {r=j;d=3;pl=N-i+1-r;}
l=N-2*(r-1)-1;
b=(4*N-4)*(r-1)-4*(r-1)*(r-2)+1;
printf("%4d",b+l*d+pl);
}
printf("n");
}
system("pause");
}
else
{
printf("Error Input!n");
system("pause");
continue;
}
}while
(1);
}
蛇形方阵
#include <stdio.h>
#define MAX 20
int n,square[MAX][MAX];
int ok(int x,int y)
{
return (0<=x&&x<n&&0<=y&&y<n&&square[x][y]==0);
}
main()
{
int i,j,k,d;
printf("请输入螺旋方阵的阶数n:");
scanf("%d",&n);
memset(square,0,MAX*MAX*sizeof(int));
for(i=j=d=0,k=1;k<=n*n;++k)
{
square[i][j]=k;
switch(d%4)
{
case 0:if(ok(i,j+1)) ++j;
else ++d,++i;
break;
case 1:if(ok(i+1,j)) ++i;
else ++d,--j;
break;
case 2:if(ok(i,j-1)) --j;
else ++d,--i;
break;
case 3:if(ok(i-1,j)) --i;
else ++d,++j;
}
}
for(i=0;i<n;++i)
{
printf("n");
for(j=0;j<n;++j)
printf("%4d",square[i][j]);
printf("n");
}
printf("n");
} (随机推荐阅读本站500篇优秀文章点击前往:500篇优秀随机文章)
来源:本文由易搜IT博客原创撰写,欢迎分享本文,转载请保留出处和链接!