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.
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).
I am trying to create multiple adders via Verilog to compare the speed of each adder.
I am getting the following error Unable to bind parameter WIDTH in arith_unit
This is my code so far:
module arith_unit
#(
parameter ADDER_TYPE = 0,
parameter WIDTH = 8)
(input [WIDTH-1:0] a,
input [WIDTH-1:0] b,
output [WIDTH-1:0] sum,
);
////////////////////////////////////////////////////////////////
if (ADDER_TYPE == 0)
begin
ripple_carry rc1 (a, b, cin, sum, cout);
end
////////////////////////////////////////////////////////////////
else if (ADDER_TYPE == 1)
begin
carry_bypass cb1 (a, b, cin, sum, cout);
end
//// more else statment for ADDER_TYPE...
endmodule
//////////////////////////////////////////////////////////////////
module ripple_carry_4_bit(a, b, cin, sum, cout);
input [3:0] a,b;
input cin;
wire c1,c2,c3;
output [3:0] sum;
output cout;
full_adder fa0(.a(a[0]), .b(b[0]),.cin(cin), .sum(sum[0]),.cout(c1));
full_adder fa1(.a(a[1]), .b(b[1]), .cin(c1), .sum(sum[1]),.cout(c2));
full_adder fa2(.a(a[2]), .b(b[2]), .cin(c2), .sum(sum[2]),.cout(c3));
full_adder fa3(.a(a[3]), .b(b[3]), .cin(c3), .sum(sum[3]),.cout(cout));
endmodule
////////////////////////////////////////////////////////////////
module ripple_carry (a, b, cin, sum, cout);
input [WIDTH-1:0] a,b;
input cin;
wire c1,c2,c3, c4, c5, c6, c7;
output [WIDTH-1:0] sum;
output cout;
if (WIDTH == 8) begin //<-------- compiler is pointing here as the problem
ripple_carry_4_bit rca1 (.a(a[3:0]),.b(b[3:0]),.cin(cin), .sum(sum[3:0]),.cout(c1));
ripple_carry_4_bit rca2 (.a(a[7:4]),.b(b[7:4]),.cin(c1), .sum(sum[7:4]),.cout(cout));
end
else if (WIDTH == 16) begin
ripple_carry_4_bit rca1 (.a(a[3:0]),.b(b[3:0]),.cin(cin), .sum(sum[3:0]),.cout(c1));
ripple_carry_4_bit rca2 (.a(a[7:4]),.b(b[7:4]),.cin(c1), .sum(sum[7:4]),.cout(c2));
ripple_carry_4_bit rca3 (.a(a[11:8]),.b(b[11:8]),.cin(c2), .sum(sum[11:8]),.cout(c3));
ripple_carry_4_bit rca4 (.a(a[15:12]),.b(b[15:12]),.cin(c3), .sum(sum[15:12]),.cout(cout));
end
//// more else statment for 32 & 64 bits...
endmodule
I looked online and it seems that my if and else statements are correct. So I am assuming that my WIDTH is declared wrong? I tried passing in WIDTH via module ripple_carry (a, b, cin, sum, cout, WIDTH); kind of how you would do it with C++, but I still get the same error.
I've tested the ripple_carry on its own (no if statements, no parameter) with 8 bits, 16 bits, etc. and it works fine. So I know that my logic for the ripple_carry is fine.
Please help me, thank you.
It would have helped to show the exact error message with the line number, but you are missing a WIDTH parameter declaration in the module ripple_carry.
module ripple_carry #(parameter WIDTH=8)(...
And add it when instantiating it inside the generate block.
ripple_carry #(WIDTH) (a, b, cin, sum, cout);
I'm trying to make a parameter and module to AND two numbers together and return a result. However, I seem to be having troubles. My code is as follows:
module main;
reg signed [3:0] x;
reg signed [3:0] y;
wire [3:0] out1;
ander #(.L1(4)) andd(.in1(x), .in2(y), .out(out1));
initial begin
x = 4'b0111;
y = 4'b1110;
$display("%b", out1);
$finish;
end
endmodule
module ander #(parameter L1 = 8)
(input [L1-1:0] in1,
input [L1-1:0] in2,
output wire [L1-1:0] out);
assign out = in2&in1;
endmodule
When I try to run this, the output is "zzzz." Am I doing something wrong?
When I change it to
$display("%b", x&y);
it seems to display just fine.
Do you have any recommendations?
Add a little delay before the $display
initial begin
x = 4'b0111;
y = 4'b1110;
#1; // <-- Add a little delay
$display("%b", out1);
$finish;
end
Verilog does one operation at a time. When inside a procedural block, it will execute every operation within the block until it hits a blocking statement or finishes. In Your case, the assign statement didn't have time to react to the changes in x and y before the initial block called $display and $finish.
If you want a deterministic output, add some delay before your $display:
initial begin
x = 4'b0111;
y = 4'b1110;
#1;
$display("%b", out1);
$finish;
end
This prints 0110.
I would like to skip n number of lines. It could be done as:
do i = i,n
read(file,*)
enddo
However, I would like to do that by implied do-loop without dummy variable, like
read(file,*)(,i=1,n)
READ (file, '(' // REPEAT('/',n-1) // ')')
but promise you won't do this...