Jashanpreet is right. *h* is the counter for the number of yogurts you had for a day. *max* is the counter for the total number of yogurts you had. *h* should be incremented when and only when *max* is incremented.
Jashanpreet Singh <[email protected]>于2018年9月19日周三 下午1:38写道: > In the code snippet: > * for(int h=1;h<=k;h++)* > > > > > * { if(index==n){break;} > if(g<a[index]){max++;index++;}* > > * else{index++;} //break;REMOVED > }* > > `h` should be incremented only when the loop enters the second `if` block. > > On Thu, Sep 20, 2018 at 1:36 AM Anamika Mathur <[email protected]> > wrote: > >> Thanku so much again for taking the time and effort. >> >> I think to correct my solution I just need to remove the break statement >> in the else condition to continue checking the next yogurt till full. >> >> I did do that and am still getting incorrect answer. >> >> import java.io.*; >> import java.lang.*; >> import java.util.Scanner; >> import java.util.Arrays; >> class A >> { >> public static void main(String args[])throws IOException >> { >> >> Scanner input = new Scanner(System.in); >> int t = input.nextInt(); >> int x; >> for(x=1;x<=t;x++){ >> >> int n = input.nextInt(); >> int k = input.nextInt(); >> int a[]=new int[n]; >> int i; >> for(i=0;i<n;i++) >> a[i] = input.nextInt(); >> Arrays.sort(a); >> >> int max=0,index=0; >> for(int g=0;g<a[n-1];g++){ >> if(index==n){break;} >> >> for(int h=1;h<=k;h++){ >> if(index==n){break;} >> >> if(g<a[index]){max++;index++;} >> >> /* You are trying to do greedy for picking yogurt everyday >> >> Your algorithm: >> 1. Sort all yogurt by their expiry day. >> 2. Each day, until I am full, I check the next yogurt >> 2.1 If the yogurt is not expired, I eat them >> 2.2 If the yogurt is expired, I throw it away and end this >> day. >> >> The mistakes happen at 2.2 >> Instead of ending this day, you should keep checking >> the next yogurt until you are full.*/ >> >> else{index++;} //break;REMOVED >> } >> >> } >> System.out.println("Case #"+x+": "+max); >> } >> >> } >> } >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Google Code Jam" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/google-code/2ffde858-b1a2-4d83-b839-1cf7d16d8bef%40googlegroups.com >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- > You received this message because you are subscribed to the Google Groups > "Google Code Jam" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-code/CACmYGGQ_GcjUj8jRGS_nPUzo%2B26QgFS%3DFe1Q6KdCJv6PzvO-WQ%40mail.gmail.com > <https://groups.google.com/d/msgid/google-code/CACmYGGQ_GcjUj8jRGS_nPUzo%2B26QgFS%3DFe1Q6KdCJv6PzvO-WQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/google-code/CAGDEU-JbY5ZYeev421FuzL1-NDmwh%3DD3Pt%2BsAiGONtKj53cuVg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
