[蓝桥杯2018初赛]乘积尾零与全球变暖

乘积尾零

如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?
 

5650 4542 3554 473 946 4114 3871 9073 90 4329 
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594 
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899 
1486 5722 3135 1170 4014 5510 5120 729 2880 9019 
2049 698 4582 4346 4427 646 9742 7340 1230 7683 
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649 
6701 6645 1671 5978 2704 9926 295 3125 3878 6785 
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915 
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074 
689 5510 8243 6114 337 4096 8199 7313 3685 211 
#include <stdio.h>
int main(void)
{
    int a[100] = {5650,4542,3554,473,946,4114,3871,9073,90,4329,2758,7949,6113,5659,5245,7432,3051,4434,6704,3594,9937,1173,6866,3397,4759,7557,3070,2287,1453,9899,1486,5722,3135,1170,4014,5510,5120,729,2880,9019,2049,698,4582,4346,4427,646,9742,7340,1230,7683,5693,7015,6887,7381,4172,4341,2909,2027,7355,5649,6701,6645,1671,5978,2704,9926,295,3125,3878,6785,2066,4247,4800,1578,6652,4616,1113,6205,3264,2915,3966,5291,2904,1285,2193,1428,2265,8730,9436,7074,689,5510,8243,6114,337,4096,8199,7313,3685,211};
    int b[2] = {0}, t;
    
    for(int i = 0; i < 100; i ++)
    {
        t = a[i];
        for(; !(t & 1); t /= 2)
            b[0] ++;
        for(; t % 5 == 0; t /= 5)
            b[1] ++;
    }
    
    if(b[0] > b[1]) printf("%d",b[1]);
    else printf("%d",b[0]);
    
    return 0;
}

全球变暖

你有一张某海域NxN像素的照片,"."表示海洋、"#"表示陆地,如下所示:

.......
.##....
.##....
....##.
..####.
...###.
.......

其中"上下左右"四个方向上连在一起的一片陆地组成一座岛屿。例如上图就有2座岛屿。  
由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。
具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。  
例如上图中的海域未来会变成如下样子:

.......
.......
.......
.......
....#..
.......
.......

请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。

输入格式

第一行包含一个整数N。  (1 <= N <= 1000)  
以下N行N列代表一张海域照片。  
照片保证第1行、第1列、第N行、第N列的像素都是海洋。  

输出格式

一个整数表示答案。

输入样例

7 
.......
.##....
.##....
....##.
..####.
...###.
.......  

输出样例

1
#include <iostream>
#include <bits/stdc++.h>
#define x first
#define y second
 
using namespace std;
 
const int N = 1000 + 10;
 
int n;
typedef pair<int,int> PII;
PII q[N * N];
bool st[N][N];
char g[N][N];
 
int dx[4] = {-1, 0, 1, 0},dy[4] = {0, 1, 0, -1};
 
void bfs(int sx, int sy, int &tl, int &bd)
{
 
    int tt = 0,hh = 0;
 
    st[sx][sy] = true;
    q[0] = {sx, sy};
 
    while(hh <= tt)
    {
        PII t = q[hh ++];
        tl ++;
        bool is_bd = false;
 
        for(int i = 0; i < 4; i ++)
        {
            int x = t.x + dx[i],y = t.y + dy[i];
            if(x < 0 || x >= n || y < 0 || y >= n || st[x][y]) continue;
            if(g[x][y] == '.')
            {
                is_bd = true;
                continue;
            }
 
            st[x][y] = true;
            q[++ tt] = {x, y};
        }
 
        if(is_bd) bd++;
    }
 
}
 
int main(){
 
   cin >> n;
   
   for(int i = 0;i < n;i++) cin >> g[i];
 
   int cnt  = 0;
 
   for(int i = 0;i < n;i++)
    for(int j = 0;j < n;j++)
    {
        if(!st[i][j] && g[i][j] == '#')
        {
            int tl = 0,bd = 0;
 
            bfs(i, j, tl, bd);
 
            if(tl == bd) cnt ++;
        }
    }
 
    cout << cnt << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aasd23/article/details/124946047