題目分析能力實戰訓練

一、掃雷(題目在代碼源程序裏)
1、 循環依次查看每個點周圍有的八個點當中,多少個是雷。
2、 判斷點爲雷,count加1
3、修改對應矩陣的值

/*
掃雷遊戲(難度係數:3)
(輸入文件:mine.txt,輸出文件:estdout.pc2)
   玩過掃雷遊的朋友都知道,該遊戲的目標是找出一個n*m矩陣內的所有的地雷,在本題中,你需要爲每一個單元格統計出它周圍地雷的個數,每個單元格最多有8個相鄰單元格,如下圖,4*4 的格子裏,用"*"表示雷,用"^" 表示沒有雷。
*^^^
^^^^
^*^^
^^^^
計算後的輸出結果爲:
   *100
   2210
   1*10
   1110
輸入數據:
文件內包含若干個矩陣,對於每一個矩陣,第一行包含兩個數M和N,分別表示該矩陣的行數和列數(0<N,M<100),接下來N行包含M個字符,就是該矩陣,用"*"表示地雷,用"^"表示空白。當N=M=0時,表示文件結束,不用處理該行,
輸出數據:
對於每一個矩陣,首先在單獨一行打印出矩陣序號:Field #X: 其中X是矩陣的編號,從1開始編號,接下來N行中,讀入的"^"用該位置周圍的地雷數目所代替,地雷處,仍用"*"表示。輸出相鄰的兩個矩陣之間,空一行。

樣例輸入:
Mine.txt:
4 4 
*^^^
^^^^
^*^^
^^^^
8 8
*^^^*^^^
*^****^^
^*^^**^^
^^**^**^
^*^^**^^
*^^^*^^^
^^**^**^
^*^^**^^
0 0
estdout.pc2:
Field #1:
*100
2210
1*10
1110

Field #2:
*324*310
*4****20
2*56**41
23**6**1
2*34**31
*334*531
23**5**1
1*33**31

*/
#include "stdio.h"
#define MAX_SIZE 100
void main()
{
    char a[MAX_SIZE][MAX_SIZE], b[MAX_SIZE][MAX_SIZE];
    int n, m;
    scanf("%d%d", &n, &m);

    for(int i=0; i<n; i++)
    {
        scanf("%s", &a[i]);
    }
    for(i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(a[i][j] == '*')
            {
                b[i][j] = '*';
            }
            else
            {
                int count = 0;
                if(i>0 && j>0 && (a[i-1][j-1] == '*'))
                    count++;
                if(i>0 && (a[i-1][j] == '*'))
                    count++;
                if(i>0 && j<m-1 && (a[i-1][j+1] == '*'))
                    count++;       
                if(j>0 && (a[i][j-1] == '*'))
                    count++;
                if( j<m-1 && a[i][j+1] == '*')
                    count++;
                if(j>0 && i<n-1 && (a[i+1][j-1] == '*'))
                    count++;
                if(i<n-1 && (a[i+1][j] == '*'))
                     count++;
                if(j<m-1 && i<n-1 && (a[i+1][j+1] == '*'))
                    count++;
                b[i][j] = '0' + count;
            }
        }
     }
     for(i=0; i<n; i++)
     {
          for(int j=0; j<m; j++)
          {
              printf("%c", b[i][j]);
          }
          printf("\n");
     }
}

二、二值圖像求最大面積
1、遞歸實現求每個點周圍四個方向上的點是否爲1,爲1面積加1,當前點置0,繼續判斷下面的點。
2、遞歸退出的條件:判斷當前的值爲0,則返回,否則需要繼續判斷它周圍的點;
3、初始化max值爲0,以後每求得一個面積和max的值相比較,比max的值大,則賦值給max。

/*【最大區域】
二值圖像是由黑白兩種像素組成的矩形點陣,圖像識別的一個操作是求出圖像中最大黑區域的面積。請設計一個程序完成二值圖像的這個操作。黑區域由黑像素組成,一個黑區域中的每個像素至少與該區域中的另一個像素相鄰,規定一個像素僅與其上、下、左、右的像素相鄰。兩個不同的黑區域沒有相鄰的像素。一個黑區域的面積是其所包含的像素的個數。
【輸入】輸入文件名"area.in"
輸入由多個測試例組成。每個測試例的第一行含兩個整數n和m, (1 <=n,m<=100), 分別表示二值圖像的行數與列數,後面緊跟着n行,每行含m個整數01,其中第i行表示圖像的第i行的m個像素,0表示白像素,1表示黑像素。同一行的相鄰兩個整數之間用一個空格隔開,兩個測試例之間用一個空行隔開,最後一個測試例之後隔一個空行,再接的一行含有兩個整數0,標誌輸入的結束。
【輸出】輸出文件名"estdout.pc2"
每個測試例對應一行輸出,含一個整數,表示相應的圖像中最大黑區域的面積。
程序運行後結果示例:
【樣例輸入】
5 6
0 1 1 0 0 1
1 1 0 1 0 1
0 1 0 0 1 0
0 0 0 1 1 1
1 0 1 1 1 0

4 6
0 1 1 0 0 1
1 1 1 1 0 1
0 1 0 1 1 0
0 0 0 1 1 1

0 0
【樣例輸出】
7
12
*/
#include<stdio.h>
int a[100][100];
int n, m, num;

int search(int x, int y)
{
   if(a[x][y] == 1)
   {
      num++;
      a[x][y]=0;
      if(x>0) search(x-1, y);
      if(x<n-1) search(x+1, y);
      if(y>0) search(x, y-1);
      if(y<m-1) search(x, y+1);
   }
   return 0;
}

main()
{
    int max = 0;

    scanf("%d%d",&n,&m);
    if(n==0 && m==0)
    {
        return 0;
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    for(i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            printf("%5d", a[i][j]);
        }
        printf("\n");
    }

    for( i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
           num = 0;
           search(i,j);            
           max = max<num ? num : max;
        }

    }

    printf("%d\n", max);
    return 0;
}

三、液晶顯示器問題
1、分析:所有的數字由七個筆畫組成,三條橫線,四條豎線,即只要能顯示數字8,其他的均可顯示。
2、爲每個數字建立一個模型。
3、顯示分三種情況,沒有筆畫的地方設置值爲0,顯示爲空格。顯示橫的地方設置爲值1,顯示爲-,顯示豎線的地方設置值爲2,顯示爲|。
4、將要顯示的多個數字先輸入到一個二維數組,最後將二維數組顯示出來。多個數字在輸入到數組的時候,每個數字佔得寬度爲:2+s,(根據寬度計算下一個數字在二維數組中的起始位置)長度爲2s+3。最後統一輸出。

/*
四、液晶顯示屏(難度係數:4)
(輸入文件:digit.txt,輸出文件:estdout.pc2)
現在要求你在文本文件中,模擬液晶顯示屏的方式顯示數據,只要求顯示0~9的數據.

輸入數據:
   第一行是一個數據的組數M,表示以下有M組數據.
   每一組數據包括兩個整數s和n,其中n>0 且n<99999999,s>=1 且s<=5,n是需要顯示的數據.
輸出數據:
   模擬液晶顯示器輸出指定的整數n,用s個"-"表示水平線,"|",表示堅線,每一個阿拉伯數字點用s+2列,和2s+3行.注意那些數字中的空白之處要填上空格,並且兩個數字之間有一個空列.每行數據之間,有一個空行

樣例輸入:
digit.txt
2
2  12345
3  67890

estdout.pc2


*/
#include "stdio.h"
#define ROW 14 //14:每個數字最多佔13行(爲了確保間距,行數加1)
#define COL 64 //8:每個數字最多佔7列(爲了確保間距,列數加1),共顯示八位
struct digit
{
     int stroke1;//上橫線
     int stroke2;//中間橫線
     int stroke3;//下橫線
     int stroke4;//左上線
     int stroke5;//右上線
     int stroke6;//左下線
     int stroke7;//右下線
}d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
int a[ROW][COL] = {0};
void Init_Digit()
{
     d0.stroke1 = 1;
     d0.stroke2 = 0;
     d0.stroke3 = 1;
     d0.stroke4 = 2;
     d0.stroke5 = 2;
     d0.stroke6 = 2;
     d0.stroke7 = 2;
     d1.stroke1 = 0;
     d1.stroke2 = 0;
     d1.stroke3 = 0;
     d1.stroke4 = 2;
     d1.stroke5 = 0;
     d1.stroke6 = 2;
     d1.stroke7 = 0;
     d2.stroke1 = 1;
     d2.stroke2 = 1;
     d2.stroke3 = 1;
     d2.stroke4 = 0;
     d2.stroke5 = 2;
     d2.stroke6 = 2;
     d2.stroke7 = 0;
     d3.stroke1 = 1;
     d3.stroke2 = 1;
     d3.stroke3 = 1;
     d3.stroke4 = 0;
     d3.stroke5 = 2;
     d3.stroke6 = 0;
     d3.stroke7 = 2;
     d4.stroke1 = 0;
     d4.stroke2 = 1;
     d4.stroke3 = 0;
     d4.stroke4 = 2;
     d4.stroke5 = 2;
     d4.stroke6 = 0;
     d4.stroke7 = 2;
     d5.stroke1 = 1;
     d5.stroke2 = 1;
     d5.stroke3 = 1;
     d5.stroke4 = 2;
     d5.stroke5 = 0;
     d5.stroke6 = 0;
     d5.stroke7 = 2;
     d6.stroke1 = 1;
     d6.stroke2 = 1;
     d6.stroke3 = 1;
     d6.stroke4 = 2;
     d6.stroke5 = 0;
     d6.stroke6 = 2;
     d6.stroke7 = 2;
     d7.stroke1 = 1;
     d7.stroke2 = 0;
     d7.stroke3 = 0;
     d7.stroke4 = 0;
     d7.stroke5 = 2;
     d7.stroke6 = 0;
     d7.stroke7 = 2;
     d8.stroke1 = 1;
     d8.stroke2 = 1;
     d8.stroke3 = 1;
     d8.stroke4 = 2;
     d8.stroke5 = 2;
     d8.stroke6 = 2;
     d8.stroke7 = 2;
     d9.stroke1 = 1;
     d9.stroke2 = 1;
     d9.stroke3 = 1;
     d9.stroke4 = 2;
     d9.stroke5 = 2;
     d9.stroke6 = 0;
     d9.stroke7 = 2;
}
void Print_Digit(digit d, int s, int n)
{
    int j;
    j = (s+3)*(n-1);
     if(d.stroke1)
     {
          for(int count = 0; count < s; count++)
          {
              a[0][j+1+count] = 1;
          }
     }
     if(d.stroke2)
     {
          for(int count = 0; count < s; count++)
          {
              a[1+s][j+1+count] = 1;
          }
     }
     if(d.stroke3)
     {
          for(int count = 0; count < s; count++)
          {
              a[2*s+2][j+1+count] = 1;
          }
     }
     if(d.stroke4)
     {
          for(int count = 0; count < s; count++)
          {
              a[1+count][j] = 2;
          }
     }
     if(d.stroke5)
     {
          for(int count = 0; count < s; count++)
          {
              a[1+count][j+s+1] = 2;
          }
     }
     if(d.stroke6)
     {
          for(int count = 0; count < s; count++)
          {
              a[2+s+count][j] = 2;
          }
     }
     if(d.stroke7)
     {
          for(int count = 0; count < s; count++)
          {
              a[2+s+count][j+s+1] = 2;
          }
     }
}
void main()
{
     int s, n, k = 0, i = 0, j = 0, num = 0;
     Init_Digit();
     scanf("%d%d", &s, &n); 
     while(n>0)
     {
          num++;
          k = n;
          while(k>=10)
          {
              k = k/10;
          }
          switch(k)
          {
              case 0:
               {
                    Print_Digit(d0, s, num);
                    break;
               }
               case 1:
               {
                    Print_Digit(d1, s, num);
                    break;
               }
               case 2:
               {
                    Print_Digit(d2, s, num);
                    break;               
               }
               case 3:
               { 
                    Print_Digit(d3, s, num);
                    break;
               }
               case 4:
               {
                    Print_Digit(d4, s, num);
                    break;
               }
               case 5:
               {
                    Print_Digit(d5, s, num);
                    break;
               }
               case 6:
               {
                    Print_Digit(d6, s, num);
                    break;
               }
               case 7:
               {
                    Print_Digit(d7, s, num);
                    break;
               }
               case 8:
               {
                    Print_Digit(d8, s, num);
                    break;
               }
               case 9:
               {
                    Print_Digit(d9, s, num);
                    break;
               }
          }
          if(n>=10000000)
              n = n%10000000;
          else if(n>=1000000)
              n = n%1000000;
          else if(n>=100000)
              n = n%100000;
          else if(n>=10000)
              n = n%10000;
          else if(n>=1000)
              n = n%1000;
          else if(n>=100)
              n = n%100;
          else if(n>=10)
              n = n%10;
          else
              break;
     }

     for(i = 0; i<ROW; i++)
     {
          for(j = 0; j<COL; j++)
          {
               if(a[i][j]==1)
                   printf("-");
               else if(a[i][j]==0)
                   printf(" ");
               else if(a[i][j]==2)
                   printf("|");
          }
          printf("\n");
     }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章