Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package main_pack;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Arras {
  6.  
  7. int length;
  8. int[] array;
  9.  
  10. //класс создаем
  11. Arras(int length){
  12. this.length = length;
  13. array = new int[this.length];
  14. }
  15.  
  16. // ввод ручками
  17. public void mInput(){
  18. java.util.Scanner inp = new java.util.Scanner(System.in);
  19. System.out.println("Вводите числа массива: ");
  20. for (int i = 0; i<array.length; i++){
  21. array[i] = inp.nextInt();
  22. }
  23. }
  24.  
  25. //рандомный ввод
  26. public void rInput(){
  27. for (int i=0; i<length; i++){
  28. array[i] = 1 + (int)(Math.random() * 1000);
  29. }
  30. }
  31.  
  32. //вывод слева направо
  33. public void lOut(){
  34. for(int i = 0; i < length; i++){
  35. System.out.print(array[i] + " ");
  36. }
  37. System.out.println("\n");
  38. }
  39.  
  40. //вывод справа налево
  41. public void rOut(){
  42. for (int i = length-1; i>=0; i--){
  43. System.out.print(array[i] + " ");
  44. }
  45. System.out.println("\n");
  46. }
  47.  
  48. //Task_1
  49. public void task_1(){
  50. System.out.println("Введите необхаодимое число: ");
  51. java.util.Scanner inp = new java.util.Scanner(System.in);
  52. int count = inp.nextInt();
  53. for (int i=0; i<this.length;i+=2){
  54. array[i]-=count;
  55. }
  56. System.out.println("Задание выполнено!");
  57. }
  58. private boolean Check_one(int numb) {
  59. int onlyone = 0;
  60. while (numb >0){
  61. if (numb%2==1)
  62. onlyone++;
  63. numb/=2;
  64. if (onlyone>3){
  65. return true;
  66. }
  67. }
  68. if (onlyone>3)
  69. return true;
  70. return false;
  71. }
  72. public String task_2(){
  73. //сохраняем в лист
  74. ArrayList <Integer> numb = new ArrayList();
  75. int count =0;
  76. for (int i : array){
  77. if (Check_one(i)) {
  78. numb.add(i);
  79. count++;
  80. }
  81. }
  82. //перезапись
  83. int [] true_num = new int[count];
  84. int t = 0;
  85. for (int i : numb){
  86. true_num[t] = i;
  87. t++;
  88. }
  89. String str = "";
  90. for (int i=0; i<true_num.length; i++){
  91. str += true_num[i];
  92. str += " ";
  93. }
  94. return str;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement