Codeforces question (unable to detect problem in my code) [closed] - c++

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 2 years ago.
Improve this question
Over the past 2 days i was practising on codeforces . I am new to programming and currently doing implementation problems.
This question is 1337B ( KANA AND DRAGON QUEST)
https://codeforces.com/problemset/problem/1337/B
I cannot figure out my mistake in this code. Please help me out
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
for (int i=0;i<t;i++)
{
long long int x,n,m;
cin>>x>>n>>m;
for( int j=0;j<n&&x>0&&x>20;j++)
{
x=x/2+10 ;
}
x=x-m*10;
if (x>>0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}

Your code is correct but you made a small error at the end. You do x>>0 instead of x > 0. ">>" is a bitwise shift operator and not a greater than operator. After checking with codeforces test input it does seem to work. Here is the version with no error
if (x>0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}

Related

what is wrong in this easy if else c++ code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This question is in hackerrank conditional statements and i wanted to solve it without using array.The code runs but gives wrong output. Can anyone see what is the mistake in this code , Why would it not work?
#include <bits/stdc++.h>
using namespace std;
int n ;
cin >> n ;
int main()
{
int n ;
cin>>n;
if(n=0){
cout<<"zero";
}
else if(n=1){
cout<<"one";
}
else if(n=2){
cout<<"two";
}
else if(n=3){
cout<<"three";
}
else if(n=4){
cout<<"four";
}
else if(n=5){
cout<<"five";
}
else if(n=6){
cout<<"six";
}
else if(n=7){
cout<<"seven";
}
else if(n=8){
cout<<"eight";
}
else if(n=9){
cout<<"nine";
}
else{
cout<<"Greater than 9";
}
return 0;
}
You should use '==' not '='
because "if (n=1)" behaves the same as "if(true)"
== is compare
= is assignement

wrong output on printing subsets of array [closed]

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 1 year ago.
Improve this question
I am trying to print all the subsets of an array. But not getting the correct output.
#include<iostream>
using namespace std;
int arr[]={1,2,3,4,5};
int n=5;
void print(int a[],int cnt, int idx)
{
if(idx==n)
{
for(int i=0;i<cnt;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
a[cnt]=arr[idx];
print(a,cnt,idx+1);
print(a,cnt+1,idx+1);
}
int main()
{
int a[5]={0};
print(a,0,0);
}
the above code only prints "5"
Please help me to rectify the same.
Replace the array with a vector. As the array in C++ is passed by reference, the print function is accessing the same array, which causes the problem.
#include <iostream>
#include<vector>
using namespace std;
int arr[]={1,2,3,4,5};
int n=5;
void print(vector<int>a,int cnt, int idx)
{
if(idx==n)
{
for(int i=0;i<cnt;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
a[cnt]=arr[idx];
print(a,cnt,idx+1);
print(a,cnt+1,idx+1);
}
int main()
{
vector<int >a(5,0);
print(a,0,0);
}

I can not do a swap operation while sorting. It crashes when I do it. Why? [closed]

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 4 years ago.
Improve this question
struct Dates
{
int day;
int month;
int year;
}accountinfo[3];
struct Accounts
{
string name,lastname;
int number;
float balance;
}account[3];
void sortduetoaccnumbers()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (account[j].number>account[j+1].number)
{
//swap
}
}
}
}
void sortduetodates()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (accountinfo[j].year>accountinfo[j+1].year)
{
//swap
}
else if (accountinfo[j].year==accountinfo[j+1].year)
{
if (accountinfo[j].month>accountinfo[j+1].month)
{
//swap
}
else if (accountinfo[j].month==accountinfo[j+1].month)
{
if (accountinfo[j].day>accountinfo[j+1].day)
{
//swap
}
}
}
}
}
}
I can not sort these accounts using sorting algorithms. It crashes if I enter them. cmd stops suddenly and finishes the program.
I entered a comment line where swapping functions have to go. So you can analyze the code.
Every other functions are working except this one. I'm stuck at this point.
this code is wrong:
if (accountinfo[j].year>accountinfo[j+1].year)
because if j==2, then j+1=3 - index over array size => undefined behavior (and crash in your case)
you need to change loop condition to j<2 or rewrite your checks

Postfix notation stack C++ [closed]

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 6 years ago.
Improve this question
I am new to C++ and I want to use a stack to evaluate an expression given as an input (2+3*5+4 for example), containing only numbers, + and *. I wrote this code but it gives me Segmentation fault: 11. Could you please help me solve this or give me a hint about what could be wrong? Thank you! (I noticed there were similar questions on this site, but I couldn't use them to solve my problem)
#include <iostream>
#include <stack>
using namespace std;
bool highPrecedence(char a, char b){
if((a=='+')&&(b=='*'))
return true;
return false;
}
int main()
{
char c = 'a';
double x;
stack<char> stack;
double v[10];
int i=0;
double res;
while(true)
{
c = cin.get();
if(c=='\n'){
while(stack.size()!=0){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
break;
}
if ( '0'<=c && c<='9' )
{
cin.putback(c);
cin>>x;
cout<<"Operand "<<x<<endl;
i=i+1;
v[i]=x;
}
else
{
if(c!=' ') cout<< "Operator " <<c<<endl;
if (stack.size()==0)
stack.push(c);
else{
while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
stack.push(c);
}
}
}
cout<<v[0]<<endl;
}
Using stack.top() is illegal if the stack is empty.
Change while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
to while((!stack.empty()) && (!highPrecedence(stack.top(),c))){
The initiali value of i is not good and you are printing uninitialized variable, which has indeterminate value.
Change int i=0; to int i=-1;

error in compilation in codeforces [closed]

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 7 years ago.
Improve this question
can anyone help me in this
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define ll long long int
struct points{
ll a;
ll b;
};
int cos(points x , points y)
{
return x.b<y.b;
}
int main()
{
ll n,r,avg,i,j,k;
points pt[100005];
cin>>n>>r>>avg;
for(i=0;i<n;i++)
{
cin>>pt[i].a>>pt[i].b;
}
sort(pt,pt+n,cos);
ll sum=avg*n;
for(i=0;i<n;i++)sum-=pt[i].a;
if(sum<=0)cout<<"0\n";
else
{
ll ans=0;
for(i=0;i<n;i++)
{
if(sum==0)break;
else
{
if(sum>r-pt[i].a)
{
ans=ans+pt[i].b*(r-pt[i].a);
sum=sum-(r-pt[i].a);
}
else
{
ans=ans+sum*pt[i].b;
sum-=sum;
}
}
}
cout<<ans<<endl;
}
return 0;
}
when im compiling in my system its working fine and getting the correct output but when i'm submitting in codeforces under GNUC++ 11 im getting compllation error? can u help me
Your compare function, cos(), collides with function in standard <cmath>. Rename it and it will compile.