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

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 resourcertos_serial

Description: 本库利用51的timer2,9600bps适用于有片内外部存储器的51芯片占用32byte缓存,重写putchar,getchar函数,循环队列,后台操作。-for the use of timer2 51, 9600 applied to the external memory unit within the occupied 32byte 51 chip cache, rewritten putchar, getchar function, circulating cohort, background operation.
Platform: | Size: 3573 | Author: 浪人 | Hits:

[Other resourcertos串口函数库

Description: RtosSerial.lib rtosSerial.h 使用说明,本库利用51的timer2,9600bps适用于有片内外部存储器的51芯片占用32byte缓存,重写putchar,getchar函数,循环队列,后台操作。 可与rtx51很好集成。任何人可以自由分发,和应用。有疑问可以联系serialrtos@163.net -RtosSerial.lib rtosSerial.h use, the use of the library's timer2 51, 9600 applied to the external memory unit within the occupied 32byte 51 chip cache, rewritten putchar, getchar function, circulating cohort, background operation. With rtx51 well integrated. Anyone is free to distribute, and applications. Doubt can be linked serialrtos@163.net
Platform: | Size: 3573 | 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:

[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:

[Otherchs

Description: C语言编写的中文处理模板。用getzh()函数代替getchar()函数来从标准输入中读取汉字。附带范例程序。
Platform: | Size: 1032 | Author: KiD | Hits:

[Other resourcegetchar

Description: 带有退格键的键盘输入程序 (for MSP430),修正厂家代码中对退格键不支持的错误
Platform: | Size: 1283 | Author: 米凯 | Hits:

[Other resourceC++_programes

Description: 高质量C++编程 电子版 林锐  如果输入参数以值传递的方式传递对象,则宜改用“const &”方式来传递,这样可以省去临时对象的构造和析构过程,从而提高效率。  【规则6-2-2】函数名字与返回值类型在语义上不可冲突。 违反这条规则的典型代表是C标准库函数getchar。
Platform: | Size: 71692 | Author: netwalk | Hits:

[Other resourceavr_usart

Description: AVR306串口程序例子----查询模式 Application Note AVR306 * Polled mode driver for UART, this is the similar to the * library default putchar() and getchar() in ICCAVR
Platform: | Size: 1076 | Author: 翁寅生 | Hits:

[SourceCodecaculator

Description: // caculator.cpp : Defines the entry point for the console application. // #include #include #include #include #include char e; int main(int argc, char *argv[]) { do { float a,c,d; char b; printf("input first number\n"); scanf("%f",&a); printf("input operator\n"); getchar(); scanf("%c",&b); printf("input the second number\n"); scanf("%f",&c); switch(b) { case'+':d=a+c;break; case'-':d=a-c;break; case'*':d=a*c;break; case'%':if(c==0) { printf("error\n"); break;} else d=a/c;break; case'^':d=pow(a,c);break; default:printf("error\n"); } printf("%f\n",d); printf("do you want to continue y/n\n"); getchar(); scanf("%c",&e);} while(e=='y'); return 0; }
Platform: | Size: 875 | Author: fmrnn@sina.com | Hits:

[SCMrtos_serial

Description: 本库利用51的timer2,9600bps适用于有片内外部存储器的51芯片占用32byte缓存,重写putchar,getchar函数,循环队列,后台操作。-for the use of timer2 51, 9600 applied to the external memory unit within the occupied 32byte 51 chip cache, rewritten putchar, getchar function, circulating cohort, background operation.
Platform: | Size: 3072 | Author: 浪人 | Hits:

[SCMrtos串口函数库

Description: RtosSerial.lib rtosSerial.h 使用说明,本库利用51的timer2,9600bps适用于有片内外部存储器的51芯片占用32byte缓存,重写putchar,getchar函数,循环队列,后台操作。 可与rtx51很好集成。任何人可以自由分发,和应用。有疑问可以联系serialrtos@163.net -RtosSerial.lib rtosSerial.h use, the use of the library's timer2 51, 9600 applied to the external memory unit within the occupied 32byte 51 chip cache, rewritten putchar, getchar function, circulating cohort, background operation. With rtx51 well integrated. Anyone is free to distribute, and applications. Doubt can be linked serialrtos@163.net
Platform: | Size: 3072 | Author: www | Hits:

[ARM-PowerPC-ColdFire-MIPSmips-uart-16550

Description: MIPS架构下串口API函数集合,包括getchar\putchar\scanf\printf-MIPS architecture Serial API function set, including getchar \ putchar \ Scanf \ printf
Platform: | Size: 4096 | Author: 栋梁 | 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:

[Otherchs

Description: C语言编写的中文处理模板。用getzh()函数代替getchar()函数来从标准输入中读取汉字。附带范例程序。-C language, the Chinese deal with templates. With getzh () function in place of getchar () function to read from standard input Chinese characters. Incidental examples of the procedures.
Platform: | Size: 1024 | Author: KiD | Hits:

[SCMgetchar

Description: 带有退格键的键盘输入程序 (for MSP430),修正厂家代码中对退格键不支持的错误-Backspace with the keyboard input program (for MSP430), to amend the code of manufacturers backspace does not support error
Platform: | Size: 1024 | Author: 米凯 | Hits:

[VC/MFCC++_programes

Description: 高质量C++编程 电子版 林锐  如果输入参数以值传递的方式传递对象,则宜改用“const &”方式来传递,这样可以省去临时对象的构造和析构过程,从而提高效率。  【规则6-2-2】函数名字与返回值类型在语义上不可冲突。 违反这条规则的典型代表是C标准库函数getchar。 -High-quality C++ Programming the electronic version of Mr
Platform: | Size: 71680 | Author: netwalk | Hits:

[VC/MFCputchar-()-and-getchar-()--

Description: putchar () 函数和 getchar () 的一些简单应用实例程序,已经通过调试,很适合初学C语言的人学习。-putchar () and getchar () function of some simple practical examples!
Platform: | Size: 6144 | Author: 李风 | Hits:

[Othergetchar

Description: getchar 的简单程序 希望对初学者有用-the getchar simple program useful for beginners
Platform: | Size: 6144 | Author: qi | Hits:

[Othergetchar

Description: getchar函数取消获得回车符 回车符-getchar ahanshu
Platform: | Size: 138240 | Author: 刘波 | Hits:
« 12 3 »

CodeBus www.codebus.net