Welcome![Sign In][Sign Up]
Location:
Search - DO-17

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:

[GDI-Bitmap17.如何实现图像的雾化效果?

Description: 17.如何实现图像的雾化效果?这个PHOTOSHOP效果的图怎么用VC做出来呢?-17. How to achieve image atomizing effect? The effects of Photoshop with VC map how to do it?
Platform: | Size: 37925 | Author: 黄庆龙 | Hits:

[Otherxygbook

Description: 主要功能如下:1、不用注册也可以发言,注册的话保留用户名;2、可以设置多个管理员;3、两种留言显示方式:留言本式和讨论区式,并可进行固定设置;4、管理员可以删除、固顶、锁定、提前和反向操作留言;5、留言可选心情图标;6、发帖人IP记录,管理员可查看;7、注册用户可以修改自己的留言;8、留言可按留言主题、留言内容、回复内容、留言人进行搜索;9、可设置是否必须注册才能留言;10、可设置是否只能管理员或版主才能回复;11、可设置过滤词语;12、可设置被过滤词语后是否禁止该用户再次留言; 13、留言锁定功能,使锁定留言不能回复;14、可设定是否要经过认证才能显示留言;15、可同时对多个留言进行管理操作;16、用户可发悄悄话,只有该用户和管理员或版主才能查看;17、防灌水功能,可设置用户留言时间间隔;18、可禁止一些IP用户的留言;19、两级管理员,版主只有对留言管理的权力,管理员有所有权力;20、带计数器功能,可在留言板后台设置;21、多用户回复留言功能,类似微型论坛;22、图文编辑混排功能,强大的文本编辑器。此为个人版。-main function is as follows : 1, do not have to register can also speak to retain the registered users; 2. administrators can set up a number; three, two displays messages : messages of this type and a discussion area - and may fixed set; 4, administrators can delete jacking, lock, early and contrarian voice; 5, voice mail optional mood icon; 6. people posting record IP, administrators can be found; 7, registered users can modify their own voice; 8. voice mail messages may be the theme, message, reply, message people search; 9. whether or not it can be registered before voice mail; 10, can be set only if administrators or moderator return; 11. Words can provide filtration; 12, the filter can be set to be banned words whether the user messages again; 13, voice mail lockout, so lock message
Platform: | Size: 686463 | Author: qiu | Hits:

[GDI-Bitmap17.如何实现图像的雾化效果?

Description: 17.如何实现图像的雾化效果?这个PHOTOSHOP效果的图怎么用VC做出来呢?-17. How to achieve image atomizing effect? The effects of Photoshop with VC map how to do it?
Platform: | Size: 207872 | Author: | Hits:

[SCMjoy-2007-10-17

Description: 利用ATtiny45晶片自行開發製做遊戲控制器-ATtiny45 chips using self-developed system to do the game controller
Platform: | Size: 71680 | Author: 吳小禾 | Hits:

[Software EngineeringDO_178B_Standard

Description: DO-178B document The rapid license in the use of software in airbore system and equipment used on aircraft and engines
Platform: | Size: 3574784 | Author: LX | Hits:

[Software EngineeringDO_248B_Standard

Description: FINAL REPORT FOR CLARIFICATION OF DO-178B “SOFTWARE CONSIDERATIONS IN AIRBORNE SYSTEMS AND EQUIPMENT CERTIFICATION”-FINAL REPORT FOR CLARIFICATION OF DO-178B
Platform: | Size: 340992 | Author: LX | Hits:

[Proxy ServerIPServer_SDK_3.1.6.17

Description: IPServer_SDK_3.1.6.17对于普通宽带用户(如adsl拨号上网),设备每次启动后对应的IP地址是动态分配的,通过将设备DNS地址设置为IPServer的所在PC的IP地址,那么远端的客户可以通过查询IPServer上的设备注册信息获得设备对应的IP地址。如果设备不是直接架在公网上,那么需要在私网的网关上对设备的端口做映射,如设备与网络SDK的数据通讯端口(一般为8000,如果同一个私网内有多台设备,则要求设备的数据通讯端口不能重复,以防端口映射的时候出问题)。-IPServer_SDK_3.1.6.17 for ordinary broadband users (such as dial-up adsl), equipment, after each time you start the corresponding IP address is dynamically assigned by the equipment is set to DNS address IPServer where PC s IP address, so remote clients can through inquiries IPServer of the equipment on the registration information to obtain equipment corresponding IP address. If the device is not directly mounted on the public Internet, then the need for the private network gateway equipment to do the port mapping, such as equipment and network SDK data communication port (usually 8000, if the same multiple Private network equipment request data communications equipment port can not be repeated in order to prevent problems when port mapping).
Platform: | Size: 699392 | Author: jcl | Hits:

[SCM08.7.17

Description: 用AVR单片机做的MzL02的驱动程序,可在0XOE和0XEE之间添加自己的代码就可,用的软件是IAR4.1-Using AVR to do the MzL02 single-chip driver can be 0XOE and between 0XEE add your own code you can use the software is IAR4.1
Platform: | Size: 12288 | Author: 肖若进 | Hits:

[Internet-Networksz2009-3-17

Description: 带时钟的台历:一个非常非常漂亮的仿真电子台历,不用可惜-Clock with calendar: a very, very nice simulation of electronic desk calendar, do not pity
Platform: | Size: 1024 | Author: | Hits:

[CSharpvncviewer-src-1.0.1.17

Description: The project is a VNC viewer written in C#. Do try this out
Platform: | Size: 86016 | Author: sarah | Hits:

[Delphi VCLDBISAM-4.17

Description: DBISAM v4.17 System will automatically delete the directory of debug and release, so please do not put files on these two directory.
Platform: | Size: 484352 | Author: hansolo101 | Hits:

[AI-NN-PRfet-3.17.11

Description: 遗传算法做排课软件源码!写的非常不错,利用遗传算法实现了快速排课!-Timetabling software source code genetic algorithm to do! Very well written!
Platform: | Size: 1284096 | Author: 冯云 | Hits:

[FlashMX17

Description: 用FLASH AS3制作的简单计算器,可以计算四种简单的算术。-caculator made by Flash AS3,can do four kinds of caculation.
Platform: | Size: 10240 | Author: qinjiayue | Hits:

[Dialog_Windowsplited-as-17-screens

Description: 实现窗口17分屏,左侧右侧分屏,右侧分为4*4的窗口,对做界面的朋友来说是很给力的参考代码。-Implementation 17 screens, window on the left side of the split screen on the right, the right side of the window is divided into 4* 4, for friends do interface is great reference code.
Platform: | Size: 7370752 | Author: 李映 | Hits:

[Other17-3-ATM

Description: 用c语言编写的ATM联系,虽然界面简单,但是适合初学者做联系使用-ATM using c language contact, although the interface is simple, but do contact use suitable for beginners
Platform: | Size: 3072 | Author: 杨凯 | Hits:

[OtherAtualiza-Sistema-17-02-2006

Description: application to be executed compares version of the client / server and copies the new version. aplicaç ã o ao ser executada compara versã o do cliente/servidor e copia nova versã o.
Platform: | Size: 232448 | Author: Fabricio | Hits:

[JSP/Javav0.17

Description: 一款基于java和mysql的酒店管理系统-DO IT
Platform: | Size: 617472 | Author: liudeyuan | Hits:

[OtherSTW脱机外挂6-17

Description: 石器脱机外挂,用于挂机练级,捉宠,做任务,合成等(Stone Age auxiliary software for normal hang up and do the task)
Platform: | Size: 4493312 | Author: 风雨自在 | Hits:

[WEB Code17

Description: 马克斯CMS(Maxcms)5,想你所想,做你做的事!让视频站更容易使用!马克斯程序(Maxcms)开源,免费的,功能强大的,(Marx CMS (Maxcms) 5, do what you want and do what you do. Make video stations easier to use! Marx program (Maxcms) is open source, free and powerful.)
Platform: | Size: 1932288 | Author: 3wy4e5 | Hits:
« 12 3 4 5 »

CodeBus www.codebus.net