Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package zadanie17;
  2. import java.util.Scanner;
  3. public class mass {
  4.  
  5. public static int getUnEven(String st) {
  6. int count = 0;
  7. for(int i=0; i<st.length(); i++) {
  8. if (Character.isDigit(st.charAt(i)))
  9. if (st.charAt(i) % 2 != 0)
  10. count++;
  11. }
  12. return count;
  13. }
  14.  
  15. public static void swap(String[] st, int n) {
  16. int[] C = new int[n];
  17. for(int i=0; i<n; i++)
  18. C[i] = getUnEven(st[i]);
  19. int maxIndex = 0;
  20. int max = C[maxIndex];
  21. int minIndex = n-1;
  22. int min = C[minIndex];
  23. for(int i=0; i<n; i++) {
  24. if (C[i]>max) {
  25. maxIndex = i;
  26. max = C[maxIndex];
  27. }
  28. if (C[i]<min) {
  29. minIndex = i;
  30. min = C[minIndex];
  31. }
  32.  
  33. }
  34.  
  35. String temp = st[maxIndex];
  36. st[maxIndex] = st[minIndex];
  37. st[minIndex] = temp;
  38.  
  39. }
  40.  
  41. public static void printMas(String[] st) {
  42. for(int i=0; i<st.length; i++) {
  43. System.out.print(st[i] + " ");
  44. }
  45. System.out.println();
  46. }
  47.  
  48. public static void main(String[] args) {
  49. // TODO Auto-generated method stub
  50. Scanner in=new Scanner(System.in);
  51. int n = in.nextInt();
  52.  
  53. String[] s = new String[n];
  54.  
  55. for(int i=0; i<s.length; i++) {
  56. s[i]=in.next();
  57. }
  58.  
  59. printMas(s);
  60.  
  61. swap(s,n);
  62.  
  63. printMas(s);
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement