《程序设计思维与实践》week12 — 作业题

A

题目描述

给出n个数,zjm想找出出现至少(n+1)/2次的数, 现在需要你帮忙找出这个数是多少?

Input

本题包含多组数据:
每组数据包含两行。
第一行一个数字N(1<=N<=999999) ,保证N为奇数。
第二行为N个用空格隔开的整数。
数据以EOF结束。

Output

对于每一组数据,你需要输出你找到的唯一的数。

Sample Input

5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample Output

3
5
1

解题思路

直接开了一个大小大于N的最大值的数组,每输入一个数,就将其在数组中对应位置的值加一,然后每次都和(N+1)/2相比较,如果满足大于等于的条件,就将这个数记录下来,然后输出即可;
注意:开始下一次输入的时候都要将数组进行初始化;

解题代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
using namespace std;

int N;
int shu[1000000];

int main () {
    while (cin>>N) {
        memset(shu, 0, sizeof(shu));
        int number; int ans = 0;
        for (int a = 0; a < N; a++) {
            cin>>number;
            shu[number]++;
            if (shu[number] >= (N+1)/2) ans = number;
        }
        cout<<ans<<endl;
    }
    return 0;
}

B

题目描述

zjm被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成。
zjm每次向上下前后左右移动一个单位需要一分钟,且zjm不能对角线移动。
空间的四周封闭。zjm的目标是走到空间的出口。
是否存在逃出生天的可能性?如果存在,则需要多少时间?

Input

输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度,R和C分别表示每层空间的行与列的大小。
随后L层,每层R行,每行C个字符。
每个字符表示空间的一个单元。’#‘表示不可通过单元,’.‘表示空白单元。
zjm的起始位置在’S’,出口为’E’。每层空间后都有一个空行。
L,R和C均为0时输入结束。

Output

每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。

如果无法逃生,则输出如下
Trapped!

Sample Input

3 4 5
S….
.###.
.##..
###.#

#####
#####
##.##
##…

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

解题思路

相当于把二维的迷宫题扩展到了三维,实际上还是一样的思路,bfs找最短路径,只不过换成了三维空间。套bfs的板子,稍加修改就可以了。

解题代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <utility>
using namespace std;

struct node {
    int a;
    int b;
    int c;
    
    node (int z, int y, int x) {a = z; b = y; c = x;}
};

int L, R, C;
int space[35][35][35];
bool Yspace[35][35][35];
//上下前后左右
int h[6] = {0, 0, 0, 0, -1, 1};
int z[6] = {0, 0, -1, 1, 0, 0};
int s[6] = {1, -1, 0, 0, 0, 0};
int bx, by, bz, ex, ey, ez;
queue<node> Q;

void bfs () {
    Yspace[bz][by][bx] = 1;
    while (!Q.empty()) Q.pop();
    
    node first(bz, by, bx);
    Q.push(first);
    while (!Q.empty()) {
        node next = Q.front();
        Q.pop();
        
        for (int l = 0; l < 6; l++) {
            int aa = next.a + s[l];
            int bb = next.b + z[l];
            int cc = next.c + h[l];
            if (aa <= 0 || aa > L) continue;
            if (bb <= 0 || bb > R) continue;
            if (cc <= 0 || cc > C) continue;
            
            if (space[aa][bb][cc] != 0 && Yspace[aa][bb][cc] == 0) {
                space[aa][bb][cc] = space[next.a][next.b][next.c] + 1;
                Yspace[aa][bb][cc] = 1;
                node newNode(aa, bb, cc);
                Q.push(newNode);
            }
        }
    }
}

int main () {
    while (cin>>L>>R>>C) {
        if (L == 0 && R == 0 && C == 0) return 0;
        //初始化 标记数组
        for (int a = 1; a <= L; a++) {
            for (int b = 1; b <= R; b++) {
                for (int c = 1; c <= C; c++) {
                    Yspace[a][b][c] = 0;
                }
            }
        }
        char x;
        //a-竖坐标 b-纵坐标 c-横坐标
        for (int a = 1; a <= L; a++) {
            for (int b = 1; b <= R; b++) {
                for (int c = 1; c <= C; c++) {
                    cin>>x;
                    if (x == 'S') {space[a][b][c] = 0; bz = a; by = b; bx = c;}
                    if (x == '#') space[a][b][c] = 0;
                    if (x == '.') space[a][b][c] = 1;
                    if (x == 'E') {space[a][b][c] = 1; ez = a; ey = b; ex = c;}
                }
            }
        }
        bfs();
        if (Yspace[ez][ey][ex] == 1) cout<<"Escaped in "<<space[ez][ey][ex]<<" minute(s)."<<endl;
        else cout<<"Trapped!"<<endl;
    }
}

C

题目描述

东东每个学期都会去寝室接受扫楼的任务,并清点每个寝室的人数。
每个寝室里面有ai个人(1<=i<=n)。从第i到第j个宿舍一共有sum(i,j)=a[i]+…+a[j]个人
这让宿管阿姨非常开心,并且让东东扫楼m次,每一次数第i到第j个宿舍sum(i,j)
问题是要找到sum(i1, j1) + … + sum(im,jm)的最大值。且ix <= iy <=jx和ix <= jy <=jx的情况是不被允许的。也就是说m段都不能相交。
注:1 ≤ i ≤ n ≤ 1e6 , -32768 ≤ ai ≤ 32767 人数可以为负数。。。。(1<=n<=1000000)

Input

输入m,输入n。后面跟着输入n个ai

Output

输出最大和

Sample Input

1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Output

6
8 

解题思路

用dp[ i ][ j ]代表前j个数中,以a[ j ]结尾,分成i组的最大和。
状态转移方程:dp[ i ][ j ] = max(max(dp[ i-1 ][ k ]) + a[ j ],dp[ i ][ j-1 ] + a[ j ]) 其中(i-1 <= k <= j-1)
因为是选择最大值,所以说第j个数是必须要被选上的,而对于第j个又有两种选择的方法:
(1)与最后一段合并,dp[ i ][ j ] = dp[ i ][ j-1 ] + a[ j ];
(2)成为新的一段,dp[ i ][ j ] = dp[ i-1 ][ k ] + a[ j ]; (i-1 < k <= j-1)
然后可以发现,对于k的那个循环其结果可以由上一次循环j的时候算出来,改进后,数组第一维就可以去掉了。

解题代码

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

const int minimum = -1e9;
int m, n;
int a[10000010], dp[1000010], pre[200000];

int main() {
    while(scanf("%d%d", &m, &n)!=EOF) {
        for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
        memset(dp, 0, sizeof(dp));
        memset(pre, 0, sizeof(pre));
        
        int temp = 0;
        for (int i = 1; i <= m; i++) {
            temp = minimum;
            for (int j = i; j <= n; j++) {
                dp[j] = max(pre[j-1], dp[j-1]) + a[j];
                pre[j-1] = temp;
                temp = max(temp,dp[j]);
            }
        }
        printf("%d\n",temp);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值