解决方案 »

  1.   

    胡乱写了一个,要求应该可以达到,不过前提是
    1、原句中哪里有空格,输出就哪里有空格,并且原句几个输出就几个
    2、每个单词长度短(短指在char范围内,不会有长度几万的单词)
    StringBuilder sentence = new StringBuilder("This is an apple");
    char c;
    int p1, p2;

    p1 = sentence.length();

    while (p1 > 0) {
    p1--;
    p2 = p1;

    while (p1 >= 0 && sentence.charAt(p1) != ' ') {
    p1--;
    }

    c = (char) (p2 - p1);
    p2 = p1 + 1;

    while (p1 + c >= p2) {
    System.out.print(sentence.charAt(p2));
    p2++;
    }

    if (p1 >= 0) {
    System.out.print(' ');
    }
    }
      

  2.   

    public class Test {
    public static void main(String[] args) {
    StringBuilder sentence = new StringBuilder("This is an apple"); int p1 = sentence.length() - 1;
    int p2 = 1;
    char c; while (p1 >= 0) {
    c = sentence.charAt(p1);
    while (c != ' ' && p1 > 0) {
    c = sentence.charAt(--p1);
    }
    System.out.print(c);
    c = sentence.charAt(++p1); while (c != ' ' && p1 < sentence.length()) {
    c = sentence.charAt(p1++);
    System.out.print(c);
    ++p2;
    } p1 = p1 - p2 - 1;
    p2 = 1;
    }
    }
    }
      

  3.   

    用递归谢了一个public class Test001 {
    static char c = ' ';
    static StringBuilder sentence = new StringBuilder("This is an apple");
    static int p1 = sentence.length();
    static int p2 = 0;

    public static void main(String[] args) {
    do{
    p1 = getIndex(sentence ,p1,p2,c);
    } while( p1 != -10 );

    }

    public static int getIndex(StringBuilder sentence ,int p1,int p2,char c){
    if( sentence.substring(0, p1).lastIndexOf(c) > 0 ){
    p2 = sentence.substring(0, p1).lastIndexOf(c);
    System.out.print(sentence.substring(0, p1).substring(p2+1)+c);
    }else{
    System.out.print(sentence.substring(0, p1));
    p2 = -10;
    }
    return p2;
    }
    }
      

  4.   

    想用递归来着,不知不觉还是写成了while循环了
      

  5.   

    写个不对的。
     char c;int p1;int p2;
           System.out.println(sentence.toString().split("\\s+")[3]+" "+sentence.toString().split("\\s")[2]+" "
            +  sentence.toString().split("\\s+")[1]+" "+sentence.toString().split("\\s")[0]);
      

  6.   

    在有内存空间限制的情况下,递归就不合适了,2L,3L直接打印输出的方法应该是对的。另,题目有可能希望的是将sentence里面的内容从This is an apple转换为apple an is This,
    如果是这样的话,可以采用两次置换的方法,先将整个字符串颠倒,结果是
    elppa na si sihT
    然后再对每一个单词进行颠倒,结果是
    apple na is This
      

  7.   

    char c=' ';
    int p1=0,p2;
    StringBuilder sentence = new StringBuilder("This is an apple");
    p2=sentence.toString().length();
    while((p1=sentence.toString().lastIndexOf(" ",p2-1))!=-1){
    System.out.print(sentence.toString().substring(p1+1,p2)+c);
    p2=sentence.toString().lastIndexOf(" ",p1);
    if(sentence.toString().lastIndexOf(" ",p2-1)==-1){
    System.out.print(sentence.toString().substring(0,p2));
    }
    }
      

  8.   

    我来贴一个没有技术含量的p2 = sentence.length();
    while(true){
    p1 = 0;
    for(int j=0;j<p2;j++){
    c = sentence.charAt(j);
    if(c ==' '){
    p1 = j;
    }
    }
    if(p1!=0){
    System.out.print(sentence.substring(p1+1, p2)+" ");
    }else{
    System.out.print(sentence.substring(0, p2));
    break;
    }
    p2 = p1;
    }
      

  9.   

    先反向搜索空格,找到后再正向输出,因为只允许使用那三个变量嘛所以就用了点投机取巧的方式c = (char) p1;哈哈,反正效果是达到了。 StringBuilder sentence = new StringBuilder("This is an apple"); char c;
    int p1, p2; p1 = p2 = sentence.length() - 1; while (p1 >= 0) {
    c = sentence.charAt(p1);
    if (c == ' ' || p1 == 0) { if (p1 == 0) {
    System.out.print(sentence.charAt(p1));
    } c = (char) p1;
    while (p1 < p2) {
    System.out.print(sentence.charAt(p1 + 1));
    p1++;
    } p1 = c;
    p2 = p1 - 1; if (p1 > 0) {
    System.out.print(sentence.charAt(p1));
    } }
    p1--;
    }
      

  10.   

    按照空格切一下,切成了一个String数组,反向输出数组就ok了
      

  11.   

    如果StringBuilder.substring(int start, int end)方法里的new不算开辟内存的话
    只要两个int就行了。        StringBuilder sentence = new StringBuilder("This is an apple");
            int p1, p2;
            p1 = p2 = sentence.length();
            while (p1 >= 0) {
                if (p1 == 0 || sentence.charAt(p1 - 1) == ' ') {
                    System.out.print(sentence.substring(p1, p2));
                    if (p1 >= 1)
                        System.out.print(' ');
                    p2 = p1 - 1;
                }
                p1--;
            }
        }否则        StringBuilder sentence = new StringBuilder("This is an apple");
            int p1, p2;
            char c;
            p1 = p2 = sentence.length();
            while (p1 >= 0) {
                if (p1 == 0 || sentence.charAt(p1 - 1) == ' ') {
                    c = (char) p1;
                    while (p1 < p2) {
                        System.out.print(sentence.charAt(p1++));
                    }
                    p1 = c;
                    if (p1 >= 1)
                        System.out.print(' ');
                    p2 = p1 - 1;
                }
                p1--;
            }
        
      

  12.   

    System.out.println("apple an is This");
      

  13.   


    发现3楼自己写的代码,有个小bug,所以重新发个
    StringBuffer str = new StringBuffer("This is an apple"); int p1 = str.length() - 1;
    int p2;
    char c; while (p1 >= 0) {
    c = str.charAt(p1); while (c != ' ' && p1 > 0) {
    c = str.charAt(--p1);
    } if (c != ' ') {
    System.out.print(' ');
    } System.out.print(c); p2 = p1;
    c = str.charAt(++p2); while (c != ' ' && p2 < str.length()) {
    c = str.charAt(p2++); if (c != ' ') {
    System.out.print(c);
    }
    } p1--;
    }
      

  14.   

    substring肯定是不能用的
    这个题如果有C有基础, 算是入门题
      

  15.   

    substring 返回的是String, 是常量,必然要消耗内存