My issue
please help me in solving this question.
It wil be great if you debug the code below.
My code
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n,flag=0 ;
cin>>n ;
for(int i=0;i<n;i++)
{
long long h,x,y ;
cin>>h>>x>>y ;
if((h+y)<x)
flag=1 ;
else {
while((h+y)>x)
{
long long c=h+y;
long long c1=c ;
h=c-x ;
c1+=y ;
if((h+y)>x && c1>=h)
{
flag=0 ;
break ;
}
else
{
flag=1 ;
}
}
}
if(flag==1)
cout<<1<<endl ;
else if(flag==0)
cout<<0<<endl ;
}
}
Problem Link: CodeChef: Practical coding for everyone
you forgot to include ***The first line of the input contains a single integer ***
�
***T - the number of test cases. The description of ***
�
T test cases follows.
1 Like
my mind is not able to trace it sorry!
but instead i am giving you best debugging tool
trace function
to use it watch this video
[trace function]( How to code faster with Templates and Macros | What is trace function ? - YouTube)
#include <bits/stdc++.h>
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << endl;
//use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
int main() {
// int t;
//while(t--){
int n,flag=0 ;
cin>>n ;
for(int i=0;i<n;i++)
{
long long h,x,y ;
cin>>h>>x>>y ;
if((h+y)<x)
flag=1 ;
else {
while((h+y)>x)
{
long long c=h+y;
long long c1=c ;
h=c-x ;trace(h);
c1+=y ;
trace(y,c1);
if((h+y)>x && c1>=h)
{
flag=0 ;
break ;
}
else
{
flag=1 ; trace(flag);
}
}
}
if(flag==1){
cout<<1<<endl;
trace(flag);
}
if (flag==0){
cout<<0<<endl ;
trace(flag);
}
}
//}
return 0;
}
1 Like
I dont know why your code is so big, but you just have to check if y >= x is true or not.
So to understand the question look at the question like this, you start with ‘h’ health points.
After every hit scene, you get (y - x) added to your health ‘h’.
(because y is added to your health but you lose x from your health)
So after every hit, you new health, h = h + (y - x).
Now if you calculate (y - x), if it is positive, then after some amount of hits, monster is bound to have positive health.
Suppose if h = -4 and (y - x) is 2. h = -4 + 2 + 2 + 2 + …
and h becomes positive after some hits.
(but h has minimum value 1 according to constraints so no negative case)
Suppose h = 10 and (y - x) is -2. then h = 10 -2 -2 -2 -2 -2 …
and h becomes negative after some hits.
So in short if (y - x) is positive, health h is always positive and you cant defeat the monster.
if (y - x) is negative, health h becomes always negative and you can defeat the monster.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve() {
unsigned long long h, x, y;
cin >> h >> x >> y;
if (y >= x)
cout << 0 << '\n';
else
cout << 1 << '\n';
}
int main() {
int tc;
cin >> tc;
while(tc --) {
solve();
}
return 0;
}
2 Likes