Hot Search : Source embeded web remote control p2p game More...
Location : Home Search - printf.c
Search - printf.c - List

/*
实现效果:
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列的

 


Date : 2008-05-05 Size : 4.29kb User : good@588

DL : 0
C语言输入输出不同数据类型格式转换问题,字符串和数值-C language input and output data types different format conversion, and numerical string
Date : 2025-12-21 Size : 4kb User : ghost2008

DL : 0
学会对文件的记录锁定,及解锁。#include <stdio.h> #include <unistd.h> #include <fcntl.h> int main() { int fd int i struct { char name[20] uint ID int age } myrec fd =open("name", O_RDWR|O_CREAT, 0755) if (fd == -1) return -1 printf("Input your name:") scanf("%s", myrec.name) printf("Inpute your ID :") scanf("%d", &myrec.ID) printf("Input your age :") scanf("%d", &myrec.age) lseek(fd, 0,SEEK_END) lockf(fd, 1, 0) write(fd, (void *)&myrec, sizeof(myrec)) lockf(fd, 0 ,0) return 0 } 执行命令cc lock.c –o lock.out Chmod +x lock.out ./lock.out
Date : 2025-12-21 Size : 9kb User : 华羿

DL : 0
一个Hanoi的小游戏 void Hanoi(int n, char x,char y,char z) { if (n==1) { printf("%c %d %c\n",x,n,z) return } Hanoi(n-1,x,z,y) printf("%c %d %c\n",x,n,z) Hanoi(n-1,y,x,z) }-Hanoi in a game void Hanoi (int n, char x, char y, char z) (if (n == 1) (printf ( c d c , x, n, z) return) Hanoi ( n-1, x, z, y) printf ( c d c , x, n, z) Hanoi (n-1, y, x, z))
Date : 2025-12-21 Size : 142kb User : 123

DL : 0
经典c程序100例 【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去       掉不满足条件的排列。 2.程序源代码: main() { int i,j,k printf("\n") for(i=1 i<5 i++)    /*以下为三重循环*/  for(j=1 j<5 j++)    for (k=1 k<5 k++)    {     if (i!=k&&i!=j&&j!=k)    /*确保i、j、k三位互不相同*/     printf(" d, d, d\n",i,j,k)    } }-c program
Date : 2025-12-21 Size : 18kb User : lengyuegg

c++ 的 functor实现,支持c函数和成员函数,使用非常简单,源码带使用例子 包含头函数:#include "Functor.h" 初始化c函数指针 Functor< int, int, int, int, int, int > fun1( &FuncSum ) printf( "fun1: d\n", fun1.Exec( 1, 2, 3, 4, 5 ) ) Functor< int, int, int, int, int, int > fun2 fun2 = Functor< int, int, int, int, int, int >( &FuncSum ) fun2 = fun1 成员函数初始化: Fool myObj fun3 = Functor< int, float, float >( &myObj, &Fool::CallMethod ) if ( fun3 ) { printf( "fun3 is not null......\n" ) } else { printf( "fun3 is null......\n" ) } if ( fun3 ) { printf( "fun3 result is: d\n", fun3( 1.2f, 2.1f ) ) }-c++ implementation of the functor, support c functions and member functions, using a very simple source code with the use of examples include the first function:# include " Functor.h" initialization c function pointer Functor < int, int, int, int, int, int > fun1 (& FuncSum) printf ( " fun1: d \ n" , fun1.Exec (1, 2, 3, 4, 5)) Functor < int, int, int, int, int, int> fun2 fun2 = Functor < int, int, int, int, int, int> (& FuncSum) fun2 = fun1 member function to initialize: Fool myObj fun3 = Functor < int, float, float> (& myObj, & Fool:: CallMethod) if (fun3) (printf ( " fun3 is not null ...... \ n" ) ) else (printf ( " fun3 is null ...... \ n" ) ) if (fun3) ( printf ( " fun3 result is: d \ n" , fun3 (1.2f, 2.1f)) )
Date : 2025-12-21 Size : 9kb User : zmy

一些以C語言撰寫的小程式,包括printf函數的實作、字串轉浮點數、浮點數轉字串以及最小擴張樹問題等等。-Some with small programs written in C language, including the printf function of the implementation, string float switch, float switch to the string and the minimum spanning tree problem and so on.
Date : 2025-12-21 Size : 4kb User : 鄧慧鈴

DL : 0
C,C++ Questions 1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object,. calling of that virtual method will result in which method being called? a. Base method b. Derived method.. Ans. b 2. For the following C program #define AREA(x)(3.14*x*x) main() {float r1=6.25,r2=2.5,a a=AREA(r1) printf("\n Area of the circle is f", a) a=AREA(r2) printf("\n Area of the circle is f", a) } What is the output? Ans. Area of the circle is 122.656250 Area of the circle is 19.625000 3. What do the following statements indicate. Explain. • int(*p)[10] • int*f() • int(*pf)() • int*p[10] Refer to: -- Kernighan & Ritchie page no. 122 -- Schaum series page no. 323 -C,C++ Questions 1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived object,. calling of that virtual method will result in which method being called? a. Base method b. Derived method.. Ans. b 2. For the following C program #define AREA(x)(3.14*x*x) main() {float r1=6.25,r2=2.5,a a=AREA(r1) printf("\n Area of the circle is f", a) a=AREA(r2) printf("\n Area of the circle is f", a) } What is the output? Ans. Area of the circle is 122.656250 Area of the circle is 19.625000 3. What do the following statements indicate. Explain. • int(*p)[10] • int*f() • int(*pf)() • int*p[10] Refer to: -- Kernighan & Ritchie page no. 122 -- Schaum series page no. 323
Date : 2025-12-21 Size : 34kb User : bhavin

DL : 0
c语言环境下的printf 源代码,具备多种数据类型的输出能力-c language environment printf source code, with the ability to output multiple data types
Date : 2025-12-21 Size : 1kb User : 赵楠

DL : 0
/* 十进制输出 */ printf("int_a = #o\n",int_a) /* 带标识符八进制输出 */ printf("int_a = #x\n",int_a) /* 带标识符十六进制输出 */ printf("char_e = c\n",char_e) /* 字符输出 */ printf("float_pi = f\n",float_pi) /* 实型输出 */ printf("double_g = 15.14g\n",double_pi) /* 15位双精度输出,其中13位小数 */-/* 十进制输出*/ printf("int_a = #o\n",int_a) /* 带标识符八进制输出*/ printf("int_a = #x\n",int_a) /* 带标识符十六进制输出*/ printf("char_e = c\n",char_e) /* 字符输出*/ printf("float_pi = f\n",float_pi) /* 实型输出*/ printf("double_g = 15.14g\n",double_pi) /* 15位双精度输出,其中13位小数*/
Date : 2025-12-21 Size : 49kb User : zouxuan01

DL : 0
/* 十进制输出 */ printf("int_a = #o\n",int_a) /* 带标识符八进制输出 */ printf("int_a = #x\n",int_a) /* 带标识符十六进制输出 */ printf("char_e = c\n",char_e) /* 字符输出 */ printf("float_pi = f\n",float_pi) /* 实型输出 */ printf("double_g = 15.14g\n",double_pi) /* 15位双精度输出,其中13位小数 */-/* 十进制输出*/ printf("int_a = #o\n",int_a) /* 带标识符八进制输出*/ printf("int_a = #x\n",int_a) /* 带标识符十六进制输出*/ printf("char_e = c\n",char_e) /* 字符输出*/ printf("float_pi = f\n",float_pi) /* 实型输出*/ printf("double_g = 15.14g\n",double_pi) /* 15位双精度输出,其中13位小数*/
Date : 2025-12-21 Size : 29kb User : zouxuan01

DL : 0
变温度为华氏摄氏度 然后让你知道温度为多少华氏摄氏度-#include <stdio.h> void main() { double f,c printf("请输入华氏温度:") scanf(" lf",&f) c=5.0/9.0*(f-32) printf("与华氏温度 .2lf对应的摄氏温度是: .2lf\n",f,c) }
Date : 2025-12-21 Size : 1kb User : 李晓歌

C或C++中 printf和scanf函数的用法和特点。很实用,也很简单!-C or C++ in the printf and scanf function usage and characteristics. Very practical, very simple!
Date : 2025-12-21 Size : 1kb User : 刘旭

DL : 0
进制间的转换,顶顶顶,大学期间的作品,经供参考-#include<stdio.h> #include<math.h> int main(){ int b,c,i,j int d[10000] char a[37]={ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , I , S , T , U , V , W , X , Y , Z } while(scanf(" d d",&b,&c)!=EOF){ i=0 while(b/c!=0){ d[i]=b c i++ b=b/c } d[i]=b c if(b<0) printf("-") for(j=i j>=0 j--){ if(d[j]<10) printf(" d",abs(d[j])) if(d[j]>=10) printf(" c",a[d[j]]) } printf("\n") } return 0 }
Date : 2025-12-21 Size : 220kb User : 应超

DL : 0
ADT广义表数据结构的实现 printf("* ADT广义表实现 *\n") printf("****************************************************************\n") printf("* A.创建广义表 B.取表头 C.取表尾 *\n") printf("* D.求长度 E.求深度 F.求原子结点个数 *\n") printf("* G.复制广义表 H.原表插入 I.遍历广义表 *\n") printf("* J.比较广义表 K.销毁表 Q.退出 *\n") -failed to translate
Date : 2025-12-21 Size : 3kb User : 海鹰

DL : 0
求三角形面积 判断三角形的种类以计算面积-#include<stdio.h> #include<math.h> main() { double a,b,c,p,S,i printf("Input a,b,c\n") scanf(" lf lf lf",&a,&b,&c) if(a+b>c&&a+c>b&&b+c>a){ p=(a+b+c)/2 S=sqrt((p-a)*(p-b)*(p-c)) i=0 printf("S= lf\n",S) if(a==b&&b==c){ printf("等边三角形\n") i++ } else if(a==b||a==c||b==c){ printf("等腰三角形\n") i++ } if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a){ if(i==1) printf("等腰直角三角形\n") else printf("直角三角形\n") i++ } if(i==0) printf("一般三角形\n") } else printf("DATA ERROR!\n") }
Date : 2025-12-21 Size : 2kb User : 洛洛

DL : 0
C++小程序,实现将计算结果打印在程序界面中的功能-print the result in the program Window
Date : 2025-12-21 Size : 594kb User : paul

DL : 0
4 examples to C that help you to understand C better
Date : 2025-12-21 Size : 2kb User : xiaohuihui1

The #include <stdio.h> is a preprocessor command. This command tells compiler to include the contents of stdio.h (standard input and output) file in the program. The stdio.h file contains functions such as scanf() and print() to take input and display output respectively. If you use printf() function without writing #include <stdio.h>, the program will not be compiled. The execution of a C program starts from the main() function. The printf() is a library function to send formatted output to the screen. In this program, the printf() displays Hello, World! text on the screen. The return 0; statement is the "Exit status" of the program. In simple terms, program ends with this statement.
Date : 2025-12-21 Size : 17kb User : knidra

DL : 0
In this program, an integer variable number is declared. The printf() function displays Enter an integer: on the screen. Then, the scanf() function reads an integer data from the user and stores in variable number. Finally, the value stored in the variable number is displayed on the screen using printf() function.
Date : 2025-12-21 Size : 33kb User : knidra
« 12 »
CodeBus is one of the largest source code repositories on the Internet!
Contact us :
1999-2046 CodeBus All Rights Reserved.