【模拟】Ingenious Lottery Tickets

本文介绍了一个有趣的算法挑战,目标是通过分析过去的彩票中奖号码,找出最有可能再次出现的六个数字。文章详细描述了如何记录每个数字的出现频率,并通过自定义排序策略选出最佳数字,特别强调了数字7的优先级。

题目描述

Your friend Superstitious Stanley is always getting himself into trouble. This time, in his Super Lotto Pick and Choose plan, he wants to get rich quick by choosing the right numbers to win the lottery. In this lottery, entries consist of six distinct integers from 1 to 49, which are written in increasing order. Stanley has compiled a list of winning entries from the last n days, and is going to use it to pick his winning numbers. 
In particular, Stanley will choose the six numbers that appeared the most often. When Stanley is breaking ties, he prefers smaller numbers, except that he prefers seven to every other number. What is Stanley’s entry?

 

输入

The first line of input contains a single integer T (1 ≤ T ≤ 100), the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 1,000), the number of winning entries that Stanley compiled. The next n lines each contain a lottery entry as described above.

 

输出

For each test case, output a single line containing Stanley’s entry.

 

样例输入

复制样例数据

2
3
1 2 3 4 5 6
4 5 6 7 8 9
7 8 9 10 11 12
3
1 2 3 4 5 6
4 5 6 7 8 9
1 2 3 7 8 9

样例输出

4 5 6 7 8 9
1 2 3 4 5 7

 

提示

In the first test case, the numbers 4 through 9 appear twice each, while all other numbers appear at most
one time.
In the second test case, all numbers 1 through 9 appear twice each. The tiebreaking rule means Stanley
prioritizes picking 7 and then the five smallest numbers.

 

题目大意:

先输入一个整数t,代表下面有t组测试,对于每一组测试,先输入一个整数n,下面n行每行输入六个0到49的整数,计算每个整数出现的次数,找到出现次数最多的6个整数,按从小到大的顺序输出,且若7出现的次数与其他数字出现的次数相同,优先考虑7.

解题思路:

记录一下每一个数字出现的次数,将其存到一个结构体中,然后按照出现次数的大小对结构体进行排序,先将前5位存起来,若7已被存起,则继续存入第六位,若7没被存入,则比较7出现的次数与第六个元素出现的次数的大小,如果相等,则存入7,否则存入第六项。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{
	int x;
	int num;
}arr[1200];
map<int,int> ma;
bool cmp(node a,node b) {
	if(a.num==b.num) return a.x<b.x;
	return a.num>b.num;
}
int arr1[10];
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int t;
    cin>>t;
    while(t--) {
    	int n;
    	cin>>n;
    	int nape,num7=0;
    	ma.clear();
    	int cnt=0;
    	rep(i,1,n) {
    		rep(j,1,6) {
    			cin>>nape;
    			if(nape==7) num7++;
    			if(ma[nape]!=0) arr[ma[nape]].num++;
    			else {
    				cnt++;
    				ma[nape]=cnt;
    				arr[ma[nape]].x=nape;
    				arr[ma[nape]].num=1;
    			}
    		}
    	}
    	sort(arr+1,arr+1+cnt,cmp);
    	bool ju=false;
    	rep(i,1,5) {
    		arr1[i]=arr[i].x;
    		if(arr[i].x==7) ju=true;
    	}
    	if(ju==false) {
    		if(num7==arr[6].num) arr1[6]=7;
    		else arr1[6]=arr[6].x;
    	}
    	else arr1[6]=arr[6].x;
    	sort(arr1+1,arr1+7);
    	rep(i,1,6) cout<<arr1[i]<<" ";
    	cout<<endl;
    }

    return 0;
}

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值