Problem with ACMP with incorrect C++solution

Task condition :

In a certain state, there are N firms that compete with each other. Each firm has a certain profit per year, equal to V [i] US rubles. The tsar has his favorite firms, and there are unloved ones. Accordingly, the tax for all firms is different and is appointed by the king on an individual basis. The tax on the i-th firm is equal to p [i] percent.

Collectors of statistics decided to calculate which company goes to the state treasury the highest income (all taxes go to the treasury). Unfortunately, they did not learn math or computer science as children (so learn, kids!), and their task is dramatically complicated.

Input data
In the input file INPUT.TXT first, the number N is written - the number of firms (0

Weekend data
To the output file OUTPUT.TXT output a single number - the number of the company from which the state receives the most tax. If there are several such companies, output the company with the lowest number.

What exactly should I do? Print the index of the maximum element, which is equal to a * (b/100).

The first thing you can do: let's say we have arranged the profit in ascending order.

a_1 * (b_1/100) < a_2 * (b_2/100) < ... < a_n * (b_n/100) | *100 (домножим на 100 данное неравенство)

a_1 * b_1 < a_2 * b_2 < a_3 * b_3 < ... < a_n * b_n

So we need to find the index n such that a_n * b_n = max(a_1 * b_1, a_2 * b_2, ... , a_n * b_n), and then even the floating-point variables you don't have to use it.

My solution:

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    int mxcnt = -1; //переменная для записи максимального значения a_k * b_k
    int id = -1; //запись индекса для этого значения.
    for (int i = 0; i < t; i++)
    {
        int a, b;
        cin >> a >> b;
        if (a * b > mxcnt)
        {
            mxcnt = a*b;
            id = i+1;
        }
    }
    cout << id;
}

But in some of the initial tests, my solution gives the wrong answer. What could be the problem? Is my idea itself wrong? Or does the program stumble on some special cases?

Author: Harry, 2020-06-21

1 answers

Because they do not go in pairs "income-interest", but first a series of income, then a series of interest - reread the condition...

So the complete solution in C++ for ACMP (they like it shorter :)) -

#include <iostream>
int v[100],n,m=-1,i,j,d;
#define x std::cin>>
main()
{
    x n;
    for(; i < n; x v[i++]);
    for(; x n; )
        if((n*=v[j++]) > m) m=n,d=j;
    std::cout << d;
}
 4
Author: Harry, 2020-06-22 13:08:41