Hello, I have a problem with one task from polish SPOJ. I have to write a program that counts how many times the “searched” value was included in test case. We don’t know how many test cases will be in that problem. For example, my single test case looks like this:
1 3 11 1 7
First number is the “searched” value which says what number I have to check. The second number is the “n” value, which means how many numbers after n value I have to write. In that case, n=3. So I have three numbers (11 1 7) and I have to write how many times the searched value (In that case 1) was included.
Other examples:
Inputs:
1 3 11 1 7
2 4 1 2 4 3
3 5 2 2 2 2 2
4 4 4 4 4 4
Outputs:
1
1
0
4
My code looks like this, it works with 0.02 time:
#include <iostream>
void how_many();
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
how_many();
return 0;
}
void how_many()
{
int searched,n,number,counter=0;
while (std::cin>>searched>>n)
{
while (n--)
{
std::cin>>number;
if (number==searched)
++counter;
}
std::cout<<counter<<"\n";
counter=0;
}
}
The question is: What I have to do to improve it, so it can run in 0.00 time? I really wants to get that perfect time