C的二維陣列(習題)

慕華思弦發表於2018-07-15

二維和多維陣列:

        二維陣列定義:型別 陣列名[行][列];

        空間長度:sizeof(int)*行*列

        元素個數: 行*列

        引用元素:需要確定哪一行哪一列的元素  陣列[行][列]

 

習題:

        統計這個二維陣列中每一行的總分,平均分,最高分.

#include<stdio.h>
void main()
{
int score1[5][5] = {{1,2,3,4,5},
                    {21,22,23,24,25},
                    {31,32,33,34,35},
                    {100,101,3300,103,104},
                    {1000,1001,1005,1003,1004}};
}

求總分,平均分,最高分

//總分,平均分
int j = 0;
int num = 0,pjf = 0;
for(i=0;i<5;i++)
{
    for(j=0;j<5;j++)
        {
        num = num + score1[i][j];
        //printf("%d\t",num);
        }
        pjf = num/5;
        printf("每行總分:%d平均分:%d\n",num,pjf);
        num = 0;
}

//最高分,監哨值法
int a = 0,listen = 0;
int max = 0;
for(i=0;i<5;i++)
{
     for(j=0;j<4;j++)
     {
        listen = score1[i][j+1];
        if(a<listen)
        {
              a = listen;
        }
      }
      printf("\n最高分:%d",a);
      a = 0;
}

 

習題:

        判斷下列陣列是否是稱的

                斜對稱 :

                1     2     3     4

                2     2     7     8

                3     7     3     5

                4     8     5     4

#include<stdio.h>
int main()
{
    int score[4][4]={{1,2,3,4},
                     {2,2,7,8},
                     {3,7,3,5},
                     {4,8,5,4}};

     int i = 0,j = 0,d = 0;
     for(i=0;i<4;i++)
     {
          for(j=0;j<4;j++)
          {
               if(score[i][j] = score[j][i])
              {
                  d++;
              }
          }
     }
     if(d==16)
     {
            printf("是對稱的!");
     }
     else
     {
            printf("不是對稱的!");
     }
}

 

習題:

        求下列陣列所有元素的最大值,和次大值 .

#include<stdio.h>
void main()
{
int score1[5][5] = {{1,2,3,4,5},
                    {21,22,23,24,25},
                    {31,32,33,34,35},
                    {100,101,3300,103,104},
                    {1000,1001,1005,1003,1004}};
}

    求最大值:

int i = 0,j = 0;
int listen = 0,max = 0;
for(i=0;i<5;i++)
{
    listen = score[i][0];
    for(j=0;j<4;j++)
    {
        if(listen<score[i][j+1])
        {
             listen = score[i][j+1];
        }
    }
    printf("%d",listen);
}

    求次大值:

int i = 0,j = 0;
int listen = 0,b = 0,max = 0;
for(i=0;i<5;i++)
{
    for(j=0;j<4;j++)
    {      listen = score[i][0];
        if(listen<score[i][j+1])    //當listen=3300時,score[i][j+1]=34>3300的條件不成立,執行else
        {
            b = listen;                    //此時b=32
            lsiten = score[i][j+1];        //此時listen=3300
            printf("%d,%d\n",b,listen);    //列印b,listen的值
        }
        else
        {
            if(score[i][j+1]>b)
            {
                printf("%d,",b);    //此時b=32
                b = score[i][j+1];  //此時score[i][j+1]=34
                printf("%d\n",b);   //此時b=34
            }
        }
    }
}
printf("%d",b);

 

習題:

    按照以下方法求數字:

    從每行中選取最大數,並查詢最大數的最小數。

#include<stdio.h>
void main()
{
int score1[5][5] = {{1,2,3,4,5},
                    {21,22,23,24,25},
                    {31,32,33,34,35},
                    {100,101,3300,103,104},
                    {1000,1001,1005,1003,1004}};

int listen = 0,i = 0,j = 0;
int temp = 0;
for(i = 0;i<5;i++)
{
    listen = score[i][0];
    for(j=0;j<4;j++)
    {
        if(listen<score[i][j+1])
        {
            listen = score[i][j+1];
        }
    }
    if(i == 0)
    {
        temp = listen;
    }
    else if(temp>listen)
    {
        temp = listen;
    }
}
    printf("%d\n",temp);
}

 

習題:

       求鞍點:宣告一個4行5列的二維陣列,求出陣列中鞍點及其所在行與列.

      (鞍點:即該位置上的元素在該行中最大,在該列中最小。)

#include<stdio.h>
void main()
{
int score1[5][5] = {{1,2,3,4,5},
                    {21,22,23,24,25},
                    {31,32,33,34,35},
                    {100,101,3300,103,104},
                    {1000,1001,1005,1003,1004}};

int listen = 0,i = 0,j = 0;
int temp = 0;
for(i = 0;i<5;i++)
{
    listen = score[i][0];
    for(j=0;j<4;j++)
    {
        if(listen<score[i][j+1])
        {
            listen = score[i][j+1];
        }
    }
    if(i == 0)
    {
        temp = listen;
    }
    else if(temp>listen)
    {
        temp = listen;
    }
}
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            if(temp == score[i][j])
            printf("鞍點:%d第%d第%d列",temp,i+1,j+1);
        }
    }
}

 

習題:

        匹配演算法:KMP

        從字串匹配出子字串的位置:12a34123a56a12b3c1234abc3433 求123456

#include<stdio.h>
void main()
{
    char buf[]="123ab1123fa124abc1234abcd32";
    char sub[]="1234";
    //遍歷字串:
    int i = 0,x,y;
    while(buf[i]!='\0')
    {
        if(sub[0]==buf[i])//buf從第i個到i+4個字元是否相同
        {
            x = 1;
            //比較兩個字串是否相同:
            while(sub[x]==buf[i+x] && sub[x]!='\0')x++;
            if(4==x)
            {
                printf("存在%s的字串,它的位置是%d\n",sub,i+x);
                return ;
            }
        }
        //從下個字元再匹配
        i++;
    }
        printf("不存在%s\n",sub);
}

 

 

 

 

相關文章