Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1226817
  • 博文数量: 788
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 7005
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-19 15:52
文章存档

2017年(81)

2011年(1)

2009年(369)

2008年(337)

分类:

2008-10-14 16:38:58



问题1:找出两数组的不同数和共有数的值和位置,和同值同位置的值存入数组s1……s9  
  如:  
  数组值r1为{   1,   6,23,15,   5,28,   8,11,10,19}  
  数组值r2为{   2,23,   4,15,   5,17,12,11,   3}  
   
  需要找出:  
  数组值r1不同数的值:           s1   {1,6,28,8,10,19}  
  数组值r1不同数的位置:       s2   {0,1,5,6,8,9}  
  数组值r2不同数的值:           s3   {2,4,17,12,3}  
  数组值r2不同数的位置:       s4   {0,2,5,6,8}  
  两数组共有数的值:               s5   {23,15,5,11}  
  两数组共有数r1的位置:       s6   {2,3,4,7}  
  两数组共有数r1的位置:       s7   {1,3,4,7}  
  两数组同值同位置的值:       s8   {15,5,11}  
  两数组同值同位置的位置:   s9   {3,4,7}  
   
  注意:实际应用中是动态数组,而且还很大,需要一个精简的办法!!  
   
   
   
   
  问题2:把数组按数值从小到大排列。同上注意!!!

var  
      R1:   array   of   Integer;  
      R2:   array   of   Integer;  
      SL1:   TStringList;  
      SL2:   TStringList;  
      Idx:   Integer;  
      Index:   Integer;  
      S1:   array   of   Integer;  
      S2:   array   of   Integer;  
      S3:   array   of   Integer;  
      S4:   array   of   Integer;  
      S5:   array   of   Integer;  
      S6:   array   of   Integer;  
      S7:   array   of   Integer;  
      S8:   array   of   Integer;  
      S9:   array   of   Integer;  
  begin  
      Setlength(R1,   10);  
      SetLength(R2,   9);  
      R1[0]   :=   1;  
      R1[1]   :=   6;  
      R1[2]   :=   23;  
      R1[3]   :=   15;  
      R1[4]   :=   5;  
      R1[5]   :=   28;  
      R1[6]   :=   8;  
      R1[7]   :=   11;  
      R1[8]   :=   10;  
      R1[9]   :=   19;  
   
      R2[0]   :=   2;  
      R2[1]   :=   23;  
      R2[2]   :=   4;  
      R2[3]   :=   15;  
      R2[4]   :=   5;  
      R2[5]   :=   17;  
      R2[6]   :=   12;  
      R2[7]   :=   11;  
      R2[8]   :=   3;  
   
      SL1   :=   TStringList.Create;  
      SL2   :=   TStringList.Create;  
   
      //'%.8d'的目的有2个,  
      //其一是排序,   其二是满足大数据  
   
      for   Idx   :=   Low(R1)   to   High(R1)   do  
          SL1.Add(Format('%.8d',   [R1[Idx]]));  
   
      for   Idx   :=   Low(R2)   to   High(R2)   do  
          SL2.Add(Format('%.8d',   [R2[Idx]]));  
   
      //为了显示方便,我用了ListBox.  
      //在你的实际程序中你可以用TStringList来代替.  
   
      ListBox1.Sorted   :=   True;  
      ListBox2.Sorted   :=   True;  
      ListBox3.Sorted   :=   True;  
      ListBox4.Sorted   :=   True;  
      ListBox5.Sorted   :=   True;  
      ListBox6.Sorted   :=   True;  
      ListBox7.Sorted   :=   True;  
      ListBox8.Sorted   :=   True;  
      ListBox9.Sorted   :=   True;  
   
      for   Idx   :=   0   to   Pred(SL1.Count)   do      
      begin  
          Index   :=   SL2.IndexOf(SL1[Idx]);  
          if   Index   <   0   then  
          begin  
              ListBox1.Items.Add(SL1[Idx]);  
              ListBox2.Items.Add(Format('%.8d',   [Idx]));  
          end   else  
          begin  
              ListBox5.Items.Add(SL1[Idx]);  
              ListBox6.Items.Add(Format('%.8d',   [Idx]));  
              ListBox7.Items.Add(Format('%.8d',   [Index]));  
              if   Idx   =   Index   then  
              begin  
                  ListBox8.Items.Add(SL1[Idx]);  
                  ListBox9.Items.Add(Format('%.8d',   [Idx]));  
              end;  
          end;  
      end;  
   
      for   Idx   :=   0   to   Pred(SL2.Count)   do  
      begin  
          if   SL1.IndexOf(SL2[Idx])   <   0   then  
          begin  
              ListBox3.Items.Add(SL2[Idx]);  
              ListBox4.Items.Add(Format('%.8d',   [Idx]));  
          end;  
      end;  
   
      //剩下的工作就是把Listbox   --->   s数组,  
      //我只写一个,其余的照葫芦画瓢}  
       
      SetLength(S1,   ListBox1.Items.Count);  
      for   Idx   :=   0   to   Pred(ListBox1.Items.Count)   do  
          S1[Idx]   :=   StrToInt(ListBox1.Items[Idx]);  
   
      //上面算法的前提  
      //1、是每个数组自身没有重复数  
      //2.   R1的长度大于等于R2的长度  
   
    //如要满足任何条件,算法基本没有大的变化,   只是多了一个遍历  
   
   
  end;



[新闻]Silverlight对Flash 微软打垮Adobe
博客园首页 社区 新闻频道 小组 博问 网摘 闪存
阅读(417) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~