Learn C programming in 11 days

Day 11 Samples from students

8 Queen problem by Sun Zhonghua

 

#include <stdio.h>

 

void qtry(int, int queen[8]);

 

int s = 0;

 

void main()

{

     int queen[8] = {0,0,0,0,0,0,0,0};

     qtry(0, queen);

     printf("Number of solutions: %d\n",  s);

}

 

void qtry(int n, int queen[8])

{

     int i,j;

     int  flag;

     for (i=0; i<=7; i++){

         flag=1;

         for (j=0; j<n; j++) {

              if ( i==queen[j] ) flag=0;

              if ( (n+i)==(queen[j]+j) ) flag=0;

              if ( (n-i)==(j-queen[j]) ) flag=0;

         }

         if ( flag==1 ) {

              queen[n] = i;

              if ( n<7 ) {

                   qtry(n+1, queen);

              } else {

                   printf("find    ");

                   for ( j=0; j<=7; j++) {

                       printf("%d ", queen[j]);

                   }

                  s = s + 1;

                   printf("%d\n", s);

              }

         }

     }

}       

 

Calender by Li Chao

 

/*********************************************/

/*        医预 李超  年历 2001.6.1        */

/*********************************************/

 

#include <stdio.h>

#include <stdlib.h>

 

struct

{

     int day[6][7];

} calendar[13];

 

void month(int n)

{

     char form[]={"%12s%10s"};

     switch(n)

     {

         case  1: printf(form, "JAN", " "); break;

         case  2: printf(form, "FEB", " "); break;

         case  3: printf(form, "MAR", " "); break;

         case  4: printf(form, "APR", " "); break;

         case  5: printf(form, "MAY", " "); break;

         case  6: printf(form, "JUN", " "); break;

         case  7: printf(form, "JUL", " "); break;

         case  8: printf(form, "AUG", " "); break;

         case  9: printf(form, "SEP", " "); break;

         case 10: printf(form, "OCT", " "); break;

         case 11: printf(form, "NOV", " "); break;

         case 12: printf(form, "DEC", " "); break;

     }

}

 

void main()

{

     int year, n;

 

     printf("input a year: ");

     scanf("%d",&year);

     year=year-1;

     n = year/4 - year/100 + year/400;

     n = 365*year + n + 1;

     n = n%7;

 

     int i,j, k, l;

     int *p, *p1;

     int a=1, b=2, c=3;

 

    char day[]="SU MO TU WE TH FR SA ";

     for (i=1; i<13; i++)

     {

         p1 = p = &calendar[i].day[0][0] + n;

         for (j=1; j<29; j++)

         {

              *p=j;

              p++;

         }

         if (i==1||i==3||i==5||i==7||i==8||i==10||i==12)

         {

              for (j=29; j<32; j++)

              {

                   *p=j;

                   p++;

              }

         }

         else if (i==2)

         {

              year = year + 1;

              if (year%4==0 && year%100!=0 ||year%400==0)

              {

                   *p=29;

              }

         }

         else if(i==4  || i==6 ||i==9 ||i==11)

              for (j=29; j<31; j++)

              {

                   *p=j;

                   p++;

              }

              n = (n + (p-p1)) % 7;

     }

    system("cls");

     printf("                             %d\n", year);

     printf("___________________________________________________________________\n");

     for (i=1; i<=4; i++)

     {

         month(a); month(b); month(c);

         printf("\n");

    printf("|%s|%s|%s|\n",day, day, day);

         for (j=0; j<6; j++)

         {  

              for (k=(i-1)*3+1; k<=(i-1)*3+3; k++)

              {

                   printf("|");

                   for (l=0; l<7; l++)

                   {

                       if ( calendar[k].day[j][l]!=0 )

                       printf("%2d ",calendar[k].day[j][l]);

                   else

                       printf("   ");

                   }

              }

             printf("|\n");

         }

         a+=3; b+=3; c+=3;

          printf("___________________________________________________________________\n");

         getchar();

     }

}