2389: 【入门】【P2550】彩票摇奖

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:36 解决:14

题目描述

为了丰富人民群众的生活、支持某些社会公益事业,北塔市设置了一项彩票。该彩票的规则是:

  1. 每张彩票上印有 lns="http://www.w3.org/1998/Math/MathML">7 个各不相同的号码,且这些号码的取值范围为 lns="http://www.w3.org/1998/Math/MathML">133
  2. 每次在兑奖前都会公布一个由七个各不相同的号码构成的中奖号码。
  3. 共设置 lns="http://www.w3.org/1998/Math/MathML">7 个奖项,特等奖和一等奖至六等奖。

兑奖规则如下:

  • 特等奖:要求彩票上 lns="http://www.w3.org/1998/Math/MathML">7 个号码都出现在中奖号码中。
  • 一等奖:要求彩票上有 lns="http://www.w3.org/1998/Math/MathML">6 个号码出现在中奖号码中。
  • 二等奖:要求彩票上有 lns="http://www.w3.org/1998/Math/MathML">5 个号码出现在中奖号码中。
  • 三等奖:要求彩票上有 lns="http://www.w3.org/1998/Math/MathML">4 个号码出现在中奖号码中。
  • 四等奖:要求彩票上有 lns="http://www.w3.org/1998/Math/MathML">3 个号码出现在中奖号码中。
  • 五等奖:要求彩票上有 lns="http://www.w3.org/1998/Math/MathML">2 个号码出现在中奖号码中。
  • 六等奖:要求彩票上有 lns="http://www.w3.org/1998/Math/MathML">1 个号码出现在中奖号码中。

注:兑奖时并不考虑彩票上的号码和中奖号码中的各个号码出现的位置。例如,中奖号码为 lns="http://www.w3.org/1998/Math/MathML">23 31 1 14 19 17 18,则彩票 lns="http://www.w3.org/1998/Math/MathML">12 8 9 23 1 16 7 由于其中有两个号码(lns="http://www.w3.org/1998/Math/MathML">23 和 lns="http://www.w3.org/1998/Math/MathML">1)出现在中奖号码中,所以该彩票中了五等奖。

现已知中奖号码和小明买的若干张彩票的号码,请你写一个程序帮助小明判断他买的彩票的中奖情况。

输入

输入的第一行只有一个自然数 lns="http://www.w3.org/1998/Math/MathML">,表示小明买的彩票张数;

第二行存放了 lns="http://www.w3.org/1998/Math/MathML">7 个介于 lns="http://www.w3.org/1998/Math/MathML">1 和 lns="http://www.w3.org/1998/Math/MathML">33 之间的自然数,表示中奖号码;

在随后的 lns="http://www.w3.org/1998/Math/MathML"> 行中每行都有 lns="http://www.w3.org/1998/Math/MathML">7 个介于 lns="http://www.w3.org/1998/Math/MathML">1 和 lns="http://www.w3.org/1998/Math/MathML">33 之间的自然数,分别表示小明所买的 lns="http://www.w3.org/1998/Math/MathML"> 张彩票。

输出

依次输出小明所买的彩票的中奖情况(中奖的张数),首先输出特等奖的中奖张数,然后依次输出一等奖至六等奖的中奖张数。

样例输入 复制

2
23 31 1 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

提示

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">1<1000

分析:

首先使用数组 a 读入7个中奖号码。外层循环是枚举每一张彩票,用 ans 变量来统计这张彩票上的数字是否和某个中奖号码一致,将所有中奖号码对比一下是否相等,如果发现相等,ans 就增加1。统计完一张后就知道彩票有几个号码和中奖号码相等了,这时就可以使用另外一个数组 num 记录有几个数字和中奖号码相同。最后依次输出中了7个数字、6个数字……一直到1个数字的次数,注意输出的顺序。

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n,a[10],num[10]={0};
    cin>>n;
    for (int i=1;i<=7;i++)
        cin>>a[i]; //创建一个数组存下中奖号码
	while (n--) {  //用for也可以 
		int ans=0;
		for (int i=1;i<=7;i++) {
			int x;
			cin>>x;
			for (int j=1;j<=7;j++)  //每次比较每个号码是否为中奖号码
			    if (a[j]==x)
				    ans++; 
		}
		num[ans]++;
	} 
	for (int i=7;i;i--)
	    cout<<num[i]<<(i==1?'\n':' '); 
	    /*
	    输出答案,注意最后一个要加换行而不是空格 
		*/
	return 0;
}