小代码 细心体会 含3对父子编程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小代码 细心体会 含3对父子编程相关的知识,希望对你有一定的参考价值。
#include"wz.h" #define n 5 #define N 5 //1 struct pb{int *p;struct{int x, y;}s;struct pb *next;}; void neicunduiqi() { struct pb w; printf("内存对齐\nsize of struct pb%2d\n",sizeof(w));} //2 void jcqh() { int nn=4;int i=0; int sum=1; int SUM=0;printf("阶乘求和:\n"); for(i=1;i<=nn;i++){sum*=i;SUM+=sum;} printf("%d\n",SUM); } //3 void swap(int*x,int *y) { if(*y!=*x){*y=*x^*y;*x=*x^*y;*y=*x^*y;}} void r_array(int a[],int nn){int f,l; for(f=0,l=nn-1;f<=l;f++,l--)swap(&a[f],&a[l]);} void display(int a[],int nn){int i;for(i=0;i<nn;i++)printf("%2d",a[i]);printf("\n");} void swap_array() { int a[6]={1,2,3,4,5,6};int m=6; printf("(swap):\n");r_array(a,m); display(a,m);} //4 void mppx(int v[],int nn) { int i=0,j=0,t=0; for(i=0;i<nn;i++) { for(j=0;j<nn-1-i;j++) { if(v[j]>v[j+1]) {t=v[j+1];v[j+1]=v[j];v[j]=t;} } } } void display1(int a[],int nn){int i;for(i=0;i<nn;i++)printf("%2d",a[i]);printf("\n");} void paixu_array(){ int x[9]={1,3,5,7,9,2,4,6,8}; printf("数组 冒泡排序:\n"); mppx(x,9); display1(x,9); } //5 void qiuandhoutao() { double hight=100.0,sum=0.0; int day=9,tao=1,i,nn=0; printf("落球和猴吃桃问题:\n"); while(hight>0.01) {hight=hight/2;++nn;} hight=100; for(i=1;i<=nn;i++) {sum=sum+2*hight; hight=hight/2;} printf("sum=%f\n",sum-100); while(day) { tao=2*(tao+1); day--;} printf("tao=%d\n",tao); } //6 void dis(int x[][n]) { int i=0;int j=0; for(i=0;i<n;i++)//for必须补齐,不然易错,至少自己虚拟机测试过 { for(j=0;j<n;j++){printf("%4d",x[i][j]);} printf("\n"); } } void findout(int x[][n]){} void migong() { int x[n][n]={0};int r,c,m; m=n*n/4; time_t t; srand((unsigned)time(&t)); while(m) { r=rand()%6; c=rand()%4; x[r][c]=1;x[0][0]=0; m--; } //printf("%d\n",m); printf("随机生成的迷宫如下\n"); dis(x); findout(x); } int jc(int x){int i=1;int ret=1;for(;i<=x;i++) ret*=i;return ret;} int c(int m,int l){return(jc(m)/(jc(l)*jc(m-l)));} int pw(int x){int i=1;int ret=1;for(;i<=x;i++) ret*=2;return ret;} void fuzipaidui() { int nn=3; int i=0; int flag=-1; int kinds=jc(2*nn); printf("父子排队问题:\n"); for(i=1;i<=nn;i++) {kinds+=flag*jc(2*nn-i)*pw(i)*c(nn,i);flag=-flag;} printf("kinds=%d\n",kinds); } void zArray() {int bz=0; int x[N][N]={0}; int i=0,j=0; x[0][0]=0; printf("打印Z数组:\n"); while(bz<N-2)//N=4,bz<N-1总会出错 { if(bz%2==0) { x[i+1][0]=x[i][0]+1;i++; while(i>0) {x[i-1][j+1]=x[i][j]+1;i--;j++;} bz++; } if(bz%2==1) { x[0][j+1]=x[0][j]+1;j++; while(j>0) {x[i+1][j-1]=x[i][j]+1;i++;j--;} bz++; } } //中间副对角线 x[N-1][0]=x[N-2][0]+1; i=N-1;j=0; while(i>0) { i--;j++; x[i][j]=x[i+1][j-1]+1; } bz=0; x[N-1][N-1]=N*N-1;i=N-1;j=N-1; //右下三角 while(bz<N-2) { if(bz%2==0) // 上移后下降 { x[i-1][j]=x[i][j]-1;i--; while(i<N-1) {x[i+1][j-1]=x[i][j]-1;i++;j--;} bz++; } if(bz%2==1)//左移后上升 { x[i][j-1]=x[i][j]-1;j--; while(j<N-1) {x[i-1][j+1]=x[i][j]-1;i--;j++;} bz++; } } dis(x); } //7 void xitong_time( void ) { time_t t = time(0); char tmp[64]; printf("系统时间:\n"); strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) ); puts( tmp ); } void jzqh() { int x[5][5]={0}; int i=0,j=0; int sum1=0,sum2=0; for(i=0;i<n;i++) { for(j=0;j<n;j++){ x[i][j]=i*j;} } for(i=0;i<n;i++) { for(j=0;j<n;j++){ if(i==j) sum1+=x[i][j];if(i+j==4) sum2+=x[i][j];} } dis(x); printf("主对角和=%3d,副对角和=%3d\n",sum1,sum2); } void main() { int x=18;int y=12;int xy=x*y;int r=x; if(x<y){x=x^y;y=x^y;x=x^y;} while(r) { r=y%x; y=x; x=r; } printf("(x,y)=%3d\n", y); printf("(x,y)=%3d\n", xy/y); jzqh(); xitong_time(); zArray(); fuzipaidui(); migong(); swap_array(); paixu_array(); jcqh(); neicunduiqi(); }
以上是关于小代码 细心体会 含3对父子编程的主要内容,如果未能解决你的问题,请参考以下文章