Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public class DU1 {
  2.  
  3. public static void main(String[] args) {
  4. System.out.println("JEHO: " + najvacsia(845141474));
  5. System.out.println("MOJE1: " + najvacsiaMoja(845141474));
  6. System.out.println("MOJE2: " + najvacsiaMoja2(845141474));
  7. }
  8.  
  9. public static int najvacsiaMoja2(int n) {
  10. return Integer.parseInt("1" + new String(new char[Integer.toBinaryString(n).length()-1]).replace("\0", "0"), 2);
  11. }
  12.  
  13. public static int najvacsiaMoja(int n) {
  14. int minca = 1;
  15. while (minca < n) minca *= 2;
  16. if (minca > n) minca /= 2;
  17. return minca;
  18. }
  19.  
  20. public static int najvacsia(int n) {
  21. int vysledok = 0;
  22. int kitevo = 0;
  23. int pom = n;
  24. while (Math.pow(2, kitevo) < n) {
  25. kitevo++;
  26. }
  27. if (Math.pow(2, kitevo) > n) {
  28. kitevo--;
  29. }
  30. while (kitevo >= 0) {
  31. if (Math.pow(2, kitevo) <= pom) {
  32. if (Math.pow(2, kitevo) > vysledok) {
  33. vysledok = (int)Math.pow(2, kitevo);
  34. }
  35. pom -= Math.pow(2, kitevo);
  36. kitevo--;
  37. }
  38. else {
  39. kitevo--;
  40. }
  41. }
  42. return vysledok;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement