Owl Geeks 【map 模拟】

本文介绍了一个数学问题,通过给定的二次方程找出小于等于N的所有整数解中出现次数最多的数字作为获胜数字。文章提供了完整的C++代码实现,帮助读者理解如何计算每个数字的得分并确定最终的获胜数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The owls have the following equation:

Y = a × x2 + b × x

With a, b, and N given, they decide to put into a set the integer values of Y that are less than or equal to N and that are outputted from the equation from any positive integer x.

With that set of numbers, they come up with the problem of finding the winning digit among them.

The winning digit is a digit from 0 to 9 that will get the maximum number of points. How are points for a digit calculated you may ask? Well, be a bit more patient, I’m going to tell you now.

For each number in the set, if the digit was the most repeated digit or tied with other digits as the most repeated digit in the ith number of set S, then it would get one point from that ith number.

Can you tell the owls what the winning digit is?

Input
The first line of input is T – the number of test cases.

The first line of each test case is a, b, and N (1 ≤ a, b, N ≤ 105).

Output
For each test case, print on a line the winning digit with the maximum number of points. If there is a tie, print the minimum digit among them. If the set is empty, print  - 1.

Example
Input
2
1 2 50
20 3 10
Output
3
-1
能够读懂题意,就可以写 了。
代码

#include <cstdio>
#include <cstring>
#include<algorithm>
#include<map>
using namespace std;

const int inf = 0x3f3f3f3f ;
map<int,int>mp;
map<int,int>mpp;
map<int,int>:: iterator it;

int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        mp.clear();
        int a,b,n;scanf("%d%d%d",&a,&b,&n);
        int z; int m;int cnt;int x;
        int maxnum=-1;
        for( x=1;a*x*x+b*x<=n;x++){
            z=a*x*x+b*x; m=z;
            mpp.clear(); cnt=0;

            while(m){
                int k=m%10;
                mpp[k]++;  cnt=max(cnt,mpp[k]);
                m/=10;
            }

            for(it=mpp.begin();it!=mpp.end();it++){
            if(it->second==cnt){
                //printf("%d \n",it->first);
                mp[it->first]++;
                maxnum=max(maxnum,mp[it->first]);
            }
        }
    }
        if(x==1) {
            puts("-1");
            continue;
        }
        int ans=inf;
        for(it=mp.begin();it!=mp.end();it++){
            if(it->second==maxnum){
                ans=min(ans,it->first);
            }
            //printf(" %d  %d\n",it->first,it->second);
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值