Simple simulation game. Don’t forget to read through the entire input if the game ends early! That cost me a couple of tries :[
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * * @author Sanchit M. Bhatnagar * @see http://uhunt.felix-halim.net/id/74004 * */ public class P11459 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int cases = sc.nextInt(); while (cases > 0) { int players = sc.nextInt(); int cheats = sc.nextInt(); int rolls = sc.nextInt(); int[] map = new int[101]; for (int i = 0; i < cheats; i++) { map[sc.nextInt()] = sc.nextInt(); } int playerIdx = 0; int[] pos = new int[players]; Arrays.fill(pos, 1); for (int i = 0; i < rolls; i++) { pos[playerIdx] += sc.nextInt(); if (map[pos[playerIdx]] != 0) pos[playerIdx] = map[pos[playerIdx]]; if (pos[playerIdx] >= 100) { pos[playerIdx] = 100; while (i + 1 < rolls) { sc.nextInt(); i++; } break; } playerIdx++; if (playerIdx == players) playerIdx = 0; } for (int i = 0; i < players; i++) { out.println("Position of player " + (i + 1) + " is " + pos[i] + "."); } cases--; } out.close(); sc.close(); } }
hi my code look same like you except i used hashMap nested of array map but uva give me RunTime error do you why?
Can you share your code please? Would be able to debug your code better then. Thanks!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Administrator
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader r= new BufferedReader (new InputStreamReader (System.in));
int t=Integer.parseInt(r.readLine());
for(int i=0 ;i<t;i++){
String[] s =r.readLine().split((" "));
int players=Integer.parseInt(s[0]);
int play[]=new int [players];
for(int j=0 ;j<players;j++){
play[j]=1;}
int snake = Integer.parseInt(s[1]);
int rolls = Integer.parseInt(s[2]);
Map map =new HashMap();
for(int j=0 ;j<snake;j++){
String[] s1 =r.readLine().split(" ");
map.put(Integer.parseInt(s1[0]),Integer.parseInt(s1[1]));
}
int x=0;
for(int j=0 ;j=100) { play[x]=100;
while(j+1<rolls){
r.readLine();
j++;
} break;}
x++;
if(x==players) x=0;
}
int ii=0;
for(;ii<players;ii++){
System.out.printf("Position of player %d is %d.%n",(ii+1),play[ii]);
}
}
r.close();
}}
Well firstly use something like http://pastebin.com/ to share code so it is easy to read. Secondly it doesn’t seem like your code compiles. It could be a copy paste error, dunno. Can you post your code to pastebin please? This is the code as I see it right now. https://flic.kr/p/xF2Vn2 It does not compile and certain variables are out of scope.
http://pastebin.com/pbGm7CXF
This is the code