I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
1==0 takes more precedence than pos&1. Try if((pos&1)==0){
Related
I have written this program, it passes all manual test conditions but says "wrong answer" when I submit online on an IDE.
Constraints
0≤a,b,c≤180
#include <iostream>
using namespace std;
int main() {
// your code goes here
double a,b,c;
cin>>a>>b>>c;
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
return 0;
}
The above code doesn't give correct answer when either of a,bc is zero and the a+b+c=180.
So,
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//add the below statement
if((a!=0)&&(b!=0)&&(c!=0)){
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
}
else{
cout<<"NO";
}
return 0;
}
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//made the changes with the help of suggestion from the forum
if((a>0)&&(b>0)&&(c>0)&&(a+b+c==180))
cout<<"YES";
else
cout<<"NO";
return 0;
}
Hey guys this is the question's link from hackerrank
hackerrank problem
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long int sizeArr, operation;
cin>>sizeArr>>operation;
long int array[sizeArr];
for(long int i=0;i<sizeArr;i++)
array[i]=0;
for(long int i=0;i<operation;i++)
{
long int a,b,k;
cin>>a>>b>>k;
for(long int j=a-1;j<=b-1;j++)
array[j]+=k;
}
sort(array,array+sizeArr);
cout<<array[sizeArr-1];
}
I coded it like this and the another person code it like
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int N,M,a,b,k,i,j,max=0,x=0;
cin>>N>>M;
long int *Arr = new long int[N+1]();
for(i=0;i<M;i++)
{
cin>>a>>b>>k;
Arr[a]+=k;
if((b+1)<=N) Arr[b+1]-=k;
}
for(i=1;i<=N;i++)
{
x=x+Arr[i];
if(max<x) max=x;
}
cout<<max;
return 0;
}
Mine one didn't clear all the test cases but the second one's code did. Any suggestions.
The problem is that your colleague has this if((b+1)<=N) Arr[b+1]-=k; which means he sometimes subtracts but in your code, you only add in here array[j]+=k;.
And also your while function has a bigger range than your colleague (for example if a user gives as a=1 and b=3 you go through 0,1,2 but your colleague only goes at 1 and 3. I am not sure that this is the problem but you should check out.
Description
This code is intended to find a spanner index (i) when dealing with
Splines/NURBS basis functions based on a knot vector (U), a choosen knot (u), the degree of the desired curve (p) and the number of basis funcions (n). The algorithm was taken from the NURBS Book by Piegl and Tiller. The error, I guess, is in the way I declared variable U. Thaks in advanced!
code
# include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(n,p,u,U) /* an error in this line */
{
if (u==U[n+1]) return (n);
low=p; high=n+1;
mid=(low+high)/2
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2
}
return (mid);
}
You have forgotten some semicolons and types!
here is the correct code:
#include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(int n, int p, int u, int U[])
{
if (u==U[n+1]) return (n);
int low=p, high=n+1;
int mid=(low+high)/2;
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2;
}
return (mid);
}
int main() {
cout << FindSpan(n, p, u, U);
return 0;
}
the output I'm getting for the given code is "0" even though I
initialized the value of c as "1"...can somebody explain it...
Why is the value of "C" changing in the following code??
#include <iostream>
using namespace std;
int c=1; // global initialized 'c' as 1..
long long f(long long n){
if(n==6){
return 2;
}
else{
c=c+1;
f(n-2);
}
}
int main()
{
long long n,ans,p;
cin>>n;
ans=f((2*n)-2);
cout<<c; //printing out the value of 'c'
return 0;
}
Because value of c is being changed in below code :
else{
c=c+1;
f(n-2);
}
While I am trying to compile the code with few modification in binary search recursive function. The program is acting weird. Some time it gives the correct value and some time it goes to infinite loop. Please explain what went wrong with the code. I am using DEV C++ as an IDE.
CODE:
#include<iostream>
#include<sstream>
using namespace std;
//function to compare the two integers
int compare(int low, int high)
{
if (low==high)
return 0;
if (low<high)
return 1;
else
return -1;
}
//Function for binary search using recursion
int *BinarySearch(int *Arr,int Val,int start,int end)
{
int localstart=start;
int localend=end;
int mid=(start+end)/3;
cout<<"MID:"<<mid;
int comp= compare(Val,Arr[mid]);
if(comp==0)
return &(Arr[mid]);
else if (comp>0)
return BinarySearch(Arr,Val,localstart,mid-1);
else
return BinarySearch(Arr,Val,mid+1,localend);
return NULL;
}
main()
{
int *arr;
arr= new int [256];
string str;
getline(cin,str);
stringstream ss;
ss<<str;
int index=0;
while(ss>>arr[index])
{index++;}
//cout<<arr[index-1];
cout<<"Enter Value:";
int value;
cin>>value;
int *final;
final=BinarySearch(arr,value,0,index-1);
if(final!=NULL)
cout<<"Final:"<<*final;
else
cout<<"Not Found";
getchar();
getchar();
return 0;
}
Two ideas:
What should BinarySearch do if Val is not in the array? Trace out what your code does in this case.
(start+end)/3 probably isn't the middle of the current range.