Why all output 0 [closed] - c++

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.

Related

How to add decimal to floating point number with non-decimal value [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 last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have following program and i want the output to be 10.0
what line of code I have to add in function fun so that i have desired output.
#include<bits/stdc++.h>
using namespace std;
float fun(float a)
{
return a;
}
int main()
{
float a = 10;
cout << fun(a);
return 0;
}
I tried using setprecision() but it is often used with cout. How it can be used when returning the output? I am stuck here.
Nothing to stop this
float fun(float a)
{
cout << fixed << setprecision(1);
return a;
}
But that's stupid code for a stupid puzzle. What does this have to do with real programming?

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

What's the difference between the given two codes. One gives time limit exceeded when run on ideone and the other works fine [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 5 years ago.
Improve this question
1st code: works fine gives success with time of 0sec
int main()
{
int n=100000;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{}
cout<<"ffdfdf";
}
2nd code: gives a time limit exceeded
int main()
{
int n=100000;
bool **a=new bool*[n];
for(int i=0;i<n;i++)
{
bool[i]=new bool[n];
for(int j=0;j<n;j++)
{
bool[i][j]=1;
}
}
cout<<"ffdfdf";
}
can anyone explain why the two code fragments have a vast time difference.I am not understanding it.
bool[i]=new bool[n]; is extremely expensive cf. the other statements.
A good compiler will optimise out your first program to cout << "ffdfdf";, since it will know that the loop doesn't do anything.
Once you've replaced your errant bools with as so the second snippet actually compiles, you'd be advised to pair your new[] calls with delete[].

why is this "He is offside!"(spoj) solution giving my WA? [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 7 years ago.
Improve this question
ideone link: https://ideone.com/hOBBMA
problem link: http://www.spoj.com/problems/OFFSIDE/
code:
enter code here
#include <iostream>
using namespace std;
int main() {
int n1,n2,i,j;
while(1)
{
int count=0;
cin>>n1>>n2;
if(n1==0 && n2==0)
break;
else
{
int a[n1],d[n2];
for(i=0;i<n1;i++)
{
cin>>a[i];
}
for(i=0;i<n2;i++)
{
cin>>d[i];
}
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]>d[j])
{
count++;
}
}
}
}
if(count>=2)
cout<<"N"<<endl;
else
cout<<"Y"<<endl;
}
return 0;
}
it gives the correct answer with the given test cases but apparently it's a WA
Consider this input; two attackers at 400m from the goal and two defenders also at 400m from the goal. Your code would count '0' i.e. offside whilst according to the rules neither of the attackers isn't.
An attacking player is offside if he is nearer to his opponents’ goal
line than the second last opponent. [This is not true for any of the attackers in this case so definitely not an offside]
A player is not offside if he is level with the second last opponent [In this case they are level so certainly not offside] or he is level with the last two opponents [And this is true too].
2 2
400 400
400 400
0 0

intersection code that prints the intersection of two arrays ( posting lists ) [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 9 years ago.
Improve this question
I wrote this code to print the intersection of two arrays ( posting lists) using dev c++
the problem that when I run the program nothing is printed
can you help ?
I need to know where is the problem
and what if I wanted to use cout instead of printf?
#include <iostream>
#include <stdio.h>
using namespace std;
// Intersecting two postings lists (a “merge” algorithm)
// and I will assume that the two posting listst are sorted Ascendingly
// I will suppose that the first posting list is an array wich
// have n elements I wil name it fistPost
// I will suppose that the second posting list is an array wich have
// m elements I will name it secondPost
int main()
{
int firstPost[] ={3,5,7,8,13,15,30,34};
int secondPost[]={1,5,7,9,11,15,20,34,35};
int i,j=0;
int n = sizeof(firstPost)/sizeof(firstPost[0]);
int m = sizeof(secondPost)/sizeof(secondPost[0]);
while(i<n && j<m)
{
if (firstPost[i]<secondPost[j])
i++;
else if (firstPost[i]>secondPost[j])
j++;
else if (firstPost[i]=secondPost[j])
{
printf ("%i", secondPost[j++]);
i++;
}
}
system("PAUSE");
return 0;
}
You haven't initialized 'i' to 0, hence its taking some garbage value and not executing the while loop.
change int i, j = 0;
to int i = 0, j = 0;
change this printf ("%i", secondPost[j++]);
to
printf ("%d", secondPost[j++]);