El martes, 3 de diciembre de 2013 00:37:29 UTC-6, narsi.nallamilli escribió: > On Saturday, November 16, 2013 10:53:07 AM UTC+5:30, George R wrote: > > Hello > > > > I am a beginner in competitive programming, I having a hard time to solve > > this problem https://icpcarchive.ecs.baylor.edu/external/40/4071.pdf, it is > > an easy exercise but I am stuck, any hint to solve this problem? > > This is my code so far, I am trying to do the simulation with a while loop > > but It is not working because sometimes I get the correct answer, can > > someone guide me? maybe I am totally lost > > > > import java.io.*; > > class Main2 { > > public static void main(String[] args) throws IOException { > > BufferedReader bf = new BufferedReader(new > > InputStreamReader(System.in)); > > int t = Integer.parseInt(bf.readLine()); > > String n = ""; > > String nameChosen = ""; > > int gum = 0; > > > > for(int i = 0; i< t; i++){ > > n = bf.readLine(); > > nameChosen =bf.readLine();; > > gum = Integer.parseInt(bf.readLine()); > > solve(n,nameChosen, gum); > > } > > > > } > > > > public static void solve(String arr,String name, int n){ > > String[] names = arr.split(" "); > > int pos = 0; > > for(int j= 0; j< names.length;j++){ > > if(names[j].equals(name)){ > > pos = j; > > } > > } > > int k = 0; > > String nom = ""; > > System.out.println("pos " + pos); > > System.out.println("length " + names.length); > > > > while(n >= 0){ > > if(pos > names.length -1){ > > pos = 0; > > nom = names[pos]; > > }else{ > > nom = names[pos]; > > System.out.println("nom " + nom); > > pos++; > > } > > n--; > > } > > System.out.println(nom); > > } > > } > > import java.io.FileReader; > import java.io.BufferedReader; > import java.util.StringTokenizer; > import java.util.ArrayList; > import java.util.List; > > public class BubbleGumMain{ > public static void main(String... args){ > FileReader fr = new FileReader("BubbleGumGame_Input.txt"); > BufferedReader br = new BufferedReader(fr); > String ln = null; > int i = 1; > int testCases = 0; > int start = 0; > int count = 0; > int mod = 0; > int namesLength = 0; > List<String> names = null; > while((ln=br.readline())!=null){ > switch(i){ > case 1:testCases = Integer.valueOf(ln); > i=2; > break; > case 2:StringTokenizer st = new StringTokenizer(ln); > namesLength = st.countTokens(); > names = new ArrayList<String>(namesLength); > while(st.hasMoreTokens()){ > names.add(st.nextToken()); > } > i=3; > break; > case 3:start = names.indexOf(ln); > i=4; > break; > case 4:count = Integer.valueOf(ln)-1; > i=2; > mod = countf%namesLength; > System.out.println(names.get(mod+start)); > break; > } > } > } > }
thanks a lot @narsi.nallamilli now I get the formula, I think that I was in the right direction. 1. Find the index of the name = indexUser. 2. Subtract 1 to n, so n = n -1, so there will be no problem with the arrays or lists that start with index 0. 3. The formula is (n-1) % array.length or list.size + indexUser. Thanks a lot -- 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/c29bd52d-038b-41b4-96db-6c17de6e4904%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
