C++: Error in login, identify a valid triangle with greater greater then 0, all three angles provided - c++

I have written this program, it passes all manual test conditions but says "wrong answer" when I submit online on an IDE.
Constraints
0≤a,b,c≤180
#include <iostream>
using namespace std;
int main() {
// your code goes here
double a,b,c;
cin>>a>>b>>c;
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
return 0;
}

The above code doesn't give correct answer when either of a,bc is zero and the a+b+c=180.
So,
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//add the below statement
if((a!=0)&&(b!=0)&&(c!=0)){
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
}
else{
cout<<"NO";
}
return 0;
}

int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//made the changes with the help of suggestion from the forum
if((a>0)&&(b>0)&&(c>0)&&(a+b+c==180))
cout<<"YES";
else
cout<<"NO";
return 0;
}

Related

Similar solutions but different answers

I am facing difficulty while solving a problem where we have to check whether a string is a subsequence of another string or not.
A man with name M is allowed to marry a woman with name W, only if M is a subsequence of W or W is a subsequence of M.
A is said to be a subsequence of B if A can be obtained by deleting some elements of B without changing the order of the remaining elements.
Example -
john and johanna will give "YES" as output
kayla and jayla will give "NO" as output
johanna and john will give "YES" as output
My code is :
#include <iostream>
#include<string>
using namespace std;
bool checksub(string a, string b)
{
int pos=0;
for(int i=0; i<a.size(); i++)
{
int flag=0;
for(int j=pos; j<b.size(); j++)
{
if(b[j]==a[i])
{
flag=1;
pos=j;
break;
}
}
if(flag==0)
{
return false;
}
}
return true;
}
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
if(a.size()==b.size())
{
if(a==b)
{
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;}
}
else if(a.size()>b.size()){
if(checksub(b,a))
{
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}else{
if(checksub(a,b))
{
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}
return 0;
}
The editorial of the question uses a similar approach. Can anybody tell me what's wrong with my code?
The editorial solution is given below :
#include <cstdio>
char M[25005], W[25005];
bool contains(const char *A, const char *B){
while(*A){
if(*B==*A)
B++;
A++;
}
return !*B;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%s %s", M, W);
puts(contains(M, W) || contains(W, M) ? "YES" : "NO");
}
return 0;
}
Link to the problem: https://www.codechef.com/problems/NAME2
Your code produces the wrong result with input AA BAB because it fails to account for the fact that you need to have two A in the second string.
You might be able to fix it by changing pos=j; to pos=j+1; but I'm not certain.
There really is no similarity between your code and the editoral code however. Even with my suggested fix (if it does work) your code is clearly less efficient than the editorial code because it scans the input strings repeatedly.
Got the correct answer just by changing pos=j+1 instead of pos=j. Then code will become similar to the pseudocode in the editorial. Thanks, everybody for answering.

Why am I getting a WA in Faded Palindromes in Codechef even though I found no error till now in my code and its working fine for me?

I have been debugging and seeing this code for nearly a week now still I couldn't find any solution and thus hoping to get one from the fellow developers in stack overflow.
This is the solution of the Faded Palindrome problem of September Challenge Codechef This is the problem page
#include<iostream>
#include<vector>
#include<iterator>
#include<stdlib.h>
#include<cstdio>
#include<string>
using namespace std;
string def="-1";
int main()
{
int t;
cin>>t;
//vector<string> out(t);
string out[100];
for(int i=0;i<t;i++)
{
string inp;
cin>>inp;
if(inp.size()%2) //Checking wether the number of letters is odd
{
int counter=inp.size()/2;
int counter_back=counter-1;
int counter_front=counter+1;
if(inp[counter]=='.')
inp[counter]='a';
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front=='.']){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
else{ //Checking the number of letter is even
//cout<<inp.size();
int counter_front=(inp.size())/2;
//cout<<counter_front;
int counter_back=counter_front-1;
//cout<<counter_back;
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{//cout<<"*-";
counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front]=='.'){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
//cout<<"--";
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
}
/*vector<string>::iterator itr;
for(itr=out.begin();itr!=out.end();itr++)
cout<<*itr<<endl;*/
for(int j=0;j<t;j++)
if(j==t-1)
cout<<out[j];
else
cout<<out[j]<<"\n";
return 0;
}
While your code may pass all the test cases presented in the problem statement, there are still more edge cases that your code doesn't cover. Here's a sample test case:
6
..
...
..e
a..a.v.
p..p
t.xzt
This should print:
aa
aaa
eae
avaaava
paap
tzxzt
instead it prints:
..
.a.
aaa
aa.a.aa
p..p
taxat
Hints:
The first if statement is not correctly handling the case where inp[b] and inp[f] are both '.'.
There is no need for doing two different things when |s| is even or odd.
Also, in the first else if statement, you probably meant inp[counter_front]=='.' instead of inp[counter_front=='.'].
if(inp[counter_back]==inp[counter_front]) is true if they both are equal to '.' so you won't fill that gap.

SPOJ: What is the difference between these two answers for KURUK14

I have solved this problem and got AC. My problem is related to equivalence of following two approaches. The first code got accepted, while the second didn't.
As far as I can discern, both are completely equivalent for all the (valid) test cases any human can think of. Am I wrong? If so, what test case can differentiate them?
Code#1 (Accepted one):
#include <cstdio>
bool* M;
bool proc(int N){
for(int j=0;j<=N;j++){
M[j]=false;
}
for(int i=0;i<N;i++){
int a=0;
scanf("%d",&a);
if(a>=N)
return false;
else if(!M[a])
M[a]=true;
else if(!M[N-1-a])
M[N-1-a]=true;
}
bool f = true;
for(int k=0;k<N;k++)
{
f = f && M[k];
}
return f;
}
int main() {
M=new bool[1002];
int num=0;
scanf("%d",&num);
while(num){
int N=0;
scanf("%d",&N);
if(proc(N))
printf("YES\n");
else
printf("NO\n");
num--;
}
return 0;
}
Code #2 (WA):
#include <cstdio>
bool* M;
bool proc(int N){
for(int j=0;j<=N;j++){
M[j]=false;
}
for(int i=0;i<N;i++){
int a=0;
scanf("%d",&a);
if(a>=N)
return false;
else if(!M[a])
M[a]=true;
else if(!M[N-1-a])
M[N-1-a]=true;
else
return false;
}
return true;
}
int main() {
//Exactly same as code#1
}
The bug has nothing to do with the algorithm itself—it's very possible both the algorithms are correct. But the second implementation is wrong.
When you reach a test case which should return NO, you exit the function prematurely. Which means there are some numbers from the current test case left unread in the input, which of course confuses further reading thoroughly. This means the bug only manifests when T > 1.

How can I fix a SPOJ time limit exceeded error on the STAMPS challenge?

Here is the link for the problem: http://www.spoj.com/problems/STAMPS/;
here is the ideone link for the current code: http://ideone.com/AcHfc6;
here is the code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,x,n,sum,sum2,count,i,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
cin>>x>>n;
sum=0;
int offer[n];
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
if(sum>=x)
{
sum2=0;
count=0;
for(i=n-1;i>=0;i--)
{
sum2+=offer[i];
if(sum2<=x)
count++;
else
break;
}
cout<<"Scenario #"<<j<<":"<<endl;
cout<<count<<endl;
cout<<endl;
}
else
{
cout<<"Scenario #"<<j<<":"<<endl;
cout<<"impossible"<<endl;
cout<<endl;
}
}
return 0;
}
The code gives the right answers for the given test cases but it causes TLE. I've tried converting my cin's and cout's to scanf/printf but, weirdly enough, the answers were not the same and I don't know how the answers were different from each other.
What's going wrong?
I suspect your main problem is here:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
You sort the data for every number that's entered. Also, you sort random data because you only have i rows of valid data in the array, not n rows. The sort should be done once, outside the loop:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
}
sort(offer,offer+n);

Curious behaviour of bitwise AND

I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
1==0 takes more precedence than pos&1. Try if((pos&1)==0){