Unordered_set not working on codechef ide [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 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.

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

What is the difference between the two codes in 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 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

Pseudo- andom number generation error [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 8 years ago.
Improve this question
I am the very beginner of the developing field, and I am facing a problem in Pseudo code random error in C++ language.
I think the Error in my program is:
{
srand(time (NULL));
}
Please help me how can I remove this error and the reason of error.
The program I developed is below,
#include<iostream>
using namespace std;
main()
{
int lowerRange;
int upperRange;
lowerRange=1;
upperRange=1;
int secretNumber;
int guess;
guess=10;
cout<<"My Student ID is BC130400789 "<<endl;
cout<<"Enter lower range : ";
cin>>lowerRange;
cout<<"Enter upper range : ";
cin>>upperRange;
cout<<"Computer is calculating a random secret number in the given range...Done!"<<endl;
cout<<"\nPlease guesss the secret number in the range ["<<lowerRange<<" - "<<upperRange<<"]: ";
cin>>guess;
if(guess<10)
{
cout<<"You won! You guess the correct number.. ";
}
else
{
cout<<"Oooppsss...Your entered number is too high...Computer won"<<endl<<endl;
}
{ srand(time (NULL));}
secretNumber = rand()%10+1;
cout<<"Secret number was: "<<secretNumber<<endl<<endl;
}
system("pause");
}
main HAS TO return int
rand() is defined in cstdlib which you don't include
You also need to include ctime for time function
As said by G. Samaras you shouldn't use system("pause") (also you don't include required header for it) and you have mismatched {}.
I would also recommend you reading something about code formatting.
You have an extra bracket at the end. Remove this and the system("pause"); and you will get no errors. Also main() usually returns an int, thus make it int main().
system(“pause”); - Why is it wrong?