why is this "He is offside!"(spoj) solution giving my WA? [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 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

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.

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.

Is if statement over complicated? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is the part that brakes, zod1 zod2 zodA are vectors. In vectors I store int. Cant tell why it crashes cause it gives no error
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
Questions:
1 Is if statement over complicated
2 Can I use vectors like that for solving math in if statement (sorry can't explain better)
3 If 2 question is true how should I solve it
Explanations:
variable names are in my own language (sorry if it looks weird)
Values are zero for all (ex zod1[u]=0)
added whole function (variables going to the function are passed correctly and I know I use some unnecessary thing)
void calc(vector<char> zodis1, vector<char> zodis2, vector<char> zodisAts,int zo1,int zo2,int zoA)
{int i,keliamas=0;
int k =0;
vector<int> zod1(0);
vector<int> zod2(0);
vector<int> zodA(0);
for(i=0;i<zodis1.size();i++)
{
zod1.push_back(0);
}
for(i=0;i<zodis2.size();i++)
{
zod2.push_back(0);
}
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0);
}
int u=zodis1.size()-1;
int y=zodis2.size()-1;
int t=zodisAts.size()-1;
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
{//if((zod1[u]+zod2[y])/10==1)
{
keliamas=1;
}
if(u==0||y==0||t!=0)
{
if(keliamas==1)
{
}
}
u--;
y--;
t--;
}
else
{if(zod1[u]!=9)
zod1[u]=zod1[u]+1;
else
{ if(u!=zodis1.size()-1)
u++;
else
{
cout<<"something wrong man";
}
}
}
}
EDIT:
Error is here:
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0); // should be zodA
}
Is if statement over complicated
No, I think it is very simple with just one 'or'. However, if zodA[t] is always less than 10, then your if condition can be written as:
if ( (zod1[u]+zod2[y])%10==zodA[t] )
Can I use vectors like that for solving math in if statement (sorry
can't explain better)
Yes you can.
If 2 question is true how should I solve it
If it compiles but then crashes, then probably you are accessing out of bounds indices. Check that your indices are less than the vector sizes.

C++: Algorithm wrong output ( works on java ) [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 8 years ago.
Improve this question
Can someone help me with my code ? I'm trying to use different algorithm but it returns way to big numbers, when i use algorithm between /* */ it works perfect, anyone can see whats wrong with my new code ? (same on java works)
int* czynnikiPierwsze(int n)throw (string){
if(n<0){
string wyjatek1="Nie mozna rozlozyc ujemnej liczby";
throw wyjatek1;
}
int b=0;
while(n>2){
n=n/tab[n-2];
b++;
}
dzielniki=new int[b]();
int j=0;
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
j++;
}
/* int a=n;
int*dzielniki=new int[30]();
for(int j=0;j<n+a;j++){
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
break;
}
}*/
return dzielniki;
}
There's no chance your second while(n>2) loop runs even once, since the first loop only exited when that same condition was no longer true.

How to find the highest card in Poker in C++? [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
card highCard ()
{
card highest_card;
int number=0;
for (int i =0; i <5; i++)
{
if (_cards[i].getRankValue > number)
{
number = _cards[i].getRankValue;
highest_card = _cards[i];
}
return highest_card;
}
}
I am not sure what I am doing wrong, or more like, what I am doing. I am suppose to find the highest card in my hand (including the suit and rank) for a game in poker.
What you have looks perfectly fine, except you are returning too early. You want to return after looking at all the cards in the hand. As you have it now, your function will only look at _cards[0] before returning.
card highCard ()
{
card highest_card;
int number=0;
for (int i =0; i <5; i++)
{
if (_cards[i].getRankValue > number)
{
number = _cards[i].getRankValue;
highest_card = _cards[i];
}
}
return highest_card;
}
You're returning the card in the for loop. Basically, the code is running the loop at i=0 and then returning it right after- it never runs the code when i is 1 or above. Put the return line after the for loop so you ensure the program runs through all the cards before returning the highest card.