1.
Walmart彩票
【问题描述】
Walmart推出了一项彩票。规则是:
(1) 每张彩票上印有7个互不相同的号码,号码的取值范围为1~33。
(2) 兑奖前会公布一个中将号码,由7个各不相同的号码构成。
(3) 共设置7个奖项,特等奖和一等奖至六等奖。对奖规则如下:
特等奖:彩票上的7个号码出现在中将号码中。
一等奖:彩票上有6个号码出现在中将号码中。
二等奖:彩票上有5个号码出现在中将号码中。
三等奖:彩票上有4个号码出现在中将号码中。
四等奖:彩票上有3个号码出现在中将号码中。
五等奖:彩票上有2个号码出现在中将号码中。
六等奖:彩票上有1个号码出现在中将号码中。
注:兑奖时并不考虑彩票上的号码和中将号码中的各个号码出现的位置。例如,中将号码为23 31 1 14 19 17 18,则彩票12 8 9 23 1 16 7由于其中有两个号码(23和1)出现在中号码中,所以该彩票中了五等奖。
现已知中奖号码和顾客买的若干张彩票的号码,请你写一个程序帮助Walmart判断顾客买的彩票的中奖情况。
【输入】
文件的第一行只有一个自然数n,表示顾客买的彩票张数;第二行存放了7个介于1和33之间的自然数,表示中奖号码;在随后的n行中每行都有7个介于1和33之间的自然数,分别表示顾客所买的n张彩票。
【输出】
依次输出顾客所买的彩票的中奖情况(中奖的张数),首先输出特等奖的中奖张数,然后依次输出一等奖至六等奖的中奖张数。
【输入输出样例】
lottery.in lottery.out
2
23 1 11 14 19 17 18
12 8 9 23 1 16 7
11 7 10 21 2 9 31 0 0 0 0 0 1 1
【数据范围限制】
30%的数据:n<=100。
70%的数据:n<=1000。
100%的数据:n<=100000。
我的做法是对每个号码串进行排序,进行比对时使用两个指针扫描中奖串和待比较串,用cnt数组统计。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
freopen("lottery.in","r",stdin);
freopen("lottery.out","w",stdout);
int n,aim[10],t[10];
int cnt[10];
memset(cnt, 0, sizeof(cnt));
scanf("%d",&n);
for (int i = 0; i < 7; i ++) {
scanf("%d",&aim[i]);
}
sort(aim, aim + 7);
int i, j, sum;
for (int k = 1; k <= n; k ++) {
for (int i = 0; i < 7; i ++) {
scanf("%d",&t[i]);
}
sort(t, t + 7);
i = j = sum = 0;
while (i < 7 && j < 7) {
if (aim[i] == t[j]) {
++ sum;
++ i; ++ j;
} else if (aim[i] > t[j]) {
++ j;
} else {
++ i;
}
}
++ cnt[sum];
}
printf("%d",cnt[7]);
for (int i = 6; i >= 1; -- i) {
printf(" %d",cnt[i]);
}
printf("\n");
fclose(stdin);
fclose(stdout);
return 0;
}
2.
捉 牛
【问题描述】
农夫约翰(FJ)知道逃跑的奶牛位置,要尽快把她捉回。FJ从数轴的N (0 <= N <=100,000)点出发,奶牛所在的位置是同一数轴上的K点(0 <= K <=100,000)。FJ有两种行动方式:走路和瞬移。
* 走路: FJ 能直接从X点到X-1 或 X+1,用时1分钟。
* 瞬移: FJ 能直接从X 点到 2*X,,用时1分钟。
假设奶牛不知道做什么,固定不动,FJ至少要用多少分钟捉到她?
【输入】
一行,空格分隔的两个整数 N 和 K 。
【输出】
一行,FJ捉到逃跑的奶牛用时最少的分钟数。
【输入输出样例】
catchcow.in
5 17
catchcow.out
4
【样例解释】
FJ捉到奶牛的最快方式是按路线5-10-9-18-17,用时4分钟。
同poj3728 bfs
http://blog.csdn.net/jlu_nnbs/article/details/67634513
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX = 100005;
int n,k;
bool visit[MAX];
int step[MAX];
int mov[] = {1, -1};
int bfs(int x)
{
if (n >= k) {
return n - k;
}
queue<int> q;
q.push(x);
visit[x] = 1;
while (!q.empty()) {
x = q.front(); q.pop();
for (int i = 0; i < 2; i ++) {
int xx = x + mov[i];
if (xx == k) {
return step[x] + 1;
}
if (xx >= 0 && xx <= 100000 && !visit[xx]) {
q.push(xx);
visit[xx] = 1;
step[xx] = step[x] + 1;
}
}
int xx = x * 2;
if (xx == k) {
return step[x] + 1;
}
if (xx <= 100000 && !visit[xx]) {
q.push(xx);
visit[xx] = 1;
step[xx] = step[x] + 1;
}
}
}
int main()
{
freopen("catchcow.in","r",stdin);
freopen("catchcow.out","w",stdout);
while (~scanf("%d%d",&n,&k)) {
memset(visit,0,sizeof(visit));
memset(step,0,sizeof(step));
printf("%d\n",bfs(n));
}
fclose(stdin);
fclose(stdout);
}