Welcome![Sign In][Sign Up]
Location:
Search - N-P

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:

[Console递归枚举模板

Description: 递归枚举模板 问题描述:生成长度为n的字串,其字符从26个英文字母的前p(p≤26)个字母中选取,使得没有相邻的子序列相等。例如p=3,n=5时: //  abcba 满足条件 //  abcbc 不满足条件 //输入:n p //输出:所有满足条件的字串及总数-recursive template Problem description : n generation length of the string and its 26 characters from the English alphabet before p (p 26) 000 1984 letter, makes no adjacent sequences equivalent. For example, p = 3, n = 5 : / / abcba meet the conditions / / abcbc not satisfied conditions / / input : n p / / output : to satisfy all the conditions and the total number of strings
Platform: | Size: 1290 | Author: qew | Hits:

[OtherA星算法

Description: 八数码问题的A*算法.本实验的A*算法的h(n)=p(n),p(n)定义为每个将牌与其目标位置之间的距离(不考虑夹在其间的将牌)的总和,显然有p(n)=<h*(n),g(n)仍就为节点深度.-the A * algorithm. The experiment of the A * algorithm h (n) = p (n), p (n) is defined for each license its goal the distance between the location (not consider caught in the crossfire of the license) and the aggregate obviously p (n) =
Platform: | Size: 2615 | Author: 杨秀云霁 | Hits:

[WEB Codeonenewfangfa

Description: 算法不仅具有旋转不变性, 而且对灰度变化、噪声、光照以及对比度变化等也具有很好的鲁棒性, 同时匹配速度比归一化积相关匹配算法(N P rod) 提高了近一倍。-algorithm with a rotating invariance, but the gray changes, noise, light and contrast changes with good robustness, while matching faster than a normalized correlation matching algorithm (N P rod), an increase of nearly 100%.
Platform: | Size: 349335 | Author: 李杰 | Hits:

[Other resourcenbl

Description: 逆波兰式计算,将格式化数组转化为逆波兰表达式,a为输入的数组,n为其长度,p为输出逆波兰表达式的目标,m记录逆波兰表达式的长度 -inverse Polish, the array will be formatted into reverse Polish expression, a for the importation of arrays, its length n, p output of the inverse expression Poland, Poland inverse m record the length of expression
Platform: | Size: 1273 | Author: 郑伟 | Hits:

[Communicationfft_conv

Description: 用FFT分别计算Xa(n) (p=8, q=2)与Xb(n) (a =0.1,f =0.0625)的16点循环卷积和线性卷积。
Platform: | Size: 10018 | Author: 万宁宁 | Hits:

[Othermymapp

Description: 用C++中的MFC编程实现正轴等角割圆柱投影,实现以下要求: 取克拉索夫斯基椭球 (1)制图区域: Bs=0°, BN=25° LE=105°, LE=125° (2)经纬线间隔: ΔB=ΔL=5° (3)制图比例尺: 1:M0=1:1000 000 (4)标准纬线: Bk=±15° 计算经纬网格点的 x, y,m,n, p
Platform: | Size: 41610 | Author: 张建 | Hits:

[Other resourceErChaShu

Description: 假设自上而下按层次,自左至右输入每个结点的一个三元组(N, P, L/R)。其中N为本结点的元素,P为其父结点,L指示N为P 的左孩子,R指示N为P的右孩子。试写一个建立二元树在内存的双链表示算法,并实现先根、中根、后根以及层序遍历算法。
Platform: | Size: 1080 | Author: 江紫桉 | Hits:

[Other resource最大子长方体问题

Description: *Cube:最大子长方体问题 问题描述: 一个长,宽,高分别为m,n,p的长方体被分割成个m*n*p个小立方体。每个小立方体内有一个整数。 求所给长方体的最大子长方体。子长方体的大小由它所含所有整数之和确定。 */-* Cube : The biggest problem for rectangular Problem description : a long, wide and high respectively m, n, p rectangular cut into the 000 m * n * p small cube. Within each small cube is an integer. Seeking to have the largest rectangular son cuboid. The son of rectangular size it contained all integral and defined. * /
Platform: | Size: 1256 | Author: phq | Hits:

[OtherDSPPrograms

Description: DSP编程常用函数 double uniform(double a,double b,long int* seed) double gauss(double mean,double sigma,long int *seed) double exponent(double beta,long int *seed) double laplace(double beta,long int* seed) double rayleigh(double sigma,long int *seed) double weibull(double a,double b,long int*seed) int bn(double p,long int*seed) int bin(int n,double p,long int*seed) int poisson(double lambda,long int *seed) void dft(double x[],double y[],double a[],double b[],int n,int sign) void fft(double x[],double y[],int n,int sign)-DSP programming functions commonly used double uniform (a double, double b, long int * seed) double Gauss (double mean, double sigma, long int * seed) double exponent (double beta, long int * seed) double Laplace (double beta, * long int seed) double Rayleigh (double sigma, long int * seed) double Weibull (a double, double b, long int * seed) int bn (double p, long int * seed) int bin (int n, p double, seed long int *) int poisson (double lambda, long int * seed) void dft index (double x [], double y [], [] a double, double b [], int n, int sign) void fft (double x [], double y [], int n, int sign)
Platform: | Size: 2207 | Author: 山城棒棒儿军 | Hits:

[Mathimatics-Numerical algorithms上大_net-0-1背包问题(回朔法)

Description: 0 / 1背包问题是一个N P-复杂问题,为了解决该问题,,将用回溯算法解决该问题。既然想选择一个对象的子集,将它们装入背包,以便获得的收益最大,则解空间应组织成子集树的形状(如图1 6 - 2所示)。该回溯算法与4 . 2节的装载问题很类似。首先形成一个递归算法,去找到可获得的最大收益。然后,对该算法加以改进,形成代码。改进后的代码可找到获得最大收益时包含在背包中的对象的集合。-0 / 1 knapsack problem is a P-complex issues, in order to solve the problem, and will be used backtracking algorithm to solve the problem. As to the choice of a subset of object, they will load a backpack, in order to obtain the greatest profits, the solution should be organized into space subset of the shape of the tree (Figure 1 6-2 below). The backtracking algorithms and 4. Two loading problem is very similar. Forming a recursive algorithm to get the maximum benefit available. Then, this algorithm is modified to form code. Improved code can be found at the maximum benefits included in the backpack of a collection of objects.
Platform: | Size: 8915 | Author: 周易 | Hits:

[Other Web Code回溯法实现N皇后问题

Description:

vc++源码 N皇后


Platform: | Size: 787 | Author: ykqfan | Hits:

[Other判断整数n是否为素数

Description:

编制具有如下原型的函数prime,用来判断整数n是否为素数:bool prime(int n); 而后编制主函数,任意输入一个大于4的偶数d,找出?


Platform: | Size: 368 | Author: lin_no_7 | Hits:

[Data structsn皇后问题

Description:

五子棋游戏

功能

1:实现人先下或机器先下

2:可以悔棋


Platform: | Size: 4273 | Author: ly21st | Hits:

[SourceCodezuijiasuanfa

Description: #include #include #define M 20 #define N 3 int mm[M]={0,0,0,2,3,1,0,4,2,3,2,1,0,3,2,2,1,3,0,1}; int nn[N]; int number; void Xunhuan(void); void FIFO(void); void Best(void); void LRU(void); int main() { char ss[1]; int i,j, *p=nn; int c, k=0; for(i=0;i
Platform: | Size: 2853 | Author: lipman1987 | Hits:

[Communicationc_FFT

Description: The code performs a number (ITERS) of iterations of the Bailey s 6-step FFT algorithm (following the ideas in the CMU Task parallel suite). 1.- Generates an input signal vector (dgen) with size n=n1xn2 stored in row major order In this code the size of the input signal is NN=NxN (n=NN, n1=n2=N) 2.- Transpose (tpose) A to have it stored in column major order 3.- Perform independent FFTs on the rows (cffts) 4.- Scale each element of the resulting array by a factor of w[n]**(p*q) 5.- Transpose (tpose) to prepair it for the next step 6.- Perform independent FFTs on the rows (cffts) 7.- Transpose the resulting matrix The code requires nested Parallelism.-The code performs a number (ITERS) of iterations of the Bailey s 6-step FFT algorithm (following the ideas in the CMU Task parallel suite). 1.- Generates an input signal vector (dgen) with size n=n1xn2 stored in row major order In this code the size of the input signal is NN=NxN (n=NN, n1=n2=N) 2.- Transpose (tpose) A to have it stored in column major order 3.- Perform independent FFTs on the rows (cffts) 4.- Scale each element of the resulting array by a factor of w[n]**(p*q) 5.- Transpose (tpose) to prepair it for the next step 6.- Perform independent FFTs on the rows (cffts) 7.- Transpose the resulting matrix The code requires nested Parallelism.
Platform: | Size: 4096 | Author: ramya | Hits:

[CommunicationRS9110-N-11-01_Module_DataSheet_V1_1

Description: RS9110-N-11-01 802.11bgn WLAN Module Data Sheet
Platform: | Size: 584704 | Author: SergVas | Hits:

[source in ebookN-sparse

Description: 创建一个n维的稀疏数组对象,n是任意值。 定义N可能是大于2的一类n维稀疏阵列。然而,它应该被认为是一种起动方式与普通的MATLAB稀疏矩阵和重塑它有N维度。换句话说,稀疏的数据,首先必须能够作为一个普通的2D MATLAB稀疏矩阵被前n维。事实上,如果目标数组的尺寸mxnxp……yxz,然后将它存储在内部类是一个普通的二维稀疏阵列的尺寸(M×N×P×……×Y)XZ。这导致了某些内存株时使用大量的尺寸。我发现有用的类主要用于中等尺寸像三维图像边缘检测,你经常要举行一个稀疏的3D的边缘地图。-Creates an N-dimensional sparse array object, for arbitrary N.This submission defines a class of N-dimensional sparse arrays for N possibly greater than 2. However, it should really be thought of as a way of starting with an ordinary MATLAB sparse matrix and reshaping it to have N dimensions. In other words, the sparse data must first be able to exist as an ordinary 2D MATLAB sparse matrix before being made N-dimensional. In fact, if the intended array has dimensions MxNxP...YxZ, then the class will store it internally as an ordinary 2D sparse array of dimensions (M*N*P*...*Y)xZ. This leads to certain memory strains when using large numbers of dimensions. I find the class useful mainly for moderate dimensional things like edge detection in 3D imaging, where you often want to hold a sparse 3D edge map.
Platform: | Size: 23552 | Author: 刘亮 | Hits:

[Othern皇后

Description:  要求采用链表形式,求两个一元多项式的乘积:h3 = h1*h2。函数原型为:void multiplication( NODE * h(NODE; void multiplication( NODE *, NODE * , NODE * ); void input( NODE * ); void output( NODE * ); void input( NODE * head ) { int flag, sign, sum, x; char c; NODE * p = head; while ( (c=getchar()) !='\n' ) { if ( c == '<' ) { sum = 0; sign = 1; flag = 1; } else if ( c =='-' ) sign = -1; else if( c >='0'&& c <='9' ) { sum = sum*10 + c - '0'; } else if ( c == ',' ) { if ( flag == 1 ) { x = sign * sum; sum = 0; flag = 2; sign = 1;)
Platform: | Size: 1024 | Author: 天儿飞 | Hits:

[matlab用Z-N法实现P,PI,PID控制的参数整定

Description: 用MATLAB程序软件完成Z-N法对控制器的参数整定,实现P,PI,PID控制器的分别参数整定,并求出控制器的控制参数Kp,Ti,Td。最后绘制出不同控制器控制下的及参数整定前后的单位阶跃响应图进行比较。(MATLAB program software is used to complete the parameter tuning of the controller by Z-N method. The parameters of P, PI, PID controller are adjusted, and the control parameters of the controller Kp, Ti, Td are obtained. Finally, the unit step response diagram before and after parameter tuning is plotted under different controller control.)
Platform: | Size: 7168 | Author: hhhhyy | Hits:
« 12 3 4 5 6 7 8 9 10 ... 42 »

CodeBus www.codebus.net