Welcome![Sign In][Sign Up]
Location:
Search - printf

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:

[Internet-NetworkYCArray

Description: /** * 动态数组的模板类 * 1.支持字符索引 * 2.方便的添加删除修改任意一项 * 最后更新 2004-8-9 yzh **1.优化了字符索引的运作方式,使用数组存储 **2.重写了底层数据的存储,将连续性的存储方式改为了非连续, *** 从而很好有效地支持了“引用”,并且让数据的删除增加变的更为快速 * 用法如: * YCArray<int,int> test * test.Add(\"Number2\",4) * test.Add(\"Number1\",2) * printf(\"%d %d\",test[\"Number1\"],test[\"Number2\"]) * 显示: * 2 4 ******* ******* History: 2004-11-19 修改了析构函数,解决了索引没有释放的bug **/-/ ** * dynamic array template class * 1. Support characters Index * 2. Add convenience to delete arbitrary * a final update 2004-8-9 yzh ** 1. Character Index optimized mode of operation, the use of storage arrays ** 2 . a rewrite of the underlying data storage, storage continuity conversion of non-continuous, and thus good *** effective support to the "quote", and let the data changed to delete the more rapid * usage such as : * YCArraylt; int, intgt; test * test. Add ( "Number2", 4) * test.Add ( "Number1", 2) * printf ( "% d% d", test [ "Number1"] test [ "Number2"]) * Display : * * 2 4 ****** ******* History : 2004-11-19 revised the destructors, the index did not address the release of bug ** /
Platform: | Size: 5101 | Author: 叶振华 | Hits:

[GUI Develop简单日志

Description: 你在写程序中是否要经常输出信息作为观察你的程序是否正常运行的标记. 本程序给你一个可以在Windows中用printf功能的函数log_printf,当然有更多的功能啦,自己看看就知道啦-you write procedures should always output information as you observe the procedures for normal operation markings. This program can give you a Windows function using printf function log_printf, of course, have more functions -- and see for yourself know --
Platform: | Size: 432185 | Author: 罗清胜 | Hits:

[ActiveX/DCOM/ATLTrace_dll

Description: Trace跟踪动态库,文件读写,支持如printf函数的一样参数-Trace DLL tracking, document literacy, such as support functions like printf parameters
Platform: | Size: 11421 | Author: 乔文杰 | Hits:

[Other resourcec环境下的编译器

Description: 在c环境下的编译器 一.实验完成主要功能描述: 1.if语句 2.if_else语句 3.while语句 4.数组 5.函数调用 6.对外部函数printf的调用(用于打印) 二.测试环境 1.fedora core 4(OS) 2.lex version 2.5.4 3.yacc 4.gcc (GCC) 4.0.0 20050519 (Red Hat 4.0.0-8) -in the context of a compiler. Experiments main function Description : 1.if statement 2.if_else statement 3.while statements 4. 5 array. Function call 6. The external function calls printf (print) 2. A test environment . fedora core 4 (OS) 2.lex 3.yacc 4.gcc version 2.5.4 (GCC) 4.0.0 20050519 (Red Hat 4.0.0-8)
Platform: | Size: 5945 | Author: 韩天毅 | Hits:

[Other三星S344B0X蜂鸣器测试

Description: 三星S344B0X蜂鸣器测试 // if(key== + ) // if(BeepFreq<20000) // BeepFreq += 100 // if(key== - ) // if(BeepFreq>100) // BeepFreq -=100 // SetBeepPwm(BeepFreq, 50) // printf(\"Now beep frequence is %d\\n\", BeepFreq) // Delay(2000000) // StopBeepPwm() -Samsung S344B0X buzzer testing / / if (key ==) / / if (BeepFreqlt; 20000) / / BeepFreq = 100 / / if (key ==-) / / if (BeepFreqgt; 100) / / BeepFreq -= 100 / / SetBeepPwm (BeepFreq, 50) / / printf ( "Now beep frequence is% d \\ n", BeepFreq) / / Delay (2000000) / / StopBeepPwm ()
Platform: | Size: 78149 | Author: kong | Hits:

[Other resourcekeil_iodemo

Description: The project KEIL_IODemo shows how to use memory allocation routines (malloc) and char I/O (printf, scanf) via a serial interface with the Keil ARM toolchain. The I/O functions are adapted for the Analog Devices ADuC7000 series using the SERIAL.C module. The example also shows the efficiency of the Keil CA ARM Compiler run-time library which is tuned for single chip systems. -The project shows how to use me mory allocation routines (malloc) and char I / O (printf, Scanf) via a serial interface with the Keil ARM t oolchain. The I / O functions are adapted for the Analog Devices ADuC7000 series using the SERIA Synchronization module. The example also shows the efficien cy of the CA Keil ARM Compiler run-time library w hich is tuned for single chip systems.
Platform: | Size: 19884 | Author: 郭文彬 | Hits:

[Other resourcegnu_iodemo

Description: UART I/O and Memory Allocation Example for GNU The project GNU_IODemo shows how to use memory allocation routines (malloc) and char I/O (printf, scanf) via a serial interface with the GNU toolchain. The I/O functions are adapted for the Analog Devices ADuC7000 series using the SERIAL.C module. The example also shows the efficiency of the Keil CA ARM Compiler run-time library which is tuned for single chip systems.-UART I / O and Memory Allocation for Example The GNU project GNU_IODemo shows how to use memo ry allocation routines (malloc) and char I / O (p rintf. Scanf) via a serial interface with the GNU toolc DERA. The I / O functions are adapted for the Anal og Devices ADuC7000 series using the SERIAL.C m odule. The example also shows the efficiency of the Keil CA ARM Compiler run-time library which is tuned for single chip systems.
Platform: | Size: 42967 | Author: 郭文彬 | Hits:

[Other resourcemips-uart-16550

Description: MIPS架构下串口API函数集合,包括getchar\\putchar\\scanf\\printf-MIPS architecture Serial API function set, including getchar \\ putchar \\ Scanf \\ printf
Platform: | Size: 3976 | Author: 栋梁 | Hits:

[Embeded-SCM Developuart_printf

Description: MSP430F5529单片机使用标准C语言printf()函数使参数值打印到PC显示屏、Windows 超级终端等等,对初学者有很大帮助。(MSP430F5529 microcontroller using the standard C language printf () function, so that the parameter values printed to the PC display, Windows super terminal and so on, very helpful for beginners.)
Platform: | Size: 504832 | Author: 似情非爱 | Hits:

[SCMuart_printf

Description: 基于89c51单片机的printf语句串口文本输出程序(Based on 89c51 single-chip printf statement serial output program)
Platform: | Size: 43008 | Author: hjhoooo | Hits:

[OtheruxPrintf

Description: 实现了printf的简单功能,对于单片机开发调试很有帮助!!(Realize the simple function of printf, SCM development and debugging helpful!!)
Platform: | Size: 2048 | Author: fmliywtk | Hits:

[Embeded-SCM Develop16路demo

Description: STC单片机使用printf函数,不影响串口功能,一组定时器记录系统运行时间(STC microcontroller uses the printf function, does not affect the serial port function, a set of timers record the system running time)
Platform: | Size: 56320 | Author: 伍陆柒 | Hits:

[Otherusart串口

Description: 在清风的基础上,实现PRINTF的输出(On the basis of clean wind, the output of PRINTF is realized)
Platform: | Size: 1084416 | Author: xuelongzhiwu | Hits:

[SCMSTM32f103 串口printf输出

Description: 可以通过 “USERT” 使用printf函数(The printf function can be used by "USERT")
Platform: | Size: 2508800 | Author: 解药 | Hits:

[Com Port51UART

Description: 51单片机的串口实现printf,printf自己修改的,系统printf不好用,来源:51hei(51 singlechip serial port realization printf, printf itself modified, the system printf is not good, source)
Platform: | Size: 40960 | Author: BLDCM | Hits:

[SCM6. SC-printf

Description: 单片机串口通讯协议的实现,使用printf输出到电脑(The realization of the serial communication protocol of single chip microcomputer, using printf to output to the computer)
Platform: | Size: 28672 | Author: 1231231231231231 | Hits:

[source in ebookC语言中printf的使用

Description: printf()函数是式样化输出函数, 一般用于向准则输出设备按规定式样输出消息。正在编写步骤时经常会用到此函数。printf()函数的挪用式样为: printf("<式样化字符串>",<参量表>);(The printf () function is a stylized output function, which is generally used to output messages to a standard output device in a prescribed pattern. You are often used to this function when you are writing the steps. The misappropriation of the printf () function is as follows: Printf ("< stylized string >", < parametric Table >);)
Platform: | Size: 5120 | Author: Bruce Leung | Hits:

[SCM实验15:ADC printf输出

Description: stm32f103c8t6 jlink keil5 adc printf
Platform: | Size: 6278144 | Author: zy小琐 | Hits:

[Com Port打印串口 程序 8463B printf

Description: 赛元单片机的串口打印汉字程序,支持赛元全系列芯片,非常棒!(Uart Printf str program,is very good!)
Platform: | Size: 33792 | Author: liug527 | Hits:
« 1 2 3 4 5 67 8 9 10 11 ... 38 »

CodeBus www.codebus.net