problem code:COLGLF4
i can’t understand what i am doing wrong in this code.i have tried to match the editorial with code four times
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
// your code goes here
int t;
cin>>t;
while(t--)
{
int n,e,h,omprice,shakePrice,cakePrice;
cin>>n>>e>>h>>omprice>>shakePrice>>cakePrice;
int ans=1e18;
for(int cake=0;cake<=n;cake++)
{
if(e<cake||h<cake)
{
break;
}
int om=(e-cake)/2;
int shake=(h-cake)/3;
if(cake+om+shake<n)
continue;
int omreq,shakereq;
if(shakePrice>omprice)
{
omreq=min(n-cake,om);
shakereq=n-cake-omreq;
}
if(shakePrice<omprice)
{
shakereq=min(n-cake,shake);
omreq=n-cake-shakereq;
}
int price=(shakereq*shakePrice) + (omreq*omprice)+(cake*cakePrice);
ans=min(price,ans);
}
if(ans==1e18)
ans=-1;
cout<<ans<<endl;
}
return 0;
}
Pay attention to compiler warnings (the last two in particular):
Compiling abhinav_700-COLGLF4.cpp
+ g++ -std=c++14 abhinav_700-COLGLF4.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
abhinav_700-COLGLF4.cpp: In function ‘int main()’:
abhinav_700-COLGLF4.cpp:40:13: warning: conversion to ‘double’ from ‘long long int’ may alter its value [-Wconversion]
if(ans==1e18)
^~~~
abhinav_700-COLGLF4.cpp:37:29: warning: ‘shakereq’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int price=(shakereq*shakePrice) + (omreq*omprice)+(cake*cakePrice);
~~~~~~~~~^~~~~~~~~~~~
abhinav_700-COLGLF4.cpp:37:50: warning: ‘omreq’ may be used uninitialized in this function [-Wmaybe-uninitialized]
int price=(shakereq*shakePrice) + (omreq*omprice)+(cake*cakePrice);
~~~~~~^~~~~~~~~
+ set +x
Successful
I’d recommend using g++ to compile your code locally, it will give you all the necessary details, I didn’t have a decent experience using online compilers (there probably is an online workaround that I’m not aware of).
From what I can see in your code, you’re missing a simple case when doing if statements so you leave two variables uninitialized, but I didn’t analyze your code too much, so that may not be the only mistake.