How to fix binary search algorithm? - c++

Ok so i have been learning binary search. My teacher has given me a problem on codeforces and it always fails on test 2. Here is the problem:
In this problem jury has some number x, and you have to guess it. The number x is always an integer from 1 and to n, where n
is given to you at the beginning.
You can make queries to the testing system. Each query is a single integer from 1
to n
. Flush output stream after printing each query. There are two different responses the testing program can provide:
the string "<" (without quotes), if the jury's number is less than the integer in your query;
the string ">=" (without quotes), if the jury's number is greater or equal to the integer in your query.
When your program guessed the number x
, print string "! x", where x
is the answer, and terminate your program normally immediately after flushing the output stream.
Your program is allowed to make no more than 25 queries (not including printing the answer) to the testing system.
Input
Use standard input to read the responses to the queries.
The first line contains an integer n
(1≤n≤pow(10,6) — maximum possible jury's number.
Following lines will contain responses to your queries — strings "<" or ">=". The i
-th line is a response to your i-th query. When your program will guess the number print "! x", where x
is the answer and terminate your program.
The testing system will allow you to read the response on the query only after your program print the query for the system and perform flush operation.
Output
To make the queries your program must use standard output.
Your program must print the queries — integer numbers xi
(1≤xi≤n), one query per line (do not forget "end of line" after each xi
). After printing each line your program must perform operation flush.
And here is my code:
#include <iostream>
using namespace std;
int main()
{
int n;
string s;
int k=0;
cin>>n;
int min=1,max=n;
int a;
while(k==0)
{
if(max==min+1)
{
cout<<"! "<<min;
k=1;
break;
}
a=(min+max)/2;
cout<<a<<endl;
cin>>s;
if(s==">=")
min=a;
else
max=a;
}
}
I dont know what test 2 is, but i would be happy to hear ideas as to where my programs is wrong. My guess is its something with the number of guesses. Thanks in advance!
I have tried variations of the loop written above, but they all give the same result.

Related

Find out the average value of inputted integers

I managed to get this program to work. If the user types an unfixed amount of integers, the program will calculate the average value of it. But I need to end it with <Ctrl-D> in my terminal (end of file) in order for it to work. Why can I not just press enter for it to work?
I also believe that I've used an unnecessary amount of variables. Can it be narrowed down to maybe 2 variables?
#include <iomanip>
#include <iostream>
using namespace std;
int main ()
{
int digit {};
int res {};
int counter {};
cout << "Type in integers: ";
while (cin >> digit)
{
counter ++;
res += digit;
}
cout << "The mean was " << setw(1) << setprecision(1) << fixed << static_cast<double>(res) / static_cast<double>(counter) << endl;
return 0;
}
Why can I not just press enter for it to work?
Because that's not how the overloaded >> formatted extraction operator works. This operator skips over an unlimited amount of whitespace characters, including newline characters, until it reads the integer. It's simply how it works: it will read newlines and spaces, after newlines, and spaces, until it sees a digit. That's its mission in life: read and skip over spaces and newlines until it reads at least one digit. It never gets tired of reading newlines and spaces, and will keep going as long that's the case.
To handle input in the fashion you describe requires a completely different approach: using std::getline to read a single line of input into a std::string, up until the next newline character. Then, once that's done, you can check if the std::string is empty, which means that no input was entered, and then terminate; otherwise take the input in std::string and convert it to an int value (using std::stoi, std::from_chars, or a std::istringstream -- take your pick), and then proceed with the existing algorithm.
Can it be narrowed down to maybe 2 variables?
How do you expect to do that? Hard, immutable logic dictates that you must keep track of at least two discrete values: the total sum and the number of values read. But then you just ran out of variables. You have no more variables to use for storing the next read value (if there is one), using whatever approach you chose to use. So, you can't do it. Rules of logic require the use of at least three variables, possibly more depending on how fancy and robust you want your input validation to work.

C++ string calculator

I have been doing exercises on some online judges, and I encounter this question with this default answer.
Question description:
Finally, Hansbug has finally reached the moment to do the last math problem, and there are a bunch of messy addition and subtraction equations in front of him. Obviously success is at hand. But his brain cells have been exhausted, so this important task is left to you.
Input format:
One line, containing a string of addition and subtraction polynomials (the range of each item is 0-32767).
Output format:
An integer, which is the result of the calculation (guarantee that the result of the calculation will not exceed the range of the long integer).
Input and output sample:
Enter #1:
1+2-3
Output #1:
0
And the default answer is :
#include<bits/stdc++.h>
using namespace std;
int ans;
int c;
int main() {
while (cin >> c)
ans += c;
cout << ans;
return 0;
}
How is this even possible!?
All right, let's try it, let's put there some expression with addition and subtraction of ints only, after that press Return, then ctrl+D (end of input):
$ ./a.out
111-222+1
-110$
The loop while (cin >> c) will parse integers including the sign one by one using iostream capabilities until an end of input (you also have to terminate the last number by pressing Return, effectively putting the newline there and triggering the last cin >> c).

Why is the output a question mark in a box instead of a number?

I'm trying to write a program that alerts a user if a certain puppy owner has a puppy that is beyond a certain distance from the puppy owner.
Specifically, the following program:
first asks the user to input the location of the owner, namely inputting 2 integers a and b.
then asks the user to input the number of puppies the owner has. This is a positive integer n.
for each puppy i of the n puppies, the program asks the user to input the location of puppy i. This is 2 integers x and y, both of which are of course dependent on i.
If puppy i is calculated to have reached a distance greater than 10 units from its owner, then the program should inform the user by printing i.
Finally, the program should tell the user the total number of puppies whose numbers have been printed. This number is represented by the variable count, a positive integer.
The following is an example case
Inputs:
(Owner location) 2 1
(number puppies) 4
(location puppies) (15 15), (14 -2), (1 3), (0 4)
Outputs:
Puppy 1 and Puppy 2 too far away
Total 2 puppies too far away
When I try to run the program, the program outputs a question mark instead of the instead of the i's. What did I do wrong, please?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
string puppies;
int a,b;
cin>>a>>b;
int n;
cin>>n;
int i,x,y,count=0;
for (i=1;i<=n;i++){
cin>>x>>y;
int dist;
dist=abs(a-x)+abs(b-y);
if (dist>10){
count++;
puppies += i;
}
}
if (count==1){
cout<<"Puppy "<<puppies[0]<<" too far away"<<endl;
cout<<"Total "<< count <<" puppy too far away";
}
if (count>1){
int j;
for (j=0;j<=(count-2);j++){
cout<<"Puppy "<<puppies[j]<<" and"<<" ";
}
cout<<"Puppy "<<puppies[count-1]<<" too far away"<<endl;
cout<<"Total "<< count <<" puppies too far away";
}
if (count==0){
cout<<"No puppies too far away";
}
}
Here is the copied output (for the same case as above)
Puppy and Puppy too far away
Total 2 puppies too far awayPress any key to continue . . .
Here is a screenshot
enter image description here
You're trying to use a std::string as a container for numbers. It can do that for small enough ones, sure, but you'll probably want to switch to std::vector<int>.
The issue you're actually observing is that puppies[0] is a char, which in std::cout << puppies[0] is interpreted as a single text character, which in your case is a low codepoint corresponding to a non-printable character, hence the question mark displayed by your shell. You can fix it by converting explicitly with static_cast<int>(puppies[0]), but again using a suitable container for actual numbers would be more advisable.
puppies += i;
If you take a look at the overload set, you'll find that there is no overload for int. However, there is an overload for char. All integer types are implicitly convertible to other integer types, and in this case int is converted to char. The character that is appended to the string is the one which is represented by the integer value. What integer value represent what character depends on the character set that your system uses.
You may have intended to append the integer in a textual representation instead. You can convert an integer to a string for example using std::to_string.
That said, using string for this purpose seems backwards, as you don't appear to use it as a character string, but more like an array of integers. As such, a vector of integers might be a more sensible choice.

Time Limit Exceeded - Simple Program - Divisibility Test

Input
The input begins with two positive integers n k (n, k<=10^7). The next n lines of input contain one positive integer ti, not greater than 10^9, each.
Output
Write a single integer to output, denoting how many integers ti are divisible by k.
Example
Input:
7 3
1
51
966369
7
9
999996
11
Output:
4
My Code:
#include <iostream>
using namespace std;
int main()
{
long long n,k, i;
cin>>n;
cin>>k;
int count=0;
for(i=0;i<n;i++)
{
int z;
cin>>z;
if(z%k == 0) count++;
}
cout<<count;
return 0;
}
Now this code produces the correct output. However, its not being accepted by CodeChef(http://www.codechef.com/problems/INTEST) for the following reason: Time Limit Exceeded. How can this be further optimized?
As said by caleb the problem is labeled "Enormous Input Test" so it requires you to use some better/faster I/O methods
just replacing cout with printf and cin with scanf will give you an AC but to improve your execution time you need to use some faster IO method for example reading character by character using getchar_unlocked() will give you a better execution time
so you can read the values by using a function like this , for a better execution time.
inline int read(){
char c=getchar_unlocked();
int n=0;
while(!(c>='0' && c<='9'))
c=getchar_unlocked();
while(c>='0' && c<='9'){
n=n*10 + (c-'0');
c=getchar_unlocked();
}
return n;
}
The linked problem contains the following description:
The purpose of this problem is to verify whether the method you are
using to read input data is sufficiently fast to handle problems
branded with the enormous Input/Output warning. You are expected to be
able to process at least 2.5MB of input data per second at runtime.
Considering that, reading values from input a few bytes at a time using iostreams isn't going to cut it. I googled around a bit and found a drop-in replacement for cin and cout described on CodeChef. Some other approaches you could try include using a memory-mapped file and using stdio.
It might also help to look for ways to optimize the calculation. For example, if ti < k, then you know that k is not a factor of ti. Depending on the magnitude of k and the distribution of ti values, that observation alone could save a lot of time.
Remember: the fact that your code is short doesn't mean that it's fast.

Build a pyramid with numbers between 1 and the inserted integer

i'm trying to build a pyramid with numbers between 1 and the inserted number. For example, if i insert 6 to the integer, that the piramid will be as there:
12345654321
234565432
3456543
45654
565
6
I tried using a for loop but i get in any line one or ++ numbers to 6.
This is the code:
#include<stdio.h>
#include <iostream>
#include <conio.h>
int main()
{
int i,j,d;
std::cin >> d;
for(i=1;i<=d;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
return 0;
}
How can i solve this problem building a pyramid like the shown.
Since this is homework, I won't paste an algorithm, but here's a few hints:
This 12345654321 can be printed by counting from one to six and then back to one.
This __3456543__ means that for numbers smaller than n, you have to output a _ instead, where n depends on the level you are printing.
Define your loop variables within the loop: for(int i=1;i<=d;i++) ... They are only interesting within the loop, and access outside the loop is usually an error, which is then flagged by the compiler.
There's no need to for the getch(); at the end. When you're in the debugger, you can put a breakpoint on the last line. If you aren't you don't want to have to press a key just to end your program.
If you use std::cout << j and std::cout << '\n' for outputting, you don't need printf() either. (Once you want formatting, many will tell you that printf format strings are easier. I don't believe that, but would accept it, if it weren't that you can crash any application with an ill-formed printf format string, while it's much harder to come up with a way to crash your app using streams.)
There you go:
for(j=i;j<=d;j++)
Also you forgot about formatting and the right side of the pyramid, but I think that's out of scope for this question and you can figure the code yourself :)
Try to have the first line working the way you want.
Repeat it d times, where d is the number entered by the user.
Notice that on line l, numbers < l are replaced by spaces.
Consider this: you have D rows, 1..D. Six rows means your rows are numbered 1 to 6. Now, for each row d:
print d-1 space characters. First row has no spaces, second has one and so on.
print the numbers d..D. So on the first line you print 1..6, on the second you print 2..6.
print the numbers D-1..d. So on the first line you print 5..1, on the second you print 5..2
print a new line