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
思路:
将每个数存储到给定的数组中,碰到就相应的加一,然后一遍循环找出满足条件的即可。
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m;
int sum[1000005];
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
cin >> m;
sum[m]++;
}
for(int i=0;i<=1000000;i++)
{
if(sum[i]>=(n+1)/2)
{
cout << i << endl;
break;
}
}
}
}
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 Outpu
Escaped in 11 minute(s).
Trapped!
思路:
一道三维立体的bfs题目,设立一个三维visit数组用于剪枝,然后在节点中加入坐标信息以及距离起点已经走过的距离,
等待bfs遍历结束,查看能否经过终点,返回true or false 。
代码:
#include<iostream>
#include<string.h>
#include<queue>
#include<cstdio>
using namespace std;
bool vis[31][31][31];
int l,r,cc,s1,s2,s3,e1,e2,e3;
string x;
struct node
{
int a,b,c,s;
node(int x,int y,int z,int w){
a=x;
b=y;
c=z;
s=w;
}
};
bool bfs(int a,int b,int c,int s)
{
vis[a][b][c]=1;
queue<node> q;
node now(a,b,c,s);
q.push(now);
while(!q.empty())
{
now=q.front();q.pop();
a=now.a;
b=now.b;
c=now.c;
s=now.s;
if(a==e1&&b==e2&&c==e3)
{
printf("Escaped in %d minute(s).\n",s);
return true;
}
s++;
if(a+1<=l&&!vis[a+1][b][c])
{
node now(a+1,b,c,s);
q.push(now);
vis[a+1][b][c]=1;
}