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

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:

[Other计算一个数的N(整数)次方根的程

Description: 输入一个浮点数,和整数N,输出该浮点数的N次方根-importation of a float, and the integer N, the output of the Nth float -
Platform: | Size: 997 | Author: 山海石 | Hits:

[Other resource改进遗传算法-郭涛算法做最优化问题很管用

Description: 改进遗传算法-郭涛算法做最优化问题很管用,算法的基本思想是 先任意产生n个随机数,然后从n个数里随机选择m个数,再有这m个 数合成一个新数,将这个新数同n个数中间适应值函数值的最差的比较, 如果好的话就取代最差的那个,如果它比最好的还要好的话,则把最好的 也取代。如果比最差的坏,则重新合成一个新数。依次循环下去。 程序的奇妙之处是GA_crossover()函数,产生的新数确实比较好,看看 那位大侠能改进一下,产生比这跟好的数。-improved genetic algorithm - Guo Tao done optimization algorithm is very effective, and the algorithm is the first basic idea arbitrary n generated random number and then the number li n m randomly selected number, and this number m of a new synthesis of the new middle with n number fitness function of the worst by comparison, if a good case to replace the worst, um, if it even than the best, then ruled the best have replaced. If worse than the worst, a de novo synthesis of new. Followed by the cycle continues. The magic of procedures is GA_crossover () function, the new really good to see heroes who can be improved upon, it generated more than a few with a good.
Platform: | Size: 2788 | Author: 龚文引 | Hits:

[Other resourceZUIXINDI

Description: 最接近点对问题,分治法实现,随机生成N个数寻找最短路径的两个点-closest point of the problem, realizing the partition method, random number generation N to find the shortest path to the two points
Platform: | Size: 1369 | Author: 张亮 | Hits:

[Otherdgsxzs

Description: 这个是用c语言编写的一个用递归实现的小程序,程序的功能是实现你输入两个整数m,n(m>n),他输出一连串的数组,其中每个数组都是从这m个数中选择的不同的n个数。找出所有这样的数组。-c is the language used in a recursive procedure to achieve a small, procedural functions of achieving your input two integers m, n (MGT; N), a series of output array, each of which are from the m array select number of different n number. All of this array.
Platform: | Size: 157854 | Author: lxf | Hits:

[Other resourcelqx10005

Description: 给定n 个正整数和4 个运算符+、-、*、/, 且运算符无优先级,如2+3*5=25。对于任意给定 的整数m,试设计一个算法,用以上给出的n 个数 和4 个运算符,产生整数m,且用的运算次数最少 给出的n个数中每个数最多只能用1 次,但每种运 算符可以任意使用。-given positive integer n and four Operators +,-,*,/, but Operators without precedence, if 2 3 * 5 = 25. For any given integer m, design a test algorithm, using the above given number n and four Operators, have integer m, and with the least number of Operational n is the number of each number can only be used up to a meeting, but each Operators can use arbitrary.
Platform: | Size: 2199 | Author: 卢起雪 | Hits:

[Other resourceaaagchcv

Description: 源代码\\用动态规划算法计算序列关系个数 用关系\"<\"和\"=\"将3个数a,b,c依次序排列时,有13种不同的序列关系: a=b=c,a=b<c,a<b=v,a<b<c,a<c<b a=c<b,b<a=c,b<a<c,b<c<a,b=c<a c<a=b,c<a<b,c<b<a 若要将n个数依序列,设计一个动态规划算法,计算出有多少种不同的序列关系, 要求算法只占用O(n),只耗时O(n*n).-source \\ use dynamic programming algorithm sequence relationship with the number "
Platform: | Size: 8433 | Author: Sana | Hits:

[Other resource31yn

Description: 设计一个O(n2)时间的算法,找出由n个数组成的序列的最长单调递增子序列。-design an O (n2) time algorithm to identify by the number n of the longest sequence monotonically increasing sequences.
Platform: | Size: 1787 | Author: 33 | Hits:

[Other resourcePolyFitSingle

Description: //=== === === === === === === = //函数说明 //函数名称:PolyFit //函数功能:最小二乘法曲线拟合 //使用方法:double *x ---- 存放n个数据点的X坐标 // double *y ---- 存放n个数据点的Y坐标 // int n -------- 给定数据点个数 // double *a ---- 返回m-1次拟合多项式的m个系数 // int m -------- 拟合多项式的项数,即拟合多项式的最高次为m-1。要求m<=n,且 // m<=20。若m>n或m>20,则本函数自动按m=min{n,20}处理 // double *dt --- dt[0]返回拟合多项式与各数据点误差的平方和;dt[1]返回拟合多 // 项式与各数据点的误差绝对值之和;dt[2]返回拟合多项式与各数据 // 点误差绝对值的最大值 //注意事项:拟合多项式的形式为 y = b0 + b1*(x-Xavr)... -//=== === === === === ==== === === === === === === ==== === = / / function annotations / / function name : PolyFit / / Function functions : Least Squares Curve Fitting / / Use : double * x n-store data point coordinates of the X / / * y double-storage n data point Y coordinates / / int n -------- given the number of data points / / * a double - --- returned to the m-1 m fit polynomial coefficients / / int m -------- polynomial fitting items , which is the highest polynomial fitting time for m-1. Request m
Platform: | Size: 1373 | Author: 石林 | Hits:

[Otherselectlinklist

Description: 顺序创建N个数的链表L.并输出.再按冒泡排序法进行排序.并输出-order to create N Number of Chain L. and output. Bubble sorting click Sort. And output
Platform: | Size: 1003 | Author: 龙乙 | Hits:

[Windows Developcsharp_little

Description: c#.net常用函数和方法集,C#对注册表的操作,n个数排序,构造函数属性-c #. Net commonly used functions and methods set, C# for the operation of the registry, ranking number n, Constructors attribute
Platform: | Size: 30629 | Author: 张毅松 | Hits:

[Other resourceTotal_Permutations

Description: 任意输入一个整数n,程序将自动生成1到n之间n个数的全部可能排列。利用递归算法。-indiscriminate importation of an integer n, procedures will automatically generate between 1 to n number n of the total potential order. Recursive algorithm.
Platform: | Size: 7286 | Author: 刘金义 | Hits:

[CSharpsgayiyuanyouhua

Description: 改进遗传算法-郭涛算法做最优化问题很管用,算法的基本思想是 先任意产生n个随机数,然后从n个数里随机选择m个数,再有这m个 数合成一个新数,将这个新数同n个数中间适应值函数值的最差的比较, 如果好的话就取代最差的那个,如果它比最好的还要好的话,则把最好的 也取代。如果比最差的坏,则重新合成一个新数。依次循环下去。 -improved genetic algorithm - Guo Tao algorithm optimization is the most effective. Algorithm for the basic idea is to have arbitrary n random number and then the number n m Lane randomly selected number, This again is an m number of new synthesis of the new middle with n number of fitness function of the worst, If good to replace the worst, um, if it is even the best than good, it serves the best have replaced. If worse than the worst, the re-synthesis of a new number. Followed the cycle continues.
Platform: | Size: 2214 | Author: 思寒 | Hits:

[CSharp从n中m的组合

Description: 从n个数中选m个数的所有组合方式-selected from m n Number Number of all combinations
Platform: | Size: 5956 | Author: 林海华 | Hits:

[CSharp求N个数的和

Description: 输如N个数输出这N个数的和,并利用函数调用。
Platform: | Size: 1062 | Author: moxueweirong729252692@_ | Hits:

[SourceCodeN个数的随机重排

Description: N个数的随机重排
Platform: | Size: 893 | Author: gentlebreeze5 | Hits:

[CSharp从n中m的组合

Description: 从n个数中选m个数的所有组合方式-selected from m n Number Number of all combinations
Platform: | Size: 20480 | Author: 林海华 | Hits:

[assembly languageN-numbers-sum

Description: 用汇编语言编写从键盘输入N,代表N个数,就会从1一直加到N求和-Written in assembly language keyboard input from N, N the number of representatives will have been added to the N summation from 1
Platform: | Size: 2048 | Author: 毛毛 | Hits:

[Othern

Description: 求一个集合的第N个元素!一个集合S中的第一个元素是1,如果a ∈ S 则有 2 * a + 1∈ S , 3 * a + 1 ∈ S 在集合中所有元素由你到大排列,求第n个数是多少。-Seek a set of N elements
Platform: | Size: 3072 | Author: 梁狄林 | Hits:

[Applicationsn

Description: 求一个集合的第N个元素!一个集合S中的第一个元素是1,如果a ∈ S 则有 2 * a + 1∈ S , 3 * a + 1 ∈ S 在集合中所有元素由你到大排列,求第n个数是多少。-Seek a set of N elements
Platform: | Size: 3072 | Author: fua678026 | Hits:
« 12 3 4 5 6 7 8 9 10 ... 50 »

CodeBus www.codebus.net