Welcome![Sign In][Sign Up]
Location:
Search - v.34

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:

[Software EngineeringV.34-V.90-modem

Description: 传真相公资料,基于VOIPIP语音技术。V.34和V.90调制解调器指南-fax young gentleman, on VOIPIP voice technology. V.34 and V.90 modem Guide
Platform: | Size: 235484 | Author: wuhx | Hits:

[Develop ToolsDelphi7_Lite_Full_20110501

Description: 【Borland是一家令人敬仰的公司,當值Borland已成往事之際,謹以此產品獻給曾經的Borland,紀念已經仙逝的Borland,藉以緬懷Borland傳奇中的經(精)典Delphi7.本產品僅供學習交流之用,未與任何盈利為目的,請勿用於任何非法的商業用途,否則後果自負.】 Delphi7 Lite,簡約而不簡單,一次安裝,到處運行.內含4彈:Tiny/Mini/Meduim/Full,功能和體積由小至大,力求達到功能和體積的完美平衡.已作性能優化及優量精簡,含安裝/綠色便攜版二合一,集成了部分常用高效的IDE專家及外掛程式,致力於把Delphi7打造成一個現代化(相對而言)的整合式開發環境,使用本產品將使你在開發效率/使用體驗/系統相容性等方面上相對原版有大幅提高. 关键特性: 一次安裝,到處運行.第一次需安裝,以後每次只需註冊即可. 已安裝集成現在所有可用的升級補丁(至Delphi7 Update 1.1,IDE版本號為Build 8.1),集成目前絕大多數的Bug Fix Pack. 支援現在所有正在使用的Windows版本:Windows7, 2008 R2, Vista, XP, 2008, 2003, 2000, Me, 98, 和 NT 4.0,支援非管理員許可權使用者安裝,支援UAC. 特別地對Windows7提供全面支援. 去除了所有多餘的安裝選項但保留了原始程式碼選項,移除了非必需的檔(ModelMaker,InterBase,MergeModules,Extra Docs,Images,MDAC等). 採用安裝後動態編譯生成RTL,VCL的DCU檔,極大的減小了安裝檔體積. 可選將Delphi7 RTL/VCL替換為Delphi2007 的RTL/VCL,讓Delphi7支援Delphi2007新增的Vista屬性. 可選將Delphi7 RTL/VCL 恢復為使用官方原始的源碼檔,僅供用於部分協力廠商元件兼容性的可能需要(不推薦). 安裝程式是設計為可重複安裝的.您在安裝新版本時,如果安裝程式沒有特別指明,則不需要先刪除舊的版本,直接升級即可,所有設置都會保留,原Delphi安裝的控制項及設置等會自動導入新安裝IDE環境中. 安裝程式內置了可直接選擇的”綠色便攜版安裝模式”,使用此模式安裝後目的檔案夾即成為一個可移動的綠色版,實現徹底的名符其實的”安裝/綠色便攜版二合一”. 安裝程式自帶獨立的綠化註冊程式(D7LiteReg.exe),方便您快速地恢復Delphi7開發環境(控制項/專家/設置).直接複製整個Delphi目錄,再執行D7LiteReg.exe即可完成Delphi的註冊工作.同樣方法可適用于對其它Delphi7版本進行綠化註冊,如Delphi7 SECOND EDITION v7.2. 安裝程式支援”使用現有的原始程式碼重編譯安裝(僅覆蓋安裝時有效)”或”使用外部的Delphi7-source原始程式碼來編譯安裝”,更多詳情請查看說明. 安裝程式帶附加的命令列功能.支援對已安裝程式的功能表/註冊表/雜項進行修復,使用命令列參數/?或/help查看更多詳情. 安裝程式支援附加的命令列參數以實現額外功能.支援對已安裝Delphi7程式的功能表/註冊表/雜項進行修復,常用的命令列參數已直接集成到了安裝程式中,在視窗中點擊右鍵或使用命令列參數/?或/help查看更多詳情. 安裝程式會根據安裝目的機器性能自動選擇不同的安裝元件. 安裝程式支援多語言介面(含中文簡體/繁體/英文). 同時可選僅替換為Delphi2007中windows相關pas,以支援XP/Vista API及視窗屬性,不同于完全替換Delphi2007 RTL/VCL,能最大程度保持Delphi7的相容性的同時支援vista的新API及視窗屬性. 代碼編輯器顏色方案設為Delphi2006樣式. 包含程式設計專用字體Consolas (推薦字體大小為10並且啟用ClearType). IDE及VCL預設字體由原先的MS Sans Serif改為Tahoma,介面看上去更為美觀. 可選擇修改Delphi IDE本身支援XP風格,在設計時即可見到程式運行的XP效果,所見即所得. 集成DelphiZLib 1.2.5(ZLibEx.pas),可選擇升級內置的ZLib 1.04至ZLib 1.2.5. 集成VCL Fix Pack 1.4,需要在工程中手動引用單元VCLFixPack.pas. 集成Midas Speed Fix 1.2,需要在工程中手動引用單元MidasSpeedFix.pas. 集成以下來自www.delphi-jedi.org的擴展的Win32Api單元: SHELL LITE :: Microsoft Shell Lightweight Utility API, v.1.2/SNMP :: Microsoft headers for Simple Network Management Protocol definitions/WINSOCK 2 :: Winsock 2 API (Windows Sockets). 集成MySQL Driver for DBExpress,支援MySQL 3.22/4.0/4.1/5.0 驅動程式. 集成Newly Delphi Features Extented Components,移植自Delphi7以后版本新增的控制項,如Vista Dialogs等,讓Delphi7也可以開發Vista風格對話方塊的程式. 包含下列專家或工具(依據版本不同有所差異): Delphi IDE 外掛程式 DelphiSpeedUp 3.1.(IDE加速). Delphi IDE 外掛程式 DDevExtensions 2.3.(增強IDE中工程的設置及編譯,可為工程增加不同版本編譯配置如release/debug版). Delphi 專家 GExperts 1.34 Experimental.(超級IDE專家). Delphi 專家 DelforExp 2.5.(代碼格式化工具). Delphi 專家 UnitExpert(提高IDE單元操作效率工具). Delphi 專家 CnWizards 0.9.7.599.(由中國人開發的超級IDE專家,支援原始程式碼結構高亮連線顯示,能自動備份恢復已安裝控制項資訊,製作自己綠化版delphi的好工具). Delphi 工具 DelphiDistiller 1.85.(選擇IDE啟動時載入的控制項及設置工具). Spy工具Microsoft Spy++ v7.1和Spy4Win (Spy for Window) v0.20b. EXE/DLL依賴關係分析工具Dependency Walker 2.2. DLL匯出函數檢視器DLL Export Viewer v1.42. BDE (Borland Database Engine 5.2), 及相關工具BDE Administrator, SQL Explorer, Database Desktop, SQL Monitor, Data Pump等. BDE 示例資料庫(別名DBDEMOS).
Platform: | Size: 77731415 | Author: ygman@qq.com | Hits:

[Software EngineeringV.34-V.90-modem

Description: 传真相公资料,基于VOIPIP语音技术。V.34和V.90调制解调器指南-fax young gentleman, on VOIPIP voice technology. V.34 and V.90 modem Guide
Platform: | Size: 235520 | Author: wuhx | Hits:

[Audio program0653-800

Description: 用于GSM的增强全速率EFR的语音编码器标准C源码-For the GSM Enhanced Full Rate EFR speech coding standard C source
Platform: | Size: 372736 | Author: huangtao | Hits:

[Program docITU-TT-REC-V[1].34-199802-I!!PDF-E

Description: itu-t recommendation standard v.34 (fast)
Platform: | Size: 395264 | Author: sradu | Hits:

[Windows DevelopBootloader_9600IP_1.34

Description: Maxx 1000 satellite receiver bootloader v 1.34
Platform: | Size: 53248 | Author: Fazal | Hits:

[Internet-NetworkNS2.34-CMU_Trace

Description: cmu-trace source codes for NS2 v.2.34.
Platform: | Size: 13312 | Author: arch | Hits:

[Internet-NetworkNS2.34-setdest_Utility

Description: setdest utility source codes for NS2 v.2.34. By this utility you can create your mobility (movement) files and attach theme to your main simulation script.
Platform: | Size: 20480 | Author: arch | Hits:

[OtherPV_Array_Sfun

Description: 光伏mppt 算法 采用尚德 STP200-18/Ub 电池板 功率 200W maximum power 200W 开路电压 33.4V open circuit voltage 33.4V 短路电流 8.12A short circuit current 8.12A 工作电压 26.2V maximum power voltage 26.2V 工作电流 7.63A maximum power current 7.63A 最大系统电压 1000V maximum system voltage 1000V 电流温度系数(A/K) 0.045 /℃ 电压温度系数(V/K) -0.34 /℃-Solar mppt algorithm using the the Suntech STP200-18/Ub panels power 200W maximum power 200W open circuit voltage of 33.4V open circuit voltage 33.4V short-circuit current of 8.12A Short Circuit Current 8.12A working voltage 26.2V maximum power voltage 26.2V operating current of 7.63A maximum power current 7.63A Maximum system voltage 1000V maximum system voltage 1000V current temperature coefficient (A/K) 0.045 /° C voltage temperature coefficient (V/K)-0.34 /℃
Platform: | Size: 2048 | Author: 王哲 | Hits:

[Data structsfloyd

Description: 设计动态规划算法求解最长路径问题(见下面附录部分),要求程序能根据给定的图作为输入,输出最长路径的长度及一条最长的路径。 [附] 最长路径问题(见教材第143页习题7.34) 已知G=< V,E > 是一个有n个顶点的有向无回路的带权图(边权都为正整数)。设s和t是V中的两个顶点,其中s的入度为0,t的出度为0。请设计一个动态规划算法求G中从s到t的最长路径的长度及一条最长的路径,并分析该算法的时间复杂度 -Design dynamic programming algorithm for solving the longest path problem (see the appendix section below), the requirements of the program according to a given graph as input, output length of the longest path and a longest path. [With] the longest path problem known (see 143 textbook exercises 7.34) G = < V, E> is a n vertices of to loop a weighted graph (the edge weight is a positive integer). Let s and t is two vertices, and V s into the degree of 0 out of t is 0. Please design a dynamic programming algorithms to find the length of the longest path from s to t G, and one of the longest path, and analyze the time complexity of the algorithm
Platform: | Size: 903168 | Author: liangchuqi | Hits:

[DocumentsHuffman

Description: 根据赫夫曼编码的原理,编写一个程序建立英文字母的赫夫曼编码。下面是英文字母使用频率表( ): A 8.19 B 1.47 C 3.83 D 3.91 E 12.25 F 2.26 G 1.71 H 4.57 I 7.10 J 0.14 K 0.41 L 3.77 M 3.34 N 7.06 O 7.26 P 2.89 Q 0.09 R 6.85 S 6.36 T 9.41 U 2.58 V 1.09 W 1.59 X 0.21 Y 1.58 Z 0.08 -According to Huffman coding theory, write a program to establish the letters of Huffman coding. Here is the English alphabet using frequency table ( )
Platform: | Size: 2048 | Author: lipei | Hits:

[Internet-Network19-B】金龙板_Virtual_COM

Description: 戴南饿哦我愤怒v呢人家女今儿虐问富翁魏汝稳 微软为认为热(asdwaeifbewiri3 wetrwer wte wtwe tt 3 4t t3t 34 2r wr wq eweqter t erw g wer g ds fg sd fg e rger)
Platform: | Size: 1865728 | Author: zlchxy | Hits:

[EditorUTLog_OK_1

Description: Sentinel SRM/HASP/HL Dumper v.1.9 (public) build on (19:35:34 Dec 21 2013)
Platform: | Size: 43008 | Author: 隔壁老王Orz | Hits:

CodeBus www.codebus.net