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

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:

[Windows Developdgxjyffxq

Description: 递归下降语法分析器,如果有错误,请把getch()改成getchar() 把putch()改成putchar()VC6.0下编译执行OK-recursive syntax analyzer decline, if there was any mistake. Please getch () changed getchar () putch () changed putchar () V under +6.0 compiler implementation OK
Platform: | Size: 1091 | Author: JOHN | Hits:

[OpenGL programOpenGLSolarSys

Description: Open GL Line int main() { int gdriver=DETECT, gmode initgraph(&gdriver, &gmode, \"c:\\\\tc\") lineBres(30,20,100,150) getch() closegraph() }
Platform: | Size: 92595 | Author: mwb | Hits:

[Other Web Code词法分析程序(编译原理)

Description: 用C语言写的词法分析程序 #include "stdio.h" #include"string.h" #include "stdlib.h" FILE *fp; char filename[20]; char token[100]; int m=0; char getch() { char ch; ch=fgetc(fp); return ch; } char getbc(char ch) { while(ch==' ') ch=getch(); return ch; }
Platform: | Size: 1218 | Author: zhimoai | Hits:

[ELanguagepl/0

Description: /*PL/0编译系统C版本头文件pl0.h*/ /* typedef enum { false, true } bool; */ #define norw 13 /*关键字个数*/ #define txmax 100 /*名字表容量*/ #define nmax 14 /*number的最大位数*/ #define al 10 /*符号的最大长度*/ #define amax 2047 /*地址上界*/ #define levmax 3 /*最大允许过程嵌套声明层数[0,levmax]*/ #define cxmax 200 /*最多的虚拟机代码数*/ /*符号*/ enum symbol{ nul, ident, number, plus, minus, times, slash, oddsym, eql, neq, lss, leq, gtr, geq, lparen, rparen, comma, semicolon, period, becomes, beginsym, endsym, ifsym, thensym, whilesym, writesym, readsym, dosym, callsym, constsym, varsym, procsym, }; #define symnum 32 /*名字表中的类型*/ enum object{ constant, variable, procedur, }; /*虚拟机代码*/ enum fct{ lit, opr, lod, sto, cal, inte, jmp, jpc, }; #define fctnum 8 /*虚拟机结构代码*/ struct instruction { /*454*/ enum fct f; // 虚拟机代码指令 int l; //引用层与声明层的层次差 int a; //根据f的不同而不同 }; FILE * fas; //输出名字表 FILE * fa; //输出虚拟机代码 FILE * fa1; //输出源文件及其各行对应的首地址 FILE * fa2; //输出结果 bool listswitch; //显示虚拟机代码与否 bool tableswitch; //显示名字与否 char ch; //获取字符的缓冲区,getch使用 enum symbol sym; //当前的符号 char id[al+1]; //当前ident,多出的一个字节用于存放0 int num; //当前number int cc,ll; //getch使用的计数器,cc表示当前字符(ch)的位置 int cx; //虚拟机代码指针,取值范围[0,cxmax-1] char line[81]; //读取行缓冲区 char a[al+1]; //临时符号,多出的一个字节用于存放0 struct instruction code[cxmax]; //存放虚拟机代码的数组 char word[norw][al]; //保留字 enum symbol wsym[norw]; //保留字对应的符号值 enum symbol ssym[256]; //单字符的符号值 char mnemonic[fctnum][5]; //虚拟机代码指令名称 bool declbegsys[symnum]; //表示声明开始的符号集合 bool statbegsys[symnum]; //表示语句开始的符号集合 bool facbegsys[symnum]; //表示因子开始的符号集合 //名字表结构 struct tablestruct { char name[al]; //名字 enum object kind; //类型:const,var,array or procedure int val; //数值,仅const使用 int level; //所须层,仅const不能用 int adr; //地址,仅const不能用 int size; //需要分配的数据空间,仅procedure使用 }; struct tablestruct table[txmax]; //名字表 FILE * fin; FILE * fout; char fname[al]; int err; //错误计数器 //当函数中发生fatal error时,返回-1告知调用它的函数,最终退出程序 #define getsymdo if(-1==getsym()) return -1 #define getchdo if(-1==getch()) return -1 #define testdo(a,b,c) if(-1==test(a,b,c)) return -1 #define gendo(a,b,c) if(-1==gen(a,b,c)) return -1 #define expressiondo(a,b,c) if(-1==expression(a,b,c)) return -1 #define factordo(a,b,c) if(-1==factor(a,b,c)) return -1 #define termdo(a,b,c) if(-1==term(a,b,c)) return -1 #define conditiondo(a,b,c) if(-1==condition(a,b,c)) return -1 #define statementdo(a,b,c) if(-1==statement(a,b,c)) return -1 #define constdeclarationdo(a,b,c) if(-1==constdeclaration(a,b,c)) return -1 #define vardeclarationdo(a,b,c) if(-1==vardeclaration(a,b,c)) return -1 void error(int n); int getsym(); int getch(); void init(); int gen(enum fct x,int y ,int z); int test(bool *s1,bool *s2,int n); int inset(int e,bool *s); int addset(bool *str,bool * s1,bool * s2,int n); int subset(bool *str,bool * s1,bool * s2,int n); int mulset(bool *str,bool * s1,bool * s2,int n); int block(int lev,int tx,bool * fsys); void interpret(); int factor(bool * fsys,int * ptx,int lev); int term(bool * fsys,int * ptx,int lev); int condition(bool * fsys,int * ptx,int lev); int expression(bool * fsys,int * ptx,int lev); int statement(bool * fsys,int * ptx,int lev); void listcode(int cx0); int vardeclaration(int *ptr, int lev,int *ptx); int constdeclaration(int *ptr, int lev,int *ptx); int position(char * idt,int tx); void enter(enum object k,int * ptx,int lev, int * pdx); int base(int l,int * s,int b)
Platform: | Size: 25139 | Author: xqq771084591 | Hits:

[CSharp迷宫求解的非递归算法

Description: #include #include #include #define N 20 int aa[N][N]; int yes=0; int x[100][2],n=0; void fun1(int (*aa)[N],int (*a)[N]); int fun(int (*a)[N],int i,int j); void begain(int (*t)[N]); void pr(int (*t)[N],int nn); void win(int (*t)[N]); void lose(); void main(void) { int t[N][N]; begain(t); pr(t,0); fun(t,1,1); if(yes) win(t); else lose(); getch(); } void fun1(int (*aa)[N],int (*a)[N]) { int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) aa[i][j]=a[i][j]; } int fun(int (*a)[N],int i,int j) { if(i==N-2&&j==N-2) { yes=1; return; } a[i][j]=1; fun1(aa,a); if(aa[i+1][j+1]==0&&!yes) { fun(aa,i+1,j+1); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i+1][j]==0&&!yes) { fun(aa,i+1,j); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i][j+1]==0&&!yes) { fun(aa,i,j+1); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i-1][j]==0&&!yes) { fun(aa,i-1,j); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i-1][j+1]==0&&!yes) { fun(aa,i-1,j+1); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i+1][j-1]==0&&!yes) { fun(aa,i+1,j-1); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i][j-1]==0&&!yes) { fun(aa,i,j-1); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } fun1(aa,a); if(aa[i-1][j-1]==0&&!yes) { fun(aa,i-1,j-1); if(yes) {x[n][0]=i,x[n++][1]=j;return;} } } void begain(int (*t)[N]) { int i,j; system(cls); randomize(); for(i=0;i<N;i++) { for(j=0;j<N;j++) { if(i==0||i==N-1||j==0||j==N-1) t[i][j]=1; else if(i==1&&j==1||i==N-2&&j==N-2) t[i][j]=0; else t[i][j]=random(2); } } } void pr(int (*t)[N],int nn) { int i,j,ii; textcolor(RED); gotoxy(1,1); for(i=0;i<N;i++) { for(j=0;j<N;j++) { if(nn!=1) printf(%2d,t[i][j]); else { for(ii=0;ii<n;ii++) { if(x[ii][0]==i&&x[ii][1]==j) { cprintf(%2d,t[i][j]); break; } } if(ii<n) continue; if(i==N-2&&j==N-2) cprintf( 0); else printf(%2d,t[i][j]); } } printf(\n); } } void win(int (*t)[N]) { int i,j,ii,jj; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(x[j][0]==x[i][0]&&x[j][1]==x[i][1]) { for(jj=j,ii=i;jj=0;i--) printf(%3d%3d->,x[i][0],x[i][1]); printf(%3d%3d\n,N-2,N-2); t[1][1]=0; pr(t,1); } void lose() { printf(\nNot find way!\n); }
Platform: | Size: 854 | Author: 461600486@qq.com | Hits:

[Windows Developdgxjyffxq

Description: 递归下降语法分析器,如果有错误,请把getch()改成getchar() 把putch()改成putchar()VC6.0下编译执行OK-recursive syntax analyzer decline, if there was any mistake. Please getch () changed getchar () putch () changed putchar () V under+6.0 compiler implementation OK
Platform: | Size: 1024 | Author: JOHN | Hits:

[OpenGL programOpenGLSolarSys

Description: Open GL Line int main() { int gdriver=DETECT, gmode initgraph(&gdriver, &gmode, "c:\\tc") lineBres(30,20,100,150) getch() closegraph() } -Open GL Line int main () (int gdriver = DETECT, gmode initgraph (
Platform: | Size: 1274880 | Author: mwb | Hits:

[ELanguagecifafenxi

Description: 词法分析程序:一. 为PL/0语言建立一个词法分程序GETSYM(函数) 把关键字、算符、界符称为语言固有的单词,标识符、常量称为用户自定义的单词。为此设置三个全程量:SYM,ID,NUM 。 SYM:存放每个单词的类别,为内部编码的表示形式。 ID:存放用户所定义的标识符的值,即标识符字符串的机内表示。 NUM:存放用户定义的数。 GETSYM要完成的任务: 1. 滤掉单词间的空格。 2. 识别关键字,用查关键字表的方法识别。当单词是关键字时,将对应的类别放在SYM中。如IF的类别为IFSYM,THEN的类别为THENSYM。 3. 识别标识符,标识符的类别为IDENT,IDRNT放在SYM中,标识符本身的值放在ID中。关键字或标识符的最大长度是10。 4. 拼数,将数的类别NUMBER放在SYM中,数本身的值放在NUM中。 5. 拼由两个字符组成的运算符,如:>=、<=等等,识别后将类别存放在SYM中。 6. 打印源程序,边读入字符边打印。 由于一个单词是由一个或多个字符组成的,所以在词法分析程序GETSYM中定义一个读字符过程GETCH。 -err
Platform: | Size: 7168 | Author: xxx | Hits:

[Windows Developcifafenxi

Description: token,字符数组,长度为32,存放识别出来的单词符号。 tmp,字符数组,长度为256,可以缓存用户输入的字符串。 char *fgets(char *string, int n, *stdin):*string输入数据的首地址, n 一次读入数据块的长度, stream文件指针. Void getbc() //检查ch中的字符是否为空白字符,如是则调用getch(),直到ch中读入一个非空白字符为止. fgets() //从流中读取一字符串 Void getch() //读字符函数,从输入缓冲区读入写一个字符放在ch中,并指向下一个字符。 int stricmp(const char *s1,const char *s2) 比较字符串s1与s2的大小,并返回s1-s2. int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0. int reserve() //对token中的字符串查关键字表,若是则返回关键字,否则返回15.-err
Platform: | Size: 1024 | Author: 乐正清 | Hits:

[SCMkeyscan

Description: 功能:4×4键盘扫描程序 返回:键序号(0~F),0xFF表示没有字符输入 软件特点:简洁高效,无须延时消抖,避免连发 编程语言:Keil-C -Function: 4 × 4 keyboard scanner return: key serial number (0 ~ F), 0xFF that there is no character input software features: simple and efficient, no need to delay extinction Buffeting to avoid bursts of programming languages: Keil-C
Platform: | Size: 1024 | Author: 李文旭 | Hits:

[Linux-UnixLinux_getch()

Description: LINUX 下无 getch() 函数,这是一个模拟实现getch() 函数的源代码.-Under LINUX without getch () function, which is a simulation of the realization of getch () function of the source code.
Platform: | Size: 107520 | Author: wcx | Hits:

[Other Gamesxiaobawangxuexiji

Description: c语言编写的小霸王学习机界面,一个小车的游戏,给出简单加减运算,用户输入结果,正确并给出评判小车前进一步。(本代码是在win—tc写的,在visual c++里边运行要将getch()去掉。)-c language learning machine interface written Sundance Kid, a car game, give a simple addition and subtraction, the results of user input, and gives judges the right car forward. (This code is written in the win-tc in visual c++ to run inside to getch () removed.)
Platform: | Size: 5120 | Author: cpcwest | Hits:

[SCMhongwaiyaokongwendu

Description: DS18B20温度采集 ...Ir_work(void)....红外键值散转程序 ...Ircordpro(void)...红外码值处理函数 ...void Getch()...键盘扫描-DS18B20 temperature acquisition ... Ir_work (void ).... infrared key bulk transfer process ... Ircordpro ??(void) ... IR code value processing function ... void Getch ()... keyboard scan
Platform: | Size: 38912 | Author: 张均平 | Hits:

[Windows DevelopORDER2-EQUATION

Description: #include<stdio.h> #include<conio.h> #include<math.h> main(){ float delta,a,b,c float x,x1,x2,z printf("enter a:") scanf(" f",&a) printf("enter b:") scanf(" f",&b) printf("enter c:") scanf(" f",&c) delta=(b*b)-((4)*(a)*(c)) if(delta<0)printf("delta < 0") if(delta==0){ x=(-b)/(2*a) printf("X = 3.2f",x) } if(delta>0){z=sqrt(delta) x1=(-b-z)/(2*a) x2=(-b+z)/(2*a) printf("x1 = 3.2f",x1) printf("\nx2 = 3.2f",x2) } printf("\n") getch() }-#include<stdio.h> #include<conio.h> #include<math.h> main(){ float delta,a,b,c float x,x1,x2,z printf("enter a:") scanf(" f",&a) printf("enter b:") scanf(" f",&b) printf("enter c:") scanf(" f",&c) delta=(b*b)-((4)*(a)*(c)) if(delta<0)printf("delta < 0") if(delta==0){ x=(-b)/(2*a) printf("X = 3.2f",x) } if(delta>0){z=sqrt(delta) x1=(-b-z)/(2*a) x2=(-b+z)/(2*a) printf("x1 = 3.2f",x1) printf("\nx2 = 3.2f",x2) } printf("\n") getch() }
Platform: | Size: 8192 | Author: shafagh | Hits:

[Software Engineeringtime-example

Description: 1.程序分析: 2.程序源代码: #include "stdio.h" #include "conio.h" #include "time.h" void main() { time_t lt /*define a longint time varible*/ lt=time(NULL) /*system time and date*/ printf(ctime(<)) /*english format output*/ printf(asctime(localtime(<))) /*tranfer to tm*/ printf(asctime(gmtime(<))) /*tranfer to Greenwich time*/ getch() } -shi jiancheng xumiao shu
Platform: | Size: 3072 | Author: zhenxinjian | Hits:

[assembly languagedisplaystring

Description: enter and display string by using getch() function and the enter of string has no restriction of size
Platform: | Size: 1024 | Author: chris wong | Hits:

[VC/MFCLab02-Supplement1-Stopping-the-Output-Window-in-D

Description: You will no be able to see the output: Welcome to ICS 103: Programming in C To overcome this we can stop the output window in two ways: Use an input statement such as getch(), getchar(), or scanf before the return statement Use the Operating System call system("PAUSE") before the return statement The first method is better than the second as it results in a more efficient program. -You will no be able to see the output: Welcome to ICS 103: Programming in C To overcome this we can stop the output window in two ways: Use an input statement such as getch(), getchar(), or scanf before the return statement Use the Operating System call system("PAUSE") before the return statement The first method is better than the second as it results in a more efficient program.
Platform: | Size: 11264 | Author: alaa | Hits:

[.netFBPredict

Description: this football prediction for windows. it can getch from website data and match with the goals, results and predict next.... thx u.-this is football prediction for windows. it can getch from website data and match with the goals, results and predict next.... thx u.
Platform: | Size: 2195456 | Author: ngelay | Hits:

[Othergetch_hanshu

Description: getch()函数的功能介绍,详细说明该函数的作用-getch () function Features, detailed description of the role of the function
Platform: | Size: 1024 | Author: 小易 | Hits:
« 12 »

CodeBus www.codebus.net