Chapter VI Hemopurification C language after-school exercise

1, less than 100 by seeking prime screening

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main()
 4 {
 5     int i,j,n,a[101];
 6     for(i=1;i<=100;i++)
 7         a[i]=i;
 8     a[1]=0;
 9     for(i=2;i<sqrt(100);i++)
10         for(j=i+1;j<=100;j++)
11         {
12             if(a[i]!=0&&a[j]!=0)
13                 if(a[j]%a[i]==0)
14                     a[j]=0;
15         }
16         printf("\n");
17         for(i=2,n=0;i<=100;i++)
18         {if(a[i]!=0)
19         {printf("%5d",a[i]);
20         n++;
21         }
22         if(n==10)
23         {printf("\n");
24         n=0;
25         }
26         }
27         printf("\n");
28         return 0;
29 }

2, ten integers sorted by selective

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int i,j,min,temp,a[11];
 5     printf("enter data:\n");
 6     for(i=1;i<=10;i++)
 7     {
 8         printf("a[%d]=",i);
 9         scanf("%d",&a[i]);
10     }
11     printf("\n");
12     printf("the number is");
13     for(i=1;i<=10;i++)
14         printf("%5d",a[i]);
15         printf("\n");
16     for(i=1;i<=9;i++)
17     {min=i;
18     for(j=i+1;j<=10;j++)
19     if(a[min]>a[j])
20     
21     /*    min=j;//法1
22         temp=a[i];
23         a[i]=a[min];
24         a[min]=temp;*/
25     {temp=a[i];  //法2
26     a[i]=a[j];
27     a[j]=temp;}
28     }
29     printf("number is:\n");
30     for(i=1;i<=10;i++)
31         printf("%5d",a[i]);
32     printf("\n");
33      Return  0 ;
34      }

 3, seeking a 3 * 3 matrix and the diagonal elements of the 1

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a[3][3],sum=0;
 5     int i,j;
 6     printf("enter data:");
 7         for(i=0;i<3;i++)
 8             for(j=0;j<3;j++)
 9                 scanf("%d",&a[i][j]);
10             for(i=0;i<3;i++)
11                 sum=sum+a[i][i];
12             printf("sum=%6d\n",sum);
13                 return 0;
14 }

4, a number of the inserted row has a good array

1  // assumed to be sorted in ascending order 
2 #include <stdio.h>
 . 3  int main ()
 . 4  {
 . 5      int A [ . 11 ] = { 1 , . 4 , . 6 , . 9 , 13 is , 16 , . 19 , 28 , 40 , 100 } ;
 . 6      int temp1, temp2 of, Number, End, I, J;
 . 7      the printf ( " Array A: \ n- " );
 . 8      for (I = 0 ; I < 10 ; I ++ )
 9         printf("%5d",a[i]);
10     printf("\n");
11     printf("inset data:");
12     scanf("%d",&number);
13     end=a[9];
14     if(number>a[9])
15         a[10]=number;
16     else
17     {
18         for(i=0;i<10;i++)
19         {if(a[i]>number)
20         {temp1=a[i];
21         a[i]=number;
22         for(j=i+1;j<11;j++)
23         {temp2=a[j];
24         a[j]=temp1;
25         temp1=temp2;
26         }
27         break;
28         }
29         }
30     }
31     printf("now array a:\n");
32     for(i=0;i<11;i++)
33     printf("%5d",a[i]);
34         printf("\n");
35     return 0;
36 }

5, the values ​​in the array in reverse order of the discharge

 1 #include<stdio.h>
 2 #define N 6
 3 int main()
 4 {
 5     int a[N],i,temp;
 6     printf("enter array :\n");
 7     for(i=0;i<N;i++)
 8         scanf("%d",&a[i]);
 9     printf("array a:\n");
10     for(i=0;i<N;i++)
11         printf("%4d",a[i]);
12     for(i=0;i<N/2;i++)
13     {temp=a[i];
14     a[i]=a[N-i-1];
15     a[N-i-1]=temp;
16     }
17     printf("\n now array is:\n");
18     for(i=0;i<N;i++)
19         printf("%4d",a[i]);
20     printf("\n");
21     return 0;
22 }

 6, Triangle

. 1 #include <stdio.h>
 2  #define N 10
 . 3  int main ()
 . 4  {
 . 5      int I, J, A [N] [N];
 . 6      for (I = 0 ; I <N; I ++) // first let the first column and diagonal equal. 1 
. 7      {A [I] [I] = . 1 ;
 . 8      A [I] [ 0 ] = . 1 ;
 . 9      }
 10      for (I = 2 ; I <N; I ++) // third row starts 
. 11          for (J = . 1 ; J <= I- . 1 ; J ++) // from the start of the second row 
12             A [I] [J] = A [I- . 1 ] [J- . 1 ] + A [I- . 1 ] [J]; // equal to one plus the first element on an element of the element 
13 is          for (I = 0 ; I <N; I ++ )
 14          {
 15              for (J = 0 ; J <= I; J ++ )
 16                  the printf ( " % 6D " , A [I] [J]);
 . 17              the printf ( " \ n- " ); // end of each line wrap 
18 is          }
 . 19          the printf ( " \ n- " );
 20 is          return 0;
21 }

7, the output of magic square

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a[15][15],i,j,k,p,n;
 5     p=1;
 6     while(p==1)
 7     {
 8         printf("enter n(n=1--15):");
 9         scanf("%d",&n);
10         if((n!=0)&&(n<=15)&&(n%2!=0))
11             P = 0 ;
 12 is      }
 13 is      // initialization 
14      for (I = . 1 ; I <n-; I ++ )
 15          for (J = . 1 ; J <= n-; J ++ )
 16              A [I] [J] = 0 ;
 . 17          / / build magic square 
18 is          J = n-/ 2 + . 1 ;
 . 19          A [ . 1 ] [J] = . 1 ;
 20 is          for (K = 2 ; K <= n-n-*; K ++ )
 21 is          {
 22 is              I = I- . 1;
23             j=j+1;
24             if((i<1)&&(j>n))
25             {    i=i+2;
26             j=j-1;}
27             else
28             {if(i<1) i=n;
29             if(j>n) j=1;}
30             
31             if(a[i][j]==0)
32                 a[i][j]=k;
33             else
34             {i=i+2;
35             j=j-1;
36             a[i][j]=k;
37         }
38         }
39         //输出
40         for(i=1;i<=n;i++)
41         {for(j=1;j<=n;j++)
42         printf("%5d",a[i][j]);
43         printf("\n");
44         }
45         return 0;
46 }

 

8, a two-dimensional array to find the saddle point, that is the largest in the bank, the smallest of the column

 1 #include<stdio.h>
 2 #define N 4
 3 #define M 5
 4 int main()
 5 {
 6     int i,j,k,a[N][M],max,maxj,flag;
 7     printf("please input matrix:\n");
 8     for(i=0;i<N;i++)
 9         for(j=0;j<M;j++)
10             scanf("%d",&a[i][j]);
11         for(i=0;i<N;i++)//Cyclic N line 
12 is          {
 13 is              max = A [I] [ 0 ];
 14              MAXJ = 0 ;
 15              for (J = 0 ; J <M; J ++ )
 16                  IF (A [I] [J]> max) // Found maximum line 
. 17                  {max = A [I] [J];
 18 is                  MAXJ = J;
 . 19                  }
 20 is                  In Flag = . 1 ;
 21 is                  for (K = 0 ; K <N; K ++) // judged whether or not the column minimum 
22 is                      IF (max > A [K] [MAXJ])
23                     {flag=0;
24                     continue;}
25                     if(flag)
26                     {printf("a[%d][%d]=%d\n",i,maxj,max);
27                     break;}
28         }
29         if(!flag)
30             printf("not exit! \n");
31         return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/1998wdq/p/11305397.html