Welcome![Sign In][Sign Up]
Location:
Search - 2d c

Search list

[Other用c编写的N*N的螺旋矩阵源代码

Description:

/*
实现效果:
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
*/
#include <stdio.h>
#define N 5 //阶数,即N*N的螺旋矩阵

void main()
{
    int i, j, num=1, a[N][N];
    for(i=0; i<=N/2; i++)
    {
        for(j=i; j<N-i; j++) a[i][j]=n++;
        for(j=i+1; j<N-i; j++) a[j][N-i-1]=n++;
        for(j=N-i-2; j>i; j--) a[N-i-1][j]=n++;
        for(j=N-i-1; j>i; j--) a[j][i]=n++;
    }
    for(i=0; i<N; i++)
    {
        for(j=0; j<N; j++)
            printf("%2d ",a[i][j]);
        printf("\n");
    }
}
    

 

不知道叫什么,先叫它“回宫图”吧
年初的时候在贴吧瞎逛,看到了一个程序挺有意思,会输出如下的形状:
01 24 23 22 21 20 19
02 25 40 39 38 37 18
03 26 41 48 47 36 17
04 27 42 49 46 35 16
05 28 43 44 45 34 15
06 29 30 31 32 33 14
07 08 09 10 11 12 13
仔细看这个形状,数字是按顺序往里回旋的,觉得很有创意,可是一看源代码头就大了,
每个编程人都知道看别人的代码是很困难的,尤其像这种不知道思路的,所以也就放下
没管了。
昨天上物理课实在是没心思听,就想起这个程序,想了一节课,果然不负有心人,给弄出来了,这个是增强版的,可以输入1-10中的任意个数,然后生成图形。
先看代码,没有注释,所以不好看的懂。
#include<stdio.h>
main()
{
       int n,m,i,j,t,k=1;
       int a[11][11];
       clrscr();
       do{
       printf("please input a number(1-10):");
       scanf("%d",&n);
       }while(n<1||n>10);
       t=n+1;
       for(m=1;m<=t/2;m++)
         {
           for(i=m;i<=t-m;i++)
             {a[i][m]=k;k++;}
           for(j=m+1;j<=t-m;j++)
             {a[i-1][j]=k;k++;}
           for(i=n-m;i>=m;i--)
             {a[i][j-1]=k;k++;}
           for(j=n-m;j>=m+1;j--)
             {a[i+1][j]=k;k++;}
         }
       for(i=1;i<=n;i++)
         {
           for(j=1;j<=n;j++)
             {
               if(a[i][j]<=9) printf("0%d ",a[i][j]);
               else printf("%d ",a[i][j]);       }
           printf("\n");
         }
       getch();
}
就是这样的。


可以更简洁些:

#include<stdio.h>
main()
{
       int n,m,i,j,t,k=1;
       int a[11][11];
       clrscr();
       do{
       printf("please input a number(1-10):");
       scanf("%d",&n);
       }while(n<1||n>10);
       t=n+1;
       for(m=1;m<=t/2;m++)
         {
           for(i=m;i<=t-m;i++)
             a[i][m]=k++;
           for(j=m+1;j<=t-m;j++)
             a[i-1][j]=k++;
           for(i=n-m;i>=m;i--)
             a[i][j-1]=k++;
           for(j=n-m;j>=m+1;j--)
             a[i+1][j]=k++;
         }
       for(i=1;i<=n;i++)
         {
           for(j=1;j<=n;j++)
             {
               if(a[i][j]<=9) printf("0%d ",a[i][j]);
               else printf("%d ",a[i][j]);       }
           printf("\n");
         }
       getch();
}

 


 #include <stdio.h>
#define N 8
main(){
 int i,j,n=1,a[N][N];
 for(i=0;i<=N/2;i++){
  for(j=i;j<N-i;j++)
   a[i][j]=n++;
  for(j=i+1;j<N-i;j++)
   a[j][N-i-1]=n++;
  for(j=N-i-2;j>i;j--)
   a[N-i-1][j]=n++;
  for(j=N-i-1;j>i;j--)
   a[j][i]=n++;
 }
 for(i=0;i<N;i++){
  printf("\n\n");
  for(j=0;j<N;j++)
   printf("%5d",a[i][j]);
 }
}
 

 


                                马踏棋盘问题


#include <stdio.h>
#define N 5
void main(){
 int x,y;
 void horse(int i,int j);
 printf("Please input start position:");
 scanf("%d%d",&x,&y);
 horse(x-1,y-1);
}
void horse(int i,int j){
 int a[N][N]={0},start=0,
  h[]={1,2,2,1,-1,-2,-2,-1},
  v[]={2,1,-1,-2,2,1,-1,-2},
  save[N*N]={0},posnum=0,ti,tj,count=0;
 int jump(int i,int j,int a[N][N]);
 void outplan(int a[N][N]);
 a[i][j]=posnum+1;
 while(posnum>=0){
  ti=i;tj=j;
  for(start=save[posnum];start<8;++start){
   ti+=h[start];tj+=v[start];
   if(jump(ti,tj,a))
    break;
   ti-=h[start];tj-=v[start];
  }
  if(start<8){
   save[posnum]=start;
   a[ti][tj]=++posnum+1;
   i=ti;j=tj;save[posnum]=0;
   if(posnum==N*N-1){
    //outplan(a);
    count++;
   }
  }
  else{
   a[i][j]=0;
   posnum--;
   i-=h[save[posnum>;j-=v[save[posnum>;
   save[posnum]++;
  }
 }
 printf("%5d",count);
}
int jump(int i,int j,int a[N][N]){
 if(i<N&&i>=0&&j<N&&j>=0&&a[i][j]==0)
  return 1;
 return 0;
}
void outplan(int a[N][N]){
 int i,j;
 for(i=0;i<N;i++){
  for(j=0;j<N;j++)
   printf("%3d",a[i][j]);
  printf("\n");
 }
 printf("\n");
 //getchar();
}
用回溯法得到所有的解,但效率较低,只能算出5行5列的

 


Platform: | Size: 4395 | Author: good@588 | Hits:

[Develop Tools3569-2d

Description: C++编程.数据结构与程序设计方法的源代码.找了很久才找到-C programming. Data structures and processes for the design of the source code. Looking for a long time to find
Platform: | Size: 298199 | Author: 12123 | Hits:

[Windows DevelopDijistra最短路径 二维数组结构 c++

Description: Dijistra最短路径 二维数组结构 c-Dijistra Shortest Path 2D array structure c
Platform: | Size: 1594 | Author: 阿宝 | Hits:

[Other2D

Description: 利用简单的C++程序对三角形进行简单的二维变化-simple C program right triangle simple two-dimensional changes
Platform: | Size: 46642 | Author: 许诺 | Hits:

[assembly language2D-TM

Description: c++builder下编的FDTD二维TM波传播情况!
Platform: | Size: 1582443 | Author: 唐涛 | Hits:

[Other resourceAlphaTriangles.c

Description: 主要是查看三角的颜色是否匹配,用的是ARGB,采用的是3D公式,但本程序却不能采用2D公式
Platform: | Size: 9148 | Author: 于芳 | Hits:

[Other resourceStandard-2D-FDTD

Description: 标准的基于C语言的2D FDTD程序 用于电磁波模拟
Platform: | Size: 2560 | Author: 胡兵 | Hits:

[2D Graphic2D图形变换

Description: 通过C++实现简单的2D图形绘制 并且能够让图像以一定的规律变换 比如旋转啊
Platform: | Size: 1212 | Author: 478604094@QQ.com | Hits:

[Graph Recognize2D

Description: C code for "Computational Geometry in C (Second Edition)": Code function Chapter pointer directory ----------------------------------------------------- Triangulate Chapter 1, Code 1.14 /tri Convex Hull(2D) Chapter 3, Code 3.8 /graham Convex Hull(3D) Chapter 4, Code 4.8 /chull sphere.c Chapter 4, Fig. 4.15 /sphere Delaunay Triang Chapter 5, Code 5.2 /dt SegSegInt Chapter 7, Code 7.2 /segseg Point-in-poly Chapter 7, Code 7.13 /inpoly Point-in-hedron Chapter 7, Code 7.15 /inhedron Int Conv Poly Chapter 7, Code 7.17 /convconv Mink Convolve Chapter 8, Code 8.5 /mink Arm Move Chapter 8, Code 8.7 /arm-C code for "Computational Geometry in C (Second Edition)": Code function Chapter pointer directory----------------------------------------------------- Triangulate Chapter 1, Code 1.14 /tri Convex Hull(2D) Chapter 3, Code 3.8 /graham Convex Hull(3D) Chapter 4, Code 4.8 /chull sphere.c Chapter 4, Fig. 4.15 /sphere Delaunay Triang Chapter 5, Code 5.2 /dt SegSegInt Chapter 7, Code 7.2 /segseg Point-in-poly Chapter 7, Code 7.13 /inpoly Point-in-hedron Chapter 7, Code 7.15 /inhedron Int Conv Poly Chapter 7, Code 7.17 /convconv Mink Convolve Chapter 8, Code 8.5 /mink Arm Move Chapter 8, Code 8.7 /arm
Platform: | Size: 67584 | Author: Mark | Hits:

[2D Graphic2D_CAD_System_SRC

Description: 《二维CAD软件的框架结构》,这是一个很好的二维CAD软件的框架结构,学习这个框架可以知道怎样作一个很好的CAD软件的基本方法-"2D CAD software framework," This is a very good 2-D CAD software framework structure, the framework could know how to give a good CAD software for the basic method
Platform: | Size: 73728 | Author: 罗龙文 | Hits:

[Books3569-2d

Description: C++编程.数据结构与程序设计方法的源代码.找了很久才找到-C programming. Data structures and processes for the design of the source code. Looking for a long time to find
Platform: | Size: 297984 | Author: 12123 | Hits:

[Game Engine2_5D_SrollScreen_Map

Description: 自己编的2.5D及2D地图引擎的实现,采用VC++ windows API编程,使用此程序请先安装direct7.0以上版本SDK,否则编译不能通过,对于学习windows API变成,direct编程,游戏开发及地图算法的同学有很大帮助-Own map of the 2.5D and 2D engines realize, using VC++ Windows API programming, you use this procedure to install direct7.0 above SDK, the compiler can not be otherwise, for the windows API into learning, direct programming , game development and map algorithm has a lot to help students
Platform: | Size: 189440 | Author: webber | Hits:

[Other GamesPentarchy

Description: 一个3D RPG游戏,采用Direct9和C#实现。主要特点在于模块清楚,涉及技术主要是人物的控制,以及场景变换。-A 3D RPG game using Direct9 and C# Realize. Module is clearly the main features, mainly dealing with the technical control of the characters and scenes transform.
Platform: | Size: 644096 | Author: 廖洪申 | Hits:

[AlgorithmStandard-2D-FDTD

Description: 标准的基于C语言的2D FDTD程序 用于电磁波模拟-Standards-based C language program for the 2D FDTD simulation of electromagnetic waves
Platform: | Size: 2048 | Author: 胡兵 | Hits:

[Game Enginegame+engine+2D

Description: c#的2d游戏引擎,功能很强大代码很强大 终于传完5个资源了!—_—|-c# the 2d game engine, features a very powerful very powerful code Chuan finally finished the five resources!-_-|
Platform: | Size: 4841472 | Author: lizhaolong | Hits:

[DSP programconv2d

Description: Convolution 2D C code
Platform: | Size: 139264 | Author: mkg081000 | Hits:

[Graph programC++

Description: 飞机大战二维游戏,有碰撞检测文字显示和二维空间漫游(World War II aircraft 2D game, collision detection and text display two-dimensional space roaming)
Platform: | Size: 324608 | Author: 伊尔 | Hits:

[Game Program俄罗斯方块(3D、2D)

Description: 使用c# winform 实现俄罗斯方块,包含2D/3D功能。 声明:这篇文章主要是参考几个别人的博文及源代码学习(the Tetris game develop with C#(Winform),the function include 2D and 3D.)
Platform: | Size: 111616 | Author: yy3b2007com | Hits:

[Game Program粒子系统-喷泉(C#)

Description: 使用c#开发实现2D粒子喷泉效果,功能完整。喜欢的可以下载。(the project is Fountain Particles System,the function is perfect.you can down anywhere.)
Platform: | Size: 63488 | Author: yy3b2007com | Hits:

[AlgorithmFFT-and-FFT-2D-ANSI-C

Description: 标准C语言实现的一维和二维快速傅里叶变换代码,代码运行时不需要额外占用多余的内存空间,简单易用,具体可见代码说明。(FFT and 2 dimension FFT algorithm acommplished with ANSI C.It occupies the least memory space while running after embeded into you project.The source code is easy and brief,you can check the text file for more information.)
Platform: | Size: 3072 | Author: 邓中洋 | Hits:
« 12 3 4 5 6 7 8 9 10 ... 34 »

CodeBus www.codebus.net