将字符串拆分为三个部分并将递归应用于flip:java

时间:2017-09-26 02:47:33

标签: java

我正在尝试将字符串分成三部分并向后打印每个部分。因此,当用户输入" HelloWorld"它应该打印" dlroWolleH"。我不知道我错在哪里,它像对子一样翻转。

import java.util.*;
public class recursion
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner (System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();
        System.out.print(revRec3(str));
    }

    public static String revRec3(String str)
    {
        if ((str.length() == 1)|| (str.length()==0 || str.length()==2))
        {
            return str;
        }

        String left = str.substring(0, (str.length() / 3)),
        middle = str.substring((str.length()/3) ,(str.length()-left.length())),
        right = str.substring((str.length()-middle.length()),str.length());
        String revLeft = revRec3(left);
        String revMiddle = revRec3(middle);
        String revRight = revRec3(right);

        return revRight + revMiddle + revLeft;
    }        
}

4 个答案:

答案 0 :(得分:0)

也许你正在努力实现这个目标

import java.util.Scanner;

public class Main {

  public static void main(String args[])
  {
    Scanner sc = new Scanner (System.in);
    System.out.print("Enter a string: ");
    String str = sc.next();
    System.out.print(revRec3(str));
  }



public static String revRec3(String str)
  {
    if ((str.length() == 1)|| (str.length()==0 || str.length()==2))
    {
  return str;
}

String left = str.substring(0, (str.length() / 3)),
    middle = str.substring((str.length()/3) ,(str.length()-left.length())),
    right = str.substring((str.length()-middle.length()+1),str.length());
String revLeft = revRec(left);
String revMiddle = revRec(middle);
String revRight = revRec(right);
return revRight + revMiddle + revLeft;


 }



private static String revRec(String middle) {
    StringBuilder sb = new StringBuilder(middle);
   return sb.reverse().toString();
  }
}

答案 1 :(得分:0)

你有一个基本案例,它将字符串保持不变:

str.length()==2

在这种情况下,您需要在返回字符串之前将其反转。

答案 2 :(得分:0)

我相信这就是你想要的。

public static void main(String args[])
{
        Scanner scanner = new Scanner (System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();
        System.out.print(threeSplitString(str));
}

public static String threeSplitString(String str)
{
    if (str.length() < 3)
    {
        return str;
    }
    int len = str.length();

    String p1 = str.substring(len * 0 / 3, len * 1 / 3); // First Third
    String p2 = str.substring(len * 1 / 3, len * 2 / 3); // Second Third
    String p3 = str.substring(len * 2 / 3, len * 3 / 3); // Third Third

    // The recursion
    p1 = threeSplitString(p1);
    p2 = threeSplitString(p2);
    p3 = threeSplitString(p3);

    return p3 + p2 + p1;
}

答案 3 :(得分:0)

这是我的javascript代码,希望它有用。

function rev(input) {
  let len = input.length;
  if(len < 2) return input;
  if(len == 2) return input[1]+input[0];
  let left = input.substring(0, Math.round(len/3))
  let mid = input.substring(Math.round(len/3), Math.round(len-len/3))
  let right = input.substring(Math.round(len-len/3), len)

  return rev(right) + rev(mid) + rev(left);
}

但我不知道为什么我们需要分成3部分,我们已经做了2部分

function rev(input) {
  let len = input.length;
  if(len < 2) return input;
  if(len == 2) return input[1]+input[0];
  let left = input.substring(0, Math.round(len/2));
  let right = input.substring(Math.round(len/2), len);

  return rev(right) + rev(left);
}