本帖最后由 java2000_net 于 2008-09-30 16:22:53 编辑

解决方案 »

  1.   

    import javax.swing.JOptionPane;   
      
    public class ReverseString {   
        public static void reverseString (String str){   
            if (str.length() == 1){   
                System.out.print(str);   
            }   
            else{   
                String subString1 = str.substring(0, str.length()-1);   
                String subString2 = str.substring(str.length()-1);   
                   
                System.out.print(subString2);   
                   
                reverseString (subString1);            
            }   
        }   
           
        public static void main (String args[]){   
            String originalString;   
               
            originalString = JOptionPane.showInputDialog("Please input a String: ");   
               
            reverseString (originalString);        
        }   
    }
      

  2.   

    Sorry, I just copied the code from my blog without any edit...^_^...
      

  3.   

    This is a very good problem. :-)
      

  4.   

    I don't think it's a problem. The method I used in 1# bases on substring(). It's just a way of thinking, nothing more.
      

  5.   


    Hi, Mate.I didn't comment on your program. I was saying that this problem is a good one and students can learn a lot from it.:-)
      

  6.   

    class T {
          private static StringBuilder builder = new StringBuilder();
      public static void reserse(int[] a, int left, int right) {
          if(right == 0){
           builder.append(a[0]);
           return;
          }else {
    builder.append(a[right]);
    reserse(a, left, right - 1);
    }   }   public static void main(String args[]) throws Exception {
        int[] a = { 1, 2, 3, 4, 5 };
        reserse(a, 0, a.length - 1);
            System.out.println(builder);
      }
    }
      

  7.   

    import java.util.Arrays;class T {
          private static StringBuilder builder = new StringBuilder();
      public static void reserse(int[] a, int left, int right) {
      int t = 0;
      t = a[right];
      a[right] = a[left];
      a[left] = t;
      if(a.length / 2 == left + 1 || a.length / 2 == right - 1){
      return;
      }
          reserse(a, left + 1, right - 1);   }   public static void main(String args[]) throws Exception {
        int[] a = { 1, 2, 3, 4, 5 };
        reserse(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] 
      }
    }
      

  8.   

    先大体写一个再考虑别的public static void reverse(int[] a, int left, int right) {
            // 代码写在这里
            if (left == right) {  //数组长度为奇数
                return;
            }
            if (left + 1 == right) {  //数组长度为偶数
                int temp = a[left];
                a[left] = a[right];
                a[right] = temp;
            } else {
                int temp = a[left];
                a[left] = a[right];
                a[right] = temp;
                reverse(a, left + 1, right - 1);
            }
        }
      

  9.   

    Please allow me to give a simple input. Thanks.
    class T {  public static void reverse(int[] a, int left, int right) {
        if (left < right) {
          int tmp = a[left];
          a[left] = a[right];
          a[right] = tmp;
          reverse(a, ++left, --right);
        }
      }  public static void main(String args[]) throws Exception {
        int[] a = { 1, 2, 3, 4, 5 };
        reverse(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] 
      }
    }
      

  10.   

    import java.util.Arrays;class T {
    public static void reserse(int[] a, int left, int right) {
    int length = a.length;
    exchangeValue(a, left, right);
    if (length % 2 == 0 && length / 2 == left + 1) {
    return;
    } else if (a.length / 2 == left + 1) {
    return;
    }
    reserse(a, left + 1, right - 1); } private static void exchangeValue(int[] a, int left, int right) {
    int t = 0;
    t = a[right];
    a[right] = a[left];
    a[left] = t;
    } public static void main(String args[]) throws Exception {
    int[] a = { 1, 2, 3, 4, 5, 6 };
    reserse(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
    }
    }
      

  11.   

    import java.util.Arrays;class T
    { public static void reverse(int[] a, int left, int right)
    {
    while (left < right)
    {
    a[left] = a[left] + a[right];
    a[right] = a[left] - a[right];
    a[left] = a[left] - a[right];
    left++;
    right--;
    } } public static void main(String args[]) throws Exception
    {
    int[] a =
    { 1, 2, 3, 4, 5, 6 };
    reverse(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
    }
    }
      

  12.   

     if (left == right) {  //数组长度为奇数
                return;
            }这为什么能说明 长度为奇数?
    {1,2,3,4,5,6}
    数组长度为6left取1,right取1也不足以证明是奇数啊?
      

  13.   

    你left取1,right取1的情况是不可能出现的。
    如果是奇数的话,从最后和最前开始两两比较的话,最后肯定只剩下中间一个,所以应该有right==left
    如果是偶数的话,处理完两个之后剩下的还是偶数个,所以最后肯定是相邻的两个,应该有left+1==right
      

  14.   

    class T {  public static void reverse(int[] a, int left, int right) {
        // 代码写在这里
    if(left>=right)  return;

    tmp = a[left];
    a[left] = a[right];
    a[right] = temp;
    left++;
    right--;
    reverse(a,left,right);
      }  public static void main(String args[]) throws Exception {
        int[] a = { 1, 2, 3, 4, 5 };
        reverse(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] 
      }
    }
     
      

  15.   


    public static void reverse(int[] a, int left, int right) {
        
        // 代码写在这里
        if(left>=right)  return;  a[left]  = a[left]^a[right]; 
    a[right] = a[left]^a[right]; 
    a[left]  = a[left]^a[right]; 

    left++; 
    right--; 
    reverse(a,left,right); 
    }
      

  16.   


    public class Test { /**
     * @param args
     */ public static void reverse(int[] a, int left, int right) {
    // 代码写在这里
    if (left < right) {
    a[left] += a[right];
    a[right] = a[left] - a[right];
    a[left] = a[left] - a[right];
    reverse(a, left + 1, right -1);
    } else {
    return;
    }
    } public static void main(String args[]) throws Exception {
    int[] a = { 1, 2, 3, 4, 5 };
    reverse(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
    }}
      

  17.   

        红色可以直接用一个ELSE把!
      

  18.   


    不可以,假如此数组长度是个偶数,从left=0 right=a.length-1 每次left++,right--,那么left永远不会==right的
      

  19.   

    import java.util.Arrays;class T { public static void reverse(int[] a, int left, int right) {
    // 代码写在这里
    if((left+1)>right){//单复数都可以. 已经试验过到6了.
    return;
    }else{
    a[left]=a[left]+a[right];
    a[right]=a[left]-a[right];
    a[left]=a[left]-a[right];
    left++;
    right--;
    }
    reverse(a, left, right);
    } public static void main(String args[]) throws Exception {
    int[] a = { 1, 2, 3, 4, 5 ,6};
    reverse(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
    }
    }
      

  20.   

    a=a+b 可能会溢出a=a^b 不能用在浮点数都不好,还是老老实实地用temp比较好,清晰
      

  21.   

    public static void reverse(int[] a, int left, int right)
    {
        while(left < right)
        {
            int t = a[left];
            a[left]=a[right];
            a[right]=t;
            left++;
            right--;
        }
    }
      

  22.   

    不过 搞个int型数组  有  2147483648  项的 还是很难得的..应该很强大吧..
      

  23.   

    不是有这么多项。不过看《编程之美——微软技术面试心得》的时候,他们在处理二分查找的时候就要避免low+high溢出的情况,感觉有些麻烦了。专门看了一些实际的例子,好像没有多少考虑这样的问题的。
    作加法的是数组的元素,数组里有个比较大的元素不奇怪吧。单纯考虑lz的例子的话,就是1,2,3,4,5作为输入,什么都不考虑也没问题的。
      

  24.   

    智慧有限,没想出什么新意来,出去玩了一天,活跃下脑子. public static void reverse(int[] a, int left, int right) {
    if (left >= right)
    return;
    int temp=a[left];
    a[left]=a[right];
    a[right]=temp;
    reverse(a,++left,--right);
    }
      

  25.   

    package houlei.test;import java.util.Arrays;/**
     * 该类创建于 2008-10-1 下午08:41:40
     * 
     * @version 1.0.0
     * @author 侯磊
     */
    public class T {
    public static void reverse(int[] a, int left, int right) {
    // 代码写在这里
    if(left>=right)return;
    reverse(a,left+1,right-1);
    int t=a[left];
    a[left]=a[right];
    a[right]=t;
    } public static void main(String args[]) throws Exception {
    int[] a = { 1, 2, 3, 4, 5 };
    reverse(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
    }}
      

  26.   

    import java.util.Arrays;/**
     * 该类创建于 2008-10-1 下午08:41:40
     * 
     * @version 1.0.0
     * @author 侯磊
     */
    public class T {
    public static void reverse(int[] a, int left, int right) {
    // 代码写在这里
    if(left>=right)return;
    reverse(a,left+1,right-1);
    a[left]=a[left]+a[right];
    a[right]=a[left]-a[right];
    a[left]=a[left]-a[right];
    } public static void main(String args[]) throws Exception {
    int[] a = { 1, 2, 3, 4, 5 };
    reverse(a, 0, a.length - 1);
    System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
    }}
      

  27.   

    package com;import java.util.Arrays;
    class T {
      public static void reverse(int[] a, int left, int right) {
    if(right-left<2)
    return;
        int temp = a[left];
        a[left]=a[right];
        a[right]=temp;
        reverse(a, ++left,--right);
      }   public static void main(String args[]) throws Exception {
        int[] a = { 1, 2, 3, 4, 5 };
        reverse(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] 
      }
    }
      

  28.   

    简单的C++代码...void reverse(char* str, int len)
    {
    if (len-1<=0)
    return ; char c = str[0];
    str[0] = str[len-1];
    str[len-1] = c; reverse(str+1, len-2);
    }
      

  29.   

    前面有人说 的很好,我换个花样,改了个参数,更简单;
    package com.hust.test;import java.util.Arrays;class T {

      public static void reverse(int[] a,int[] b, int left, int right) {
       int size=a.length;
      if(left==size)  return;
    else
    {
    reverse(a,b,left+1,right-1);
    b[right]=a[left];
         }
      
       }
    public static void main(String args[]) throws Exception {
        int[] a = { 1, 2, 3, 4};
        int[] b=new int[a.length];//这是我加的一个参数
       reverse(a, b, 0,a.length - 1);
        System.out.println(Arrays.toString(b)); // 得到 [5, 4, 3, 2, 1] 
        
          
      

    }
    }
      

  30.   


    void reverse(char * str,int left,int right)
    {
      if(left<right)
       {
          char c=str[left];
          str[left]=str[right];
         str[right]=c;
       reverse(str,left++,right--)
       }
    }
    用C++同样可以用二分法
      

  31.   


    void reverse(char * str,int left,int right)
    {
      if(left<right)
       {
          char c=str[left];
          str[left]=str[right];
         str[right]=c;
       reverse(str,++left,--right);
       }
    }
    粗心了
      

  32.   


    #include <cstdlib>
    #include <iostream>
    using namespace std;
    void reverse(char *str,int left,int right)
    {
         
         if(left<right)
         {
                       char c=str[left];
                       str[left]=str[right];
                       str[right]=c;
                       reverse(str,++left,--right);
         }
        
    };  
    int main(int argc, char *argv[])
    {
        char str[]={'1','2','3','4','5','6','7'};
        cout<<sizeof(str)<<endl;
        reverse(str,0,sizeof(str)-1);
        cout<<str<<endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    不知道为什么,上段代码发生溢出了 7654321舜€|?"
      

  33.   


    #include <cstdlib>
    #include <iostream>
    using namespace std;
    void reverse(char *str,int left,int right)
    {
         
         if(left<right)
         {
                       char c=str[left];
                       str[left]=str[right];
                       str[right]=c;
                       reverse(str,++left,--right);
         }
        
    };  
    int main(int argc, char *argv[])
    {
        char str[]="1234567";
        cout<<sizeof(str)<<endl;
        reverse(str,0,sizeof(str)-1);
        str[sizeof(str)]='\0';
        cout<<str<<endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    更正我的错误