Post by huangno1 on Feb 25, 2017 7:55:51 GMT -8
程式說明
大家來玩玩賓果(BINGO)遊戲吧,為了精簡賓果遊戲,現在改變了一些規則的玩法,我們隨機給予你一個1~25的任意數字排列,請同學先將這25個數字放入bingo陣列(5×5數字格盤)中,我們會再給予你一連串的15個不同的數字(1~25之間),請您檢查會有幾條連線呢?這個遊戲規定有3條連線就bingo囉! 請想想看如何檢查輸入的數字是否為連成一線呢?
處理說明:
檢查是否有連線的方法有很多種哦,請想想如果是你,你會用什麼方法處理呢?本範例舉例:我們先假設將所有應檢查的輸入的數字輸入後,檢查bingo陣列所有元素,請將數字所在的陣列元素值改成「0」,最後再檢查直線、橫線及交叉線是否者總和為0代表已連線哦。
輸入說明:(每行中各項資料皆以空白相間隔)
第1~5列:每列連續輸入任意1~25的5個數字,依序由上到下各列,每列由左而右的順序擺發於bing陣列(5×5數字格盤)中。
第二行:連續輸入任意1~25之間的15個不同的數字(每個數字只出現一次)
輸出說明:顯示bing的5×5數字格盤最後結果(為求美觀,每個數字皆以2位數出現(小於10者10位數補0)+1格空白(space),最後要顯示出現多少條連線,當連線數大於等於3條時,則出現「BINGO!」,反之「GAME LOST!」字。
輸入範例1:
09 21 06 20 15
17 01 12 04 16
23 13 22 11 03
07 05 02 10 24
18 08 25 14 19
11 24 10 14 07 05 02 18 01 09 20 15 04 22 06
輸出範例1:
+---+---+---+---+---+
| 0 |21 | 0 | 0 | 0 |
+---+---+---+---+---+
|17 | 0 |12 | 0 |16 |
+---+---+---+---+---+
|23 |13 | 0 | 0 | 3 |
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| 0 | 8 |25 | 0 |19 |
+---+---+---+---+---+
連線數=3,BINGO!
輸入範例2:
4 19 14 16 3
11 5 18 2 22
25 8 1 23 20
21 9 6 13 12
10 15 24 7 17
4 25 10 19 8 14 18 1 6 24 16 13 3 20 17
輸入範例2:
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
|11 | 5 | 0 | 2 |22 |
+---+---+---+---+---+
| 0 | 0 | 0 |23 | 0 |
+---+---+---+---+---+
|21 | 9 | 0 | 0 |12 |
+---+---+---+---+---+
| 0 |15 | 0 | 7 | 0 |
+---+---+---+---+---+
連線數=2,GAME LOST!
輸入範例3:
4 19 14 16 3
11 5 18 2 22
25 8 1 23 20
21 9 6 13 12
10 15 24 7 17
4 10 19 5 9 14 18 1 6 24 16 2 13 3 17
輸入範例3:
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
|11 | 0 | 0 | 0 |22 |
+---+---+---+---+---+
|25 | 8 | 0 |23 |20 |
+---+---+---+---+---+
|21 | 0 | 0 | 0 |12 |
+---+---+---+---+---+
| 0 |15 | 0 | 7 | 0 |
+---+---+---+---+---+
連線數=4,BINGO!
題目敘述
原題目雖然列印時不足兩位數的要補 0,但從測試資料來看,應不需作此動作。
#include <stdio.h>
void f ( int num ) // num = 5
{
for ( int i = 0 ; i < num ; i ++ )
printf ( "+---" ) ;
printf ( "+\n" ) ;
}
int main ( )
{
const int BIN = 5 ;
int B [ BIN ] [ BIN ] = { 0 } , ans , i , j ;
for ( i = 0 ; i < BIN ; i ++ )
{
for ( j = 0 ; j < BIN ; j ++ )
scanf ( " %d" , &B [ i ] [ j ] ) ;
}
for ( i = 0 ; i < 15 ; i ++ )
{
scanf ( " %d" , &ans ) ;
for ( int i = 0 ; i < BIN ; i ++ )
{
for ( int j = 0 ; j < BIN ; j ++ )
{
if ( ans == B [ i ] [ j ] ) B [ i ] [ j ] = 0 ;
}
}
}
for ( i = 0 ; i < BIN ; i ++ )
{
f ( 5 ) ;
for ( j = 0 ; j < BIN ; j ++ )
{
if ( B [ i ] [ j ] == 0 )
printf ( "| 0 " ) ;
else
printf ( "|%2d " , B [ i ] [ j ] ) ;
}
printf ( "|\n" ) ;
}
f ( 5 ) ;
int s = 0 , k = 0;
//橫
for ( i = 0 ; i < BIN ; i ++ )
{
for ( j = 0 ; j < BIN ; j ++ )
{
s += B [ i ] [ j ] ;
}
if ( s == 0 )
k += 1 ;
s = 0 ;
}
//縱
for ( j = 0 ; j < BIN ; j ++ )
{
for ( i = 0 ; i < BIN ; i ++ )
s += B [ i ] [ j ] ;
if ( s == 0 )
k += 1 ;
s = 0 ;
}
//斜
for ( i = 0 ; i < BIN ; i ++ )
s += B [ i ] [ i ] ;
if ( s == 0 )
k += 1 ;
s = 0 ;
for ( i = BIN - 1 ; i >= 0 ; i -- )
{
j = BIN - ( i + 1 ) ;
s += B [ i ] [ j ] ;
}
if ( s == 0 )
k += 1 ;
s = 0 ;
if ( k >= 3 )
printf ( "連線數=%d,BINGO!\n" , k ) ;
else
printf ( "連線數=%d,GAME LOST!\n" , k ) ;
return 0 ;
}
大家來玩玩賓果(BINGO)遊戲吧,為了精簡賓果遊戲,現在改變了一些規則的玩法,我們隨機給予你一個1~25的任意數字排列,請同學先將這25個數字放入bingo陣列(5×5數字格盤)中,我們會再給予你一連串的15個不同的數字(1~25之間),請您檢查會有幾條連線呢?這個遊戲規定有3條連線就bingo囉! 請想想看如何檢查輸入的數字是否為連成一線呢?
處理說明:
檢查是否有連線的方法有很多種哦,請想想如果是你,你會用什麼方法處理呢?本範例舉例:我們先假設將所有應檢查的輸入的數字輸入後,檢查bingo陣列所有元素,請將數字所在的陣列元素值改成「0」,最後再檢查直線、橫線及交叉線是否者總和為0代表已連線哦。
輸入說明:(每行中各項資料皆以空白相間隔)
第1~5列:每列連續輸入任意1~25的5個數字,依序由上到下各列,每列由左而右的順序擺發於bing陣列(5×5數字格盤)中。
第二行:連續輸入任意1~25之間的15個不同的數字(每個數字只出現一次)
輸出說明:顯示bing的5×5數字格盤最後結果(為求美觀,每個數字皆以2位數出現(小於10者10位數補0)+1格空白(space),最後要顯示出現多少條連線,當連線數大於等於3條時,則出現「BINGO!」,反之「GAME LOST!」字。
輸入範例1:
09 21 06 20 15
17 01 12 04 16
23 13 22 11 03
07 05 02 10 24
18 08 25 14 19
11 24 10 14 07 05 02 18 01 09 20 15 04 22 06
輸出範例1:
+---+---+---+---+---+
| 0 |21 | 0 | 0 | 0 |
+---+---+---+---+---+
|17 | 0 |12 | 0 |16 |
+---+---+---+---+---+
|23 |13 | 0 | 0 | 3 |
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
| 0 | 8 |25 | 0 |19 |
+---+---+---+---+---+
連線數=3,BINGO!
輸入範例2:
4 19 14 16 3
11 5 18 2 22
25 8 1 23 20
21 9 6 13 12
10 15 24 7 17
4 25 10 19 8 14 18 1 6 24 16 13 3 20 17
輸入範例2:
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
|11 | 5 | 0 | 2 |22 |
+---+---+---+---+---+
| 0 | 0 | 0 |23 | 0 |
+---+---+---+---+---+
|21 | 9 | 0 | 0 |12 |
+---+---+---+---+---+
| 0 |15 | 0 | 7 | 0 |
+---+---+---+---+---+
連線數=2,GAME LOST!
輸入範例3:
4 19 14 16 3
11 5 18 2 22
25 8 1 23 20
21 9 6 13 12
10 15 24 7 17
4 10 19 5 9 14 18 1 6 24 16 2 13 3 17
輸入範例3:
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
|11 | 0 | 0 | 0 |22 |
+---+---+---+---+---+
|25 | 8 | 0 |23 |20 |
+---+---+---+---+---+
|21 | 0 | 0 | 0 |12 |
+---+---+---+---+---+
| 0 |15 | 0 | 7 | 0 |
+---+---+---+---+---+
連線數=4,BINGO!
題目敘述
原題目雖然列印時不足兩位數的要補 0,但從測試資料來看,應不需作此動作。
#include <stdio.h>
void f ( int num ) // num = 5
{
for ( int i = 0 ; i < num ; i ++ )
printf ( "+---" ) ;
printf ( "+\n" ) ;
}
int main ( )
{
const int BIN = 5 ;
int B [ BIN ] [ BIN ] = { 0 } , ans , i , j ;
for ( i = 0 ; i < BIN ; i ++ )
{
for ( j = 0 ; j < BIN ; j ++ )
scanf ( " %d" , &B [ i ] [ j ] ) ;
}
for ( i = 0 ; i < 15 ; i ++ )
{
scanf ( " %d" , &ans ) ;
for ( int i = 0 ; i < BIN ; i ++ )
{
for ( int j = 0 ; j < BIN ; j ++ )
{
if ( ans == B [ i ] [ j ] ) B [ i ] [ j ] = 0 ;
}
}
}
for ( i = 0 ; i < BIN ; i ++ )
{
f ( 5 ) ;
for ( j = 0 ; j < BIN ; j ++ )
{
if ( B [ i ] [ j ] == 0 )
printf ( "| 0 " ) ;
else
printf ( "|%2d " , B [ i ] [ j ] ) ;
}
printf ( "|\n" ) ;
}
f ( 5 ) ;
int s = 0 , k = 0;
//橫
for ( i = 0 ; i < BIN ; i ++ )
{
for ( j = 0 ; j < BIN ; j ++ )
{
s += B [ i ] [ j ] ;
}
if ( s == 0 )
k += 1 ;
s = 0 ;
}
//縱
for ( j = 0 ; j < BIN ; j ++ )
{
for ( i = 0 ; i < BIN ; i ++ )
s += B [ i ] [ j ] ;
if ( s == 0 )
k += 1 ;
s = 0 ;
}
//斜
for ( i = 0 ; i < BIN ; i ++ )
s += B [ i ] [ i ] ;
if ( s == 0 )
k += 1 ;
s = 0 ;
for ( i = BIN - 1 ; i >= 0 ; i -- )
{
j = BIN - ( i + 1 ) ;
s += B [ i ] [ j ] ;
}
if ( s == 0 )
k += 1 ;
s = 0 ;
if ( k >= 3 )
printf ( "連線數=%d,BINGO!\n" , k ) ;
else
printf ( "連線數=%d,GAME LOST!\n" , k ) ;
return 0 ;
}