public static void main(String[] args) throws Exception { /* BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 2000); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out), 2000); String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int q = Integer.parseInt(s[1]); int num[] = new int[n + 1]; int[] m = new int[3 * n + 1]; // size = 2*n+1 Arrays.fill(num, -1); s = br.readLine().split(" "); for (int i = 1; i <= n; i++) num[i] = Integer.parseInt(s[i - 1]); /// build tree maketree(1, 1, n, m, num); for (int qq = 1; qq <= q; qq++) { s = br.readLine().split(" "); int i = Integer.parseInt(s[0]); int j = Integer.parseInt(s[1]); int ans = query(1, 1, n, m, num, i, j); out.write("" + num[ans] + "\n"); out.flush(); } }
public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); // byte digit[]=new byte[101]; // boolean[] check=new boolean[101]; // int num[]=new int[99999]; ans = 0; int tests = Integer.parseInt(br.readLine()); BigInteger f[] = new BigInteger[101]; for (int i = 1; i <= 100; i++) { f[i] = new BigInteger(("" + i)); } for (int i = 2; i <= 100; i++) { f[i] = f[i].multiply(f[i - 1]); } for (int i = 0; i < tests; i++) { int n = Integer.parseInt(br.readLine()); out.write("\n" + f[n].toString()); out.flush(); } }
public static void makeTextFile(String fwrite) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(fwrite)); int i, j; for (i = 65; i < 65 + FILESIZE; i++) { for (j = 0; j < PACKET_SIZE; j++) { writer.write((char) i); } } writer.close(); }
public static void main(String[] args) throws IOException { int num = 0; int guess = -1; int answer; String name; String line; String result; String scorefile = "highscores_java"; BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("guess.java - Guess a number between 1 and 100\n"); // generate random number java.util.Random generator = new java.util.Random(System.currentTimeMillis()); answer = generator.nextInt() / 42949673 + 50; // play game while (guess != answer) { num++; System.out.print("Enter guess " + num + ": "); result = stdin.readLine(); guess = Integer.parseInt(result); if (guess < answer) { System.out.println("Higher..."); } else if (guess > answer) { System.out.println("Lower..."); } } System.out.println("Correct! That took " + num + " guesses.\n"); // save high score System.out.print("Please enter your name: "); name = stdin.readLine(); try { BufferedWriter outfile = new BufferedWriter(new FileWriter(scorefile, true)); outfile.write(name + " " + num + "\n"); outfile.close(); } catch (IOException exception) { System.out.println("ERROR: Can't read from " + scorefile + "\n"); } // print high scores try { BufferedReader infile = new BufferedReader(new FileReader(scorefile)); while ((line = infile.readLine()) != null) { System.out.println(line); } infile.close(); } catch (IOException exception) { System.out.println("ERROR: Can't read from " + scorefile + "\n"); } }
public void run() { do { try { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); File file = new File("time.txt"); FileWriter fw = new FileWriter(file); Long start = 0l; Long end = 0l; BufferedWriter bw = new BufferedWriter(fw); System.out.println("Enter the preferred choice"); System.out.println("1. REGISTER"); System.out.println("2. LEAVE"); System.out.println("3. SEARCH FOR RFC"); System.out.println("4. KEEPALIVE"); System.out.println("5. Do you want to EXIT"); System.out.println("*********************************************"); choice = Integer.parseInt(inFromUser.readLine()); // System.out.println(" Client requesting for connection"); clientSocket = new Socket("192.168.15.103", 6500); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()), true); // outToServer.println("Peer Requesting Connection"); switch (choice) { case 4: outToServer.println( "KEEP ALIVE P2P-DI Cookieno " + cookie + " OS: " + System.getProperty("os.name") + " " + "v" + System.getProperty("os.version") + " USER: "******"user.name")); System.out.println(inFromServer.readLine()); System.out.println("*********************************************"); break; case 1: try { // System.out.println("Case 1 entered"); // testing // statement System.out.println("Please enter your IP addres"); ip = inFromUser.readLine(); outToServer.println( "REG P2P-DI/1.1 -1 Portno 6789 Hostname " + ip + " OS: " + System.getProperty("os.name") + " " + "v" + System.getProperty("os.version") + " USER: "******"user.name")); cookie = Integer.parseInt(inFromServer.readLine()); TTL = 7200; System.out.println(inFromServer.readLine()); System.out.print("You are registered at time : "); ct.currenttime(); System.out.println("Peer " + cookie); // peer cookie value System.out.println("TTL Value :" + TTL); System.out.println("*********************************************"); break; } catch (Exception e) { } case 2: // System.out.println("Case 2 entered"); testing statement outToServer.println( "LEAVE P2P-DI Cookieno " + cookie + " OS: " + System.getProperty("os.name") + " " + "v" + System.getProperty("os.version") + " USER: "******"user.name")); // System.out.println("I am in peer"); testing statement System.out.println(inFromServer.readLine()); System.out.println("*********************************************"); break; case 3: outToServer.println( "PQUERY P2P-DI Cookieno " + cookie + " Portno 6789" + " OS: " + System.getProperty("os.name") + " " + "v" + System.getProperty("os.version") + " USER: "******"user.name")); System.out.println("Which RFC number do you wish to have ?"); reqrfc = Integer.parseInt(inFromUser.readLine()); // System.out.println("Entered peer again"); // outToServer.println("KEEP ALIVE cookieno "+cookie); String details = inFromServer.readLine(); String[] parray = details.split(" "); int inactive = Integer.parseInt(parray[(parray.length - 1)]); // System.out.println(darray.length); if ((parray.length == 3) && (ip.equals(parray[1]))) { System.out.println("P2P-DI No Active Peers available"); System.out.println("*********************************************"); } else { System.out.println("****<POPULATING THE ACTIVE PEER LIST>****"); System.out.println(); System.out.println("The active peer list is as follows:"); System.out.println(); String[] darray = details.split(" "); // System.out.println("Array length"+darray.length); for (int i = 0; i < (darray.length - 2); i = i + 2) { acthostname[j] = darray[i + 1]; System.out.println("Hostname :" + acthostname[j]); actportno[j] = Integer.parseInt(darray[i + 2]); System.out.println("Portno :" + actportno[j]); System.out.println("*****************************"); j = j + 1; } System.out.println("Connecting to the active peers for its RFC Index"); for (int x = 0; x < j; x++) { // System.out.println(ip); if (!(acthostname[x].equals(ip))) { System.out.println("Connecting to " + acthostname[x]); Socket peersocket = new Socket(acthostname[x], 6791); // implement // a for // loop BufferedReader inFromPeer = new BufferedReader(new InputStreamReader(peersocket.getInputStream())); PrintWriter outToPeer = new PrintWriter(new OutputStreamWriter(peersocket.getOutputStream()), true); outToPeer.println("RFCIndex"); // System.out.println(inFromServer.readLine()); // int searchrfc=Integer.parseInt(inFromUser.readLine()); // outToServer.println(searchrfc); String rfcindex = inFromPeer.readLine(); // tell server to // send rfc in // string String rfcarray[] = rfcindex.split(" "); // System.out.println(rfcindex); for (int i = 1; i < rfcarray.length; i = i + 4) { trfcno[z] = Integer.parseInt(rfcarray[i]); // System.out.println("RFC number " + trfcno[z]); trfctitle[z] = rfcarray[i + 1]; // System.out.println("RFC Title " + trfctitle[z]); tpeername[z] = rfcarray[i + 2]; // System.out.println("Peer Ip Address " + tpeername[z]); tpTTL[z] = Integer.parseInt(rfcarray[i + 3]); // System.out.println("TTL value :" + tpTTL[z]); counter1 = counter1 + 1; z = z + 1; } z = 0; // if(arraybound==0) // { System.arraycopy(trfcno, 0, rfcno, counter2, trfcno.length); System.arraycopy(trfctitle, 0, rfctitle, counter2, trfctitle.length); System.arraycopy(tpeername, 0, peername, counter2, tpeername.length); System.arraycopy(tpTTL, 0, pTTL, counter2, tpTTL.length); z = 0; counter2 = counter1; counter1 = 0; // arraybound=arraybound+1; // } System.out.println(); System.out.println(); System.out.println("*************************************************"); System.out.println("RFC Index received from the Peer"); // System.out.println(); System.out.println("\n-----------------------------------------"); System.out.println("RFC Index System - Display RFC Idex"); System.out.println("-------------------------------------------"); System.out.format( "%10s%15s%15s%10s", "RFC No", "RFC Title", "Peer Name", "TTL Value"); System.out.println(); // StudentNode current = top; // while (current != null){ // Student read = current.getStudentNode(); for (int i = 0; i < 60; i++) { System.out.format( "%10s%15s%15s%10s", " " + rfcno[i], rfctitle[i], peername[i], " " + pTTL[i]); System.out.println(); } // This will output with a set number of character spaces // per field, giving the list a table-like quality // } peersocket.close(); } // end of if for (int i = 0; i < rfcno.length; i++) { if (rfcno[i] == reqrfc) { String taddress = InetAddress.getByName(peername[i]).toString(); String[] taddr = taddress.split("/"); InetAddress tproperaddress = InetAddress.getByName(taddr[1]); // System.out.println("Inetaddress" + tproperaddress); Socket peersocket1 = new Socket(tproperaddress, 6791); // implement // a // for // loop System.out.println("The connection to the Active Peer is establshed"); BufferedReader inFromP2P = new BufferedReader(new InputStreamReader(peersocket1.getInputStream())); PrintWriter outToP2P = new PrintWriter( new OutputStreamWriter(peersocket1.getOutputStream()), true); System.out.println("Requested the RFC to the Active Peer Server"); start = System.currentTimeMillis(); outToP2P.println("GETRFC " + reqrfc); // Socket socket = ; try { // Socket socket = null; InputStream is = null; FileOutputStream fos = null; BufferedOutputStream bos = null; int bufferSize = 0; try { is = peersocket1.getInputStream(); bufferSize = 64; // System.out.println("Buffer size: " + bufferSize); } catch (IOException ex) { System.out.println("Can't get socket input stream. "); } try { fos = new FileOutputStream("E:\\rfc" + reqrfc + "copy.txt"); bos = new BufferedOutputStream(fos); } catch (FileNotFoundException ex) { System.out.println("File not found. "); } byte[] bytes = new byte[bufferSize]; int count; while ((count = is.read(bytes)) > 0) { // System.out.println(count); bos.write(bytes, 0, count); } System.out.println("P2P-DI 200 OK The RFC is copied"); end = System.currentTimeMillis(); System.out.println( "Total Time to download file " + (end - start) + " milliseconds"); bos.flush(); bos.close(); is.close(); peersocket1.close(); break; } catch (SocketException e) { System.out.println("Socket exception"); } } // end of if else { // System.out.println("No Peer with the required RFC could be found"); } clientSocket.close(); // System.out.println("Connection closed"); bw.close(); fw.close(); } // end of inner for } // end of outer for System.out.println("Connection closed"); } // end of switch } // end of else which checks the inactive conditions } // end of try catch (IOException ioe) { System.out.println("IOException on socket listen: " + ioe); ioe.printStackTrace(); } } // end of do while (choice != 5); } // end of run
public boolean test() { ExtendedService es = null; boolean ok = true; String tmpfilename = getParameter("tmpfile"); try { // Lookup the javax.jnlp.ExtendedService object es = (ExtendedService) ServiceManager.lookup("javax.jnlp.ExtendedService"); } catch (UnavailableServiceException ue) { System.out.println(ue); ue.printStackTrace(); // Service is not supported ok = false; } if (!ok) return false; // Open a specific file in the local machine File tmpfile = new File(tmpfilename); // Java Web Start will pop up a dialog asking the user to grant permission // to read/write the file 'tempfile' try { FileContents fc_tmpfile = es.openFile(tmpfile); if (!tmpfilename.equals(fc_tmpfile.getName())) { System.out.println( "\t tmpfile(out): " + tmpfilename + ", unequal to fc-filename: " + fc_tmpfile.getName() + " - info"); // ok=false; // return ok; } if (!fc_tmpfile.canWrite()) { System.out.println( "\t outfile: " + tmpfilename + ", no write access (may not exist yet) - info"); } // You can now use the FileContents object to read/write the file java.io.OutputStream sout = fc_tmpfile.getOutputStream(true); BufferedWriter bwsout = new BufferedWriter(new OutputStreamWriter(sout)); bwsout.write(datum, 0, datum.length()); bwsout.flush(); bwsout.close(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); System.out.println("\t Error while IO write - failed"); ok = false; return ok; } // read back .. try { FileContents fc_tmpfile = es.openFile(tmpfile); if (!tmpfilename.equals(fc_tmpfile.getName())) { System.out.println( "\t tmpfile(in): " + tmpfilename + ", unequal to fc-filename: " + fc_tmpfile.getName() + " - info"); // ok=false; // return ok; } if (!fc_tmpfile.canRead()) { System.out.println("\t outfile: " + tmpfilename + ", read access failed"); ok = false; } // You can now use the FileContents object to read/write the file java.io.InputStream sin = fc_tmpfile.getInputStream(); BufferedReader brsin = new BufferedReader(new InputStreamReader(sin)); String in = brsin.readLine(); if (!in.equals(datum)) { System.out.println("\t file content <" + in + "> does not match <" + datum + "> - failed"); ok = false; } brsin.close(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); System.out.println("\t Error while IO read - failed"); ok = false; return ok; } return ok; }