博文

报数问题(循环链表的应用)(2005-04-28 12:11:00)

摘要:问题; 一个经常出现的问题: n个人围成一圈,从第一个人开始1,2,3报数,当一个人报到数3时,退出.接下的从一在开始往下抱,遇3退出,依次循环下去,直到只剩一个人,输出这个人的原始数,如:输入7 结果输出;4 #include<stdio.h> #include<alloc.h> #include<conio.h> typedef struct node { int data; struct node *next; }node,*cllist; cllist head; void createlist(void) { cllist p,new,new1; int i; printf("please input the number of people:"); scanf("%d",&i); printf("\n"); while(i<=1) { printf("error due to the wrong input ! please input it again!\n"); scanf("%d",&i); } head=NULL; p=new=(node *)malloc(sizeof(node)); if(!new) {   printf("overflow!"); exit(0); } new->next=head; head=new; head->data=i; while(i>1) {   new1=(node *)malloc(sizeof(node)); if(!new1)    {     printf("overflow.\n"); exit(0); } i--; new1->next=head; head=new1; head->data=i; } p->next=head; p=p->next; while(head->next!=head)    { &nb......

阅读全文(5141) | 评论:0

sinx求值(2005-04-13 22:18:00)

摘要:#include<stdio.h> #include<math.h> #define PI 3.14159 #define EPS 1e-7 main() { double x,sum=0,an; int n; clrscr(); printf("please input the number:\n"); scanf("%lf",&x); an=x; n=1; do {   sum+=an;   ++n;   an*=-x*x/((2*n-2)*(2*n-1)); }while(fabs(an)<EPS); printf("the sin(%.4f) is:%.4f",x,sum); } ......

阅读全文(3310) | 评论:0

魔方距阵(2005-04-12 16:14:00)

摘要:#include<stdio.h> #define N 15 main() { int i,j,row,cloum,size,square[N][N],count; clrscr(); printf("please enter the square size(odd && <=15):\n"); scanf("%d",&size); while(size%2==0||size>15||size<3) { printf("error due to the wrng input!please input it again!\n"); scanf("%d",&size); } for(i=0;i<size;i++)   for(j=0;j<size;j++)   square[i][j]=0; i=0;j=(size-1)/2; square[i][j]=1; for(count=2;count<=size*size;count++) { row=i-1<0?(size-1):(i-1); cloum=j-1<0?(size-1):(j-1); if(square[row][cloum]) i=(++i)%size; else {i=row; j=j-1<0?(size-1):(j-1); } square[i][j]=count; } printf("the %d square is:\n",size); for(i=0;i<size;i++)   { for(j=0;j<size;j++) printf("%d",square[i][j]); printf("\n"); } } ......

阅读全文(3701) | 评论:0

选择排序法(2005-04-12 11:37:00)

摘要:#include<stdio.h> #include<math.h> #define wap(x,y,t) ((t)=(x),(x)=(y),(y)=(t)) #define max 12 void sort(int a[],int x); main() { int a[max],i,j; clrscr(); printf("how many elems do you want to compare?number(<=12):\n"); scanf("%d",&i); while(i>12||i<=0) { printf("error due to the wrong input,please input it again!\n"); scanf("%d",&i); } printf("\nplease enter the numbers:\n"); for(j=0;j<i;j++) { fflush(stdin);   scanf("%d",&a[j]);   } printf("\nthe original numbers is:\n"); for(j=0;j<i;j++) { printf("%d",a[j]); printf("/t/t"); } printf("/n"); sort(a,i); } void sort(int a[],int x) { int i,j,maxsu,k,m,temp; for(i=0;i<(x-1);i++) {   maxsu=a[i];   for(j=i;j<x;j++)   {     if(a[j]<maxsu)      {      maxsu=a[j];   &n......

阅读全文(4372) | 评论:0

求二次方程的根的问题(2005-04-02 20:42:00)

摘要:#include<stdio.h> #include"math.h" void seekroot (float a,float b,float c) { float m,term1,term2; if(a==0) if(b==0) printf("the answer not exist.\n"); else printf("the answer is %f\n",-c/b); else   {   m=b*b-4*a*c;   if(m>0)   { term1=(-b+sqrt(m))/(2*a); term2=(-b-sqrt(m))/(2*a); printf("the root is %f and %f",term1,term2);} else printf("the real root not exsit !\n");    } } main() { float d,e,f; printf("please input three numbers;\n"); scanf("%f%f%f",&d,&e,&f); seekroot(d,e,f); ......

阅读全文(4174) | 评论:0

起泡法排序(2005-04-02 20:40:00)

摘要:#include<stdio.h> void enter(float a[],int n) { int i; printf("please input numbers:\n"); for(i=0;i<n;i++) scanf("%f",&a[i]); printf("\nthe origin numers are:\n"); for(i=0;i<n;i++) printf("%f",a[i]); printf("\n"); return; } void bubble(float a[],int n) { int i,j,t,m; float k; for(i=0;i<n;i++) { t=n-i-1; for(j=0;j<t;j++) if(a[j]>a[j+1]) { k=a[j]; a[j]=a[j+1]; a[j+1]=k; } } printf("the versed  numbers are:\n"); for(m=0;m<n;m++) printf("%f",a[m]); printf("\n"); } main() { int n; float a[12]; printf("how many numbers do you want to input?number:\n"); scanf("%d",&n); enter(a,n); bubble(a,n); ......

阅读全文(6615) | 评论:0

兔子繁殖问题(2005-04-02 20:39:00)

摘要:#include<stdio.h> void trade (a,b,c) { b=c; a=b; } main() { int n,i,fbn,fbn1,fbn2; printf("please input the month which you want to know:"); scanf("%d\n",&n); if(n>13 || n<1) printf("error due to the wrong input !\n"); else if(n==1|| n==2)         printf("the total number of the rabbits are 1.\n");         else         fbn1=fbn2=1;             for(i=3;i<=n;i++)             {          fbn=fbn1+fbn2;                   fbn2=fbn1;       fbn1=fbn;             }   &nb......

阅读全文(4881) | 评论:0

将十进制数转化为8进制(2005-04-02 20:38:00)

摘要:#define STACK_INIT_SIZE  8 #define STACKINCRMENENT 4 #include <stdio.h> #include<math.h> typedef struct{ int *base; int *top; int stacksize; }sqstack; typedef sqstack *stacklist; stacklist p; stacklist initstack (stacklist p) { p->base=(int *)malloc(STACK_INIT_SIZE *sizeof(int)); if(!p->base)   {    printf("overflow!\n");    exit(0);   }   p->top=p->base; p->stacksize=STACK_INIT_SIZE ; printf("succeed in applying for a room!\n"); return(p); }   stacklist push (stacklist p,int m) { if(p->top-p->base>=p->stacksize)    {     p->base=(int *)realloc(p->base,(p->stacksize+STACKINCRMENENT)*sizeof(int));     if(!p->base)       {        printf("overflow leads to failure!\n");     exit(0);  &nb......

阅读全文(3323) | 评论:0