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
感想:读不懂题意是硬伤啊!!!,一开始认为是总的出现次数最大或相等就积上一分,但是这里是在一个数里面的出现次数啊!!!英语,我要向你挑战!
题解:读懂题意就没啥可讲的了吧,我们主要是怎么去实现它,实现就自己实现吧,或者看代码~
还有那个函数,是a*x*x+b*x,由于a,b为正整数,则其对称轴-b/2*a为负数,故从1开始递增,那么就好办了,一个for从1开始就好了,如果大于n跳出。如果是1的时候就大于n则输出-1,因为后面的更大于n!
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int f(int a, int b, int x) { return a * x*x + b * x; };
int num[20], point[20];
int main()
{
int t;
cin >> t;
int a, b, n;
while (t--)
{
memset(point, 0, sizeof(point));//积分数组
cin >> a >> b >> n;
if (f(a, b, 1) > n)//第一个都大于n,后面的肯定都大于n
{
cout << "-1" << endl;
continue;
}
for (int i = 1;; i++)
{
int x = f(a, b, i);
if (x > n) //终止条件
break;
memset(num, 0, sizeof(num));//这里每次都要置零,因为我们记录的在这一个数x中的位出现次数
int maxx = 0;//与上同理
while (x) //找每一位
{
num[x % 10]++;
maxx = max(maxx, num[x % 10]);//记录出现最大次数
x /= 10;
}
for (int j = 0; j <= 9; j++)//积分
{
if (num[j] == maxx)//如果他出现次数最多或者相等就积分上一分
point[j]++;
}
}
int id, minn = -1;
for (int i = 0; i <= 9; i++)//找目标
{
if (point[i] > minn)
{
minn = point[i];
id = i;
}
}
cout << id << endl;
}
return 0;
}