Monday, December 23, 2013

codechef CANDLE - "birthday candles" problem guidance and solution

codechef CANDLE - "birthday candles" problem: http://www.codechef.com/problems/CANDLE

editorial: http://discuss.codechef.com/questions/4212/candle-editorial

my c++ solution to codechef CANDLE - "birthday candles" problem: http://ideone.com/IdZCJD
#include <iostream>
using namespace std;

int main() {
// your code goes here
int t, arr[10], minVal, minVal2, minIndex, minIndex2;
cin>>t;
while(t--) {
minVal=15; minVal2=15; minIndex=-1; minIndex2=-1;
for(int i=0; i<10; i++) {
cin>>arr[i];
if(arr[i]<minVal) {
minVal=arr[i]; minIndex=i;
}
else if(arr[i]<minVal2 && i!=0) {
minVal2=arr[i]; minIndex2=i;
}
}
if(minIndex!=0) for(;minVal>=0; minVal--) cout<<minIndex;
else if(minVal==minVal2) for(;minVal2>=0; minVal2--) cout<<minIndex2;
else {
cout<<1; for(;minVal>=0; minVal--) cout<<minIndex;
}
cout<<endl;
}
return 0;
}

codechef CANDLE - "birthday candles" problem guidance: (or note on my code): if there was no zero index, or value for zero then we would not be having any confusion and it would be ok if we just printed out the minimum value's index. so lets take 2 minimum values and their indices. minVal and minIndex registers the minimum value and its index, while minVal2 and minIndex2 registers the minimum value other than 0(zero). if minVal's index(minIndex) is not zero then we just print out minIndex and that is ok. however, if minVal's index(minIndex) is zero, then we check if there any other value that equals to zero index's value(in this case, minVal) - checking process: if(minVal==minVal2), if yes, then print out minVal2's index(minIndex2). else if minVal is the least value in the array and index is 0(minIndex=0) then print out 1(because of decimal order. zero itself does not have any meaning) and then print 0 till zero index's value becomes lesser than zero.(for(;minVal>=0; minVal--) cout... stuff). thnx

No comments:

Post a Comment