1.23总结

题目描述

任何一个大于 11 的自然数 n,总可以拆分成若干个小于 n 的自然数之和。现在给你一个数 n, 要求你求出 n 的拆分成一些数字的和。每个拆分后的序列中的数字从小到大排序。然后你需要输出这些序列,其中字典序小的序列需要优先输出。

输入格式

输入:待拆分的自然数 n。

输出格式

输出:若干数的加法式子。

输入输出样例

输入 

7

输出 

1+1+1+1+1+1+1
1+1+1+1+1+2
1+1+1+1+3
1+1+1+2+2
1+1+1+4
1+1+2+3
1+1+5
1+2+2+2
1+2+4
1+3+3
1+6
2+2+3
2+5
3+4

说明/提示

数据保证,2≤n≤8。

分析:

这题使用dfs,就拿样例7来分析,最容易就是拆分成1+1+1+1+1+1+1,这是一定最长的拆分方法,直接输出这一串拆分后的数,然后我们回到上一层,,也就是让第六个一加一,这样就变成1+1+1+1+1+2,满足条件,直接输出,然后再回到上一层,使第五个1加一,调用dfs,而dfs的调用时根据上一层的数来决定,所以并不会出现1+1+1+1+2+1的情况。

代码如下:

#include <stdio.h>
int n,b[100];
void pt(int a)//该函数用来输出结果
{
    for(int i=0;i<a;i++)
    {
        if(i==0)
        printf("%d",b[i]);
        else
        printf("+%d",b[i]);
    }
    printf("\n");
}
void dfs(int x,int y,int z)//x表示拆分数,y表示拆分数之和,z表示拆数个数
{
    if(x==n)//防止直接输出n
    return;
    if(y==n){
    pt(z);
    return;
    }
    for(int i=x;i<=n-y;i++)
    {
        b[z]=i;
        dfs(i,y+i,z+1);
    }
}
int main()
{
    scanf("%d",&n);
    dfs(1,0,0);
    return 0;
} 

题目描述

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. Given a diagram of Farmer John's field, determine how many ponds he has.

输入格式

Line 1: Two space-separated integers: N and M * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

输出格式

Line 1: The number of ponds in Farmer John's field.

题意翻译

由于近期的降雨,雨水汇集在农民约翰的田地不同的地方。我们用一个 )N×M(1≤N≤100,1≤M≤100) 的网格图表示。每个网格中有水(W) 或是旱地(.)。一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑。约翰想弄清楚他的田地已经形成了多少水坑。给出约翰田地的示意图,确定当中有多少水坑。

输入第 11 行:两个空格隔开的整数:N 和 M。

第 22 行到第 N+1 行:每行 M 个字符,每个字符是 W 或 .,它们表示网格图中的一排。字符之间没有空格。

输出一行,表示水坑的数量。

输入输出样例

输入

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

输出 

3

说明/提示

OUTPUT DETAILS: There are three ponds: one in the upper left, one in the lower left, and one along the right side.

 分析:

,这题可以使用的方式dfs寻找连通块,首先找到第一'W'的网格,然后使水坑数加一,然后将这个网格变成'.'进行标记并从这一点开始试探,可一向四周八个方向试探,只要该网格没有超出田地,并且不是'.',一直试探直到无路可走,结束第一次dfs调用,继续找’W'网格,再重复上述过程,直到结束。

代码如下:

#include <stdio.h>
char a[101][101];
int n,m;
void dfs(int x,int y)
{
    int nx[]={0,1,1,1,0,-1,-1,-1};
    int ny[]={1,1,0,-1,-1,-1,0,1};
    int tx,ty;
    a[x][y]='.';
    for(int i=0;i<8;i++)
    {
        tx=x+nx[i];
        ty=y+ny[i];
        if(tx<0||tx>=n||ty<0||ty>=m||a[tx][ty]=='.')
        continue;
        a[tx][ty]='.';
        dfs(tx,ty);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%s",a[i]);
    }
    int s=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
        if(a[i][j]=='W')
        {
            s++;
            dfs(i,j);
        }
        }
    }   
    printf("%d\n",s);
    return 0;
}

  • 34
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值