How to count negatives? [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 8 years ago.
Improve this question
I wrote this code and the output is not coming as expected. The function returns the negatives recursively. But it is not doing returning what it should be.
size_t recursive_x(stack<int> mystack){
int count=0;
if(!mystack.empty()){
if(mystack.top()<0){
count+=1;
}
mystack.pop();
count+=recursive_x(mystack);
}cout<<count<<endl;
return count;
}
int main(){
stack<int> mystack;
mystack.push(9);
mystack.push(-2);
mystack.push(-2);
mystack.push(1);
mystack.push(-3);
mystack.push(-1);
mystack.push(99);
mystack.push(-1);
mystack.push(1);
mystack.push(1);
recursive_x(mystack);
}
The output should be 5 but it is coming like this:
Output:
0
0
1
2
2
3
4
4
5
5
5

Assuming you do call the function, the output is exactly what I would expect.
The display shows the number of negative numbers on the stack at the point where
cout<<count<<endl;
is invoked.
It is invoked first for the innermost recursion, because you make your recursive call before doing any output for the current recursion.
count+=recursive_x(mystack);
}cout<<count<<endl;
If you just want the output 5, you could print the result returned from your initial call
int finalCount = recursive_x(mystack);
cout<<finalCount<<endl;
and remove the output in recursive_x().

Each recursive call will print the result.
You just need to move the print command to the main:
size_t recursive_x(stack<int> mystack){
int count=0;
if(!mystack.empty()){
if(mystack.top()<0){
count+=1;
}
mystack.pop();
count+=recursive_x(mystack);
}
return count;
}
int main(){
stack<int> mystack;
mystack.push(9);
mystack.push(-2);
mystack.push(-2);
mystack.push(1);
mystack.push(-3);
mystack.push(-1);
mystack.push(99);
mystack.push(-1);
mystack.push(1);
mystack.push(1);
cout<<recursive_x(mystack)<<endl;
}
Output:
5

Related

Why all output 0 [closed]

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 1 year ago.
Improve this question
Why is the result I output on the oj website all 0? Ans defines global variables, and all the values ​​output on Oj are global variables, and the global variables have not changed. But the test samples are output on the local compiler The example can output the correct result.
code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int n,k,ans=0;
char g[100][100];
bool flagx[100],flagy[100];
//int dirx[4]={-1,0,1,0};//上右下左;
//int diry[4]={0,1,0,-1};
void dfs(int x,int y,int use)
{
if(use==k)
{
ans++;
return ;
}
if(y==n) y=0,x++;
if(x==n)
return;
//不放
dfs(x,y+1,use);
//放
if(x>=0&&x<n&&y>=0&&y<n&&g[x][y]=='#'&&!flagx[x]&&!flagy[y])
{
flagx[x]=true;
flagy[y]=true;//标记该位置已经放过棋子;
dfs(x,y+1,use+1);
flagx[x]=false;
flagy[y]=false;
}
//}
}
int main()
{
while(scanf("%d %d",&n,&k))
{
memset(flagx,'false',sizeof(flagx));
memset(flagy,'false',sizeof(flagy));
//memset(g,'')
if(n==-1&&k==-1)
break;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>g[i][j];
dfs(0,0,0);//x,y,ans;坐标,放置的棋子的个数;
cout<<ans<<endl;
ans=0;
}
return 0;
}
You can consider this screenshot:
Unfortunately, the site of your school (which you could include in the question) does not load on my system.
I think that the expected result should depend on the input for n and k at least. What is the result if you input n=1, k=0? I would expect aws=1 from reading your code.
Notice that in your case, the dfs method is exclusively called in the line dfs(0,0,0). I don't know, if that is intended, as the comments are in chinese.

Segmentation fault in solving dynamic problem [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 2 years ago.
Improve this question
Is there any difference in these type of calculating ans in dynamic problems ? Because in case of Code 3 I am getting segmentation fault while Code 1 and Code 2 are passing
int dp[8005][2];//dp is initialized as -1 in main
int a=4;
int b=6;
int tar=90;
int solve(int pos,int ba)
{
if(pos<0||pos>8000||ba>=2)
return 1e6+1;
if(pos==tar)
return 0;
//Code 1
int &ans=dp[pos][ba];
if(ans!=-1)
return dp[pos][ba];
ans=1+solve(pos+a,0);
ans=min(ans,1+solve(pos-b,ba+1));
return ans;
//Code 2
if(dp[pos][ba]!=-1)
return dp[pos][ba];
dp[pos][ba]=1+solve(pos+a,0);
dp[pos][ba]=min(dp[pos][ba],1+solve(pos-b,ba+1));
return dp[pos][ba];
//Code 3
if(dp[pos][ba]!=-1)
return dp[pos][ba];
int ans=1+solve(pos+a,0);
ans=min(ans,1+solve(pos-b,ba+1));
return dp[pos][ba]=ans;
}
Code 3 doesn't update dp until after all the calculations are done.
The first two go
Recurse
Update dp
Recurse
Update dp
Return new value
Note that 1 and 2 are equivalent, since ans is the same as dp[pos][ba].
The third is
Recurse
Recurse
Update dp and return the new value

Whats Wrong in it? [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 3 years ago.
Improve this question
Problem: https://www.hackerrank.com/challenges/migratory-birds/problem
Test Case Link: https://hr-testcases-us-east-1.s3.amazonaws.com/33294/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1573162301&Signature=MgpSHa3lxX%2FwYwumjzAmF8uviZE%3D&response-content-type=text%2Fplain
for this test case i cant get any output why ?
Thanks in advance
#include<iostream>
using namespace std;
int main()
{
long long n,i=0,num=0,mx=0,r=0;
cin>>n;
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
for(int i=1;i<6;i++)
{
if(arr[i]>mx)
{
mx=arr[i];
r=i;
}
}
cout<<r;
}
This looks problematic:
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
If the input value read into num is greater than 5 (or less than zero), undefined behavior will exist when a value is written into an invalid memory location offset from arr.
There is nothing wrong with you code i got accepted all test cases. But the solution can be return in more optimized way

Unordered_set not working on codechef ide [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
"unordered_set" is not working properly on codechef and giving wrong output on its online ide whereas I am getting right output on geeksforgeeks ide and codeblocks
for Input like 3 2 10 1 100 4 3 i am getting 4 rows as expected in codeblocks and geeksforgeeks because n+m-1 is 4 whereas I am getting only 2 rows in codechef what can be the reason behind and now how it will work on codechef?
#include<stdio.h>
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
int main()
{
int n,m,c=0,d,i,j,sum;
int a[10000];
int b[10000];
unordered_set <int> s;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
sum=a[i]+b[j];
if(s.find(sum)==s.end())
{
s.insert(sum);
printf("%d %d\n",i,j);
c++;
}
if(c>=(n+m-1))
{d=1;break;}
}
if(d==1)
break;
}
}
Your program exhibits undefined behavior, by way of accessing the value of an uninitialized variable d.

continue statement inside for loop and if condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have following code snippet and the output i am getting is 4. Please explain me if it takes i=2 or 0. I am confused. And How output was 4?
int main() {
int i=2;
for(i=0;i<2;i++) {
i=i%3;
if(i==2) {
i++;
continue; }
else
++i;
}
printf("%d",i);
}
The loop starts with i = 0. Both the if and the else to exactly the same thing. Increment i and continue.
If you use a bit of logic, the whole block can be reduced to i++ (i = i % 3 has no effect since i < 2).
It's not possible to get 4 with the code you posted.
The output cannot be 4 for the program you posted, because by the time the loop breaks, the value of i would be 2, not 4 and the loop will run exactly once.
Also, your code never enters the if block, because the condition is i==2 which can never be true inside the for loop, as by that time the loop would be exited.
So your code is equivalent to this:
int main() {
int i=2;
for(i=0;i<2;i++) {
i++;
}
printf("%d",i);
}