Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Genetics {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String parent1 = scanner.nextLine();
- String parent2 = scanner.nextLine();
- int split = Integer.parseInt(scanner.nextLine());
- String child1 = parent1.substring(0, split) + parent2.substring(split);
- System.out.println("child1 = " + child1);
- String child2 = parent2.substring(0, split) + parent1.substring(split);
- System.out.println("child2 = " + child2);
- int child1ones = 0, child2ones = 0;
- for (int i = 0; i < child1.length(); i++) {
- if (child1.charAt(i) == '1')
- child1ones++;
- if (child2.charAt(i) == '1')
- child2ones++;
- }
- double child1fit = (double) child1ones / child1.length();
- System.out.println("child1's fitness level: " + child1fit);
- double child2fit = (double) child2ones / child1.length();
- System.out.println("child2's fitness level: " + child2fit);
- boolean isSurvivor = false;
- String survivor = "";
- String survivorFlipped="";
- double survivorFit = 0.0;
- if (child1fit >= child2fit) {
- isSurvivor = true;
- survivor = child1;
- survivorFit = child1fit;
- // System.out.println("survivor is child1, with fitness level of " + child1fit);
- } else if (child2fit >= child1fit) {
- isSurvivor = true;
- survivor = child2;
- survivorFit=child2fit;
- // System.out.println("survivor is child2, with fitness level of " + child2fit);
- }
- if (survivorFit > 0.5){
- System.out.println(survivor);
- }else if (survivorFit < 0.5){
- for (int i = survivor.length()-1; i >=0 ; i--) {
- survivorFlipped += survivor.charAt(i);
- }
- System.out.println(survivorFlipped);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment