Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Here is the code, I know what does it do , but I don't understand, what does the if condition do?
if(n&1)
{
for(i=n/2,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
else
{
for(i=n/2-1,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
return 0;
}
Its a bitwise operator. There's a search term for you!
The & operator provides a mask that "cancels out" bits in the first depending if they're set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will "blank" the first set of bits, leaving you with N as 00000001.
Basically that particular if statement drops all except the last bit, which is either 0 or 1, so it is a condition detecting if N is odd or even.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
For example I want to do:
I have 1 it is 00000001 in binary.
and when I shift like 1<<3 I'll take 8 it is 00001000.
So, I need that I take 00001111 after 1<<3.
I wish you understand if something wrong add unclear ask about it please.
I want to do shorter this part:
for(int i=1;h>0;h--,i*=2) hr+=i;
As I understand, you want
std::uint32_t my_shift(std::uint32_t n, std::uint32_t lshift)
{
return (n << lshift) | ((1 << lshift) - 1);
// original shifted | 0b0001111 (lshift 1)
}
You can directly iterate hr:
for(int hr=1; h>0; h--, hr=2*hr+1)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to make it, so lets say i type in 654321, it would say that i typed in 6 numbers.
I need to make it so it counts how many numbers i have typed in, and would display so.
Looking for anyone who could do that for me, thanks in advance.
Considering your entered number is an integer, you can setup a counter variable to count the number of digits and then divide the number by 10 and subsequently increment count in a loop:
#include <iostream>
int main()
{
long long num;
int count = 0;
std::cin>> num;
do
{ count++;
num /= 10;
} while(num != 0);
std::cout<< count;
}
Use long long for large input.
If your entered number is a string, then you can use stoi() to convert it into an integer.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Trying to solve hackerrank problem.
You are given Q queries. Each query consists of a single number N. You can perform 2 operations on N in each move. If N=a×b(a≠1, b≠1), we can change N=max(a,b) or decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0.
Could you suggest how can I improve my code?
int downToZero(int n) {
int dp[n+1];
dp[0]=0;dp[1]=1;dp[2]=2;dp[3]=3;
for(int i=4;i<=n;i++)
{
dp[i]=dp[i-1]+1;
for(int j=2;j*j<=i;j++)
{
if(i%j==0)
{
int fac=max(j, i/j);
dp[i]=min(dp[i], dp[fac]+1);
}
}
}
return dp[n];
}
The bit that is too slow is:
for(int j=2;j*j<=i;j++)
{
if(i%j==0)
You are scanning linearly to find factors. There has to be a better way. Keeping a list of primes, for example.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);
Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the end.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have been working on this problem and I am having trouble solving it. Not sure where to start, can someone help me?
int below(Plot p, line l);
int above(Plot p, line l);
line findLine(Plot p, line l)
{
}
It's standard binary search problem.
Now think what you need to find?
The liney = k which divide the plot in 2 equal part (based on number of points).
What is the range of values y can take?
Clearly it would be -1 to +1.
Can you calculate if a line satisfies the property desired by the problem?
Yes you can. Just check every point of Plot p and then check how many of those points have y co-ordinate greater than k. As you know total number of points then you will know if the points on two sides of the line are equal.
Approach
Suppose you select a line y=k. Now at this point you see there are more points above than there is below it. Now you will move upward. And if there is more in downside then you will move downward.
double good = -1.0,bad= 1+EPSILON;
for(int iter=0;iter<=100;iter++)
{
mid = (good+bad)/2.0;
if(check(P,mid)) // if equally divides it then it's done
// mid is one answer;
else if( below(p,mid)<above(p,mid))
good=mid;
else
bad=mid;
}