P176 1.2.3

Experience: The first problem is to use a one-dimensional array of programming examples.

1. The first step in the number n of unsorted (a [0] ~ a [n-1]) to find the minimum number in it with a [0] exchange.

2. In the second step the remaining n-1 number of unordered (a [1] ~ a [n-1]) to find the minimum number in it with a [1] exchange.

...........

3. The first two numbers n-1 steps (a [n-2] ~ a [n-1]) in the remaining unsorted find the minimum number, it will be a [n-2] exchange.

 

The second question is similar to the example 7-6.

 

The third problem is the use of a two-dimensional matrix array of programming on the issue of the triangle. Under standard law as i <= j

 

 

 

/ * 
Sort 1. selection. Enter a positive integer n (1 <n <10) , then enter the positive integer n, the descending order and outputs them. 
 Write the corresponding test procedures. * / 
#Include <stdio.h>
 int main ( void ) 
{ 
    int I, index, K, n-, T;
     int A [ 10 ]; 
    the printf ( " Enter n-: " ); 
    Scanf ( " % D " , & n- ); 
    the printf ( " Enter% D Integer; " , n-);
         for (I = 0 ; I <n-; I ++ ) 
            Scanf ( " % D " , A & [I]);
        for(k=0;k<n-1;k++)
{
            index=k;
          for(i=k+1;i<n;i++)
            if(a[i]<a[index])
                index=i;
            t=a[index];
            a[index]=a[k];
            a[k]=t;
}
     printf("after sorted:");
       for(i=0;i<n;i++)
         printf("%4d",a[i]);
         printf("\n");
         return 0;

}

/ * Find the largest integer number that appears in a number of * / 

#include <stdio.h>
 int main ( void ) 
{ 
  int n-, I, max = 0 , TEMP, Time [ 10 ] = { 0 }; 
  the printf ( " the Enter n-: " ); 
  Scanf ( " % D " , & n-); 
  the printf ( " the Enter% D integers: " , n-);
   for (I = 0 ; I <n-; I ++ ) { 
    Scanf ( " % D " , & TEMP);
     the while (TEMP =! 0 ) {
      time[temp%10]++;
      temp/=10;
    }
  }
    for(i=0;i<10;i++){
      if(max<time[i])
        max=time[i];
    }
  printf("出现最多次数%d次的数字是:",max);
  for(i=0;i<10;i++)
    if(time[i]==max)
      printf("%d ",i);

  printf("\n");
  return 0;
}

/*
3.判断上三角矩阵。输入一个正整数n(1<n<6)和n阶方阵a中的元素,
如果a是上三角矩阵,输出"YES",否者输出“NO”。上三角矩阵
指主对角线以下的元素都为零的矩阵。主对角线为从矩阵的左上角至右下角的连线。
试编写相应程序。*/
#include<stdio.h>
int main(void)
{
    int a[6][6],flag,i,j,n;
    printf("enter n:");
    scanf("%d",&n);
    printf("enter array:\n");
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    flag=1;
    for(i=0;i<n;i++)
        for(j=0;j<i;j++)
            if(a[i][j]!=0)
                flag=0;
            if(flag)
                printf("YES\n");
            else
                printf("NO\n");
            return 0;
}

Guess you like

Origin www.cnblogs.com/yixiaoxia/p/yixiaoxia8.html