fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. int rtn = openedRoomNo(5);
  14. System.out.println("return value is : " + rtn);
  15. }
  16.  
  17.  
  18. static int openedRoomNo(int rooms) {
  19. int rtnVal = 0;
  20. //set default arr
  21. int[][] roomsArr = new int[rooms][2];
  22. int cnt = 0;
  23. for(int[] room : roomsArr) {
  24. room[0] = ++cnt;
  25. room[1] = 1;
  26. }
  27. rtnVal = loopAtAddedCount(2, roomsArr);
  28. return rtnVal;
  29.  
  30.  
  31. }
  32.  
  33. static int loopAtAddedCount (int divNo, int[][] rooms) {
  34. int loopCnt = 0;
  35.  
  36. if(divNo > rooms.length) {
  37. for(int[] room : rooms) {
  38. if(room[1] == 1) {
  39. System.out.println("after room[0] is " +room[0]);
  40. loopCnt ++;
  41. }
  42. }
  43. return loopCnt;
  44. }
  45.  
  46. for(int [] room : rooms) {
  47. for(int i = 1 ; divNo * i <= rooms.length ; i++) {
  48. if(room[0] % (divNo * i) == 0) {
  49. System.out.println("divNo is " +divNo + "changed room no " +room[0]);
  50. if(room[1] == 0) {
  51. room[1] = 1;
  52. }else {
  53. room[1] = 0;
  54. }
  55. }
  56. }
  57. }
  58. divNo +=1;
  59. return loopAtAddedCount(divNo , rooms);
  60. }
  61. }
Success #stdin #stdout 0.11s 36296KB
stdin
Standard input is empty
stdout
divNo is 2changed room no 2
divNo is 2changed room no 4
divNo is 2changed room no 4
divNo is 3changed room no 3
divNo is 4changed room no 4
divNo is 5changed room no 5
after room[0] is 1
return value is : 1