MagdalenaTakeva

Genetics

Mar 17th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Genetics {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String parent1 = scanner.nextLine();
  7. String parent2 = scanner.nextLine();
  8. int split = Integer.parseInt(scanner.nextLine());
  9.  
  10.  
  11. String child1 = parent1.substring(0, split) + parent2.substring(split);
  12. System.out.println("child1 = " + child1);
  13. String child2 = parent2.substring(0, split) + parent1.substring(split);
  14. System.out.println("child2 = " + child2);
  15.  
  16. int child1ones = 0, child2ones = 0;
  17. for (int i = 0; i < child1.length(); i++) {
  18. if (child1.charAt(i) == '1')
  19. child1ones++;
  20. if (child2.charAt(i) == '1')
  21. child2ones++;
  22. }
  23.  
  24. double child1fit = (double) child1ones / child1.length();
  25. System.out.println("child1's fitness level: " + child1fit);
  26. double child2fit = (double) child2ones / child1.length();
  27. System.out.println("child2's fitness level: " + child2fit);
  28.  
  29.  
  30. boolean isSurvivor = false;
  31. String survivor = "";
  32. String survivorFlipped="";
  33. double survivorFit = 0.0;
  34.  
  35. if (child1fit >= child2fit) {
  36. isSurvivor = true;
  37. survivor = child1;
  38. survivorFit = child1fit;
  39. // System.out.println("survivor is child1, with fitness level of " + child1fit);
  40.  
  41. } else if (child2fit >= child1fit) {
  42. isSurvivor = true;
  43. survivor = child2;
  44. survivorFit=child2fit;
  45. // System.out.println("survivor is child2, with fitness level of " + child2fit);
  46.  
  47. }
  48.  
  49. if (survivorFit > 0.5){
  50. System.out.println(survivor);
  51. }else if (survivorFit < 0.5){
  52. for (int i = survivor.length()-1; i >=0 ; i--) {
  53. survivorFlipped += survivor.charAt(i);
  54. }
  55.  
  56. System.out.println(survivorFlipped);
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment