BlogJava 联系 聚合 管理  

Blog Stats

News

 

蓝冰飞寒个人独立博客地址,www.uphenan.com

随笔档案

文章档案


蓝冰飞寒

用心去做每一件事情

好像对这个题我已经贴出了算法了,但是好象不是自己写的,现在贴出一个不是很好的,但是是我自己写的, 又费了几十分钟。好辛苦啊,其实就是把数组从某个位置开始依次向后移动一个位置,我都忘了数据结构的时候是怎么处理的了。



//【程序30】
//题目:有一个已经排好序的数组。现输入一个数,
//要求按原来的规律将它插入数组中。
/*
arraycopy
public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)
  参数:
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
*/
import java.io.*;

public class ChaRuShuZi{
     public static int m;
      public static void main(String args[]){
        ChaRuShuZi cha=new ChaRuShuZi();
      //  cha.shuRuLiu();
        cha.suanFa(3);
        
        }
        public void shuRuLiu(){
               BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
               try{
                  String in= input.readLine();  
                  m=Integer.parseInt(in);              
                 }catch(IOException e){
                   e.printStackTrace();
                   System.out.println("输入时出现异常!请再次运行本程序!");
                   }
          
          }
        public void suanFa(int m){
            int[] b={1,2,4,5,6,7};
              for(int i=0;i                System.out.print(b[i]+"  ");
                
                System.out.println();
              if(b[0]                   for(int i=0;i                           if(m                             int [] c=new int[7];
                              System.arraycopy(b,0,c,0,b.length);
                              b=c;
                              //b[i]=m;
                              
                             /*  for(;i                               int temp=b[i];
                              // temp=b[i+1];
                              
                              b[i+1]=b[i];
                              
                             b[i+2]=temp;
                              
                             }
                             b[i]=m;
                              
                               */
                              
                              
                             }
                     }
                      b[b.length-1]=m;
                      int q =0;
                               for(int j=b.length-1;j>=0;j--){
                                 if(b[j]>m&&b[j-1]                                   q =j;
                                   for(int p=b.length-2;p>m;p--)
                                   b[p+1]=b[p];
                                  
                                  
                                   }
                                
                                 }
                                 b[q]=m;
                     for(int i=0;i                System.out.print(b[i]+"  ");
                }
                
          
          
          }
  
   }
Tags -
文章来源:http://www.tt-shopping.com/kevinlau/read.php/114.htm
posted on 2009-05-09 21:59 蓝冰飞寒 阅读(58) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: