乘公交看奥运问题转乘公汽一次mathematica源码~

这段代码部分是我的功劳,大部分是饭盒同学的智慧嘎嘎~

cc[l_List, qd_, zd_] :=
  Module[{i, j, lx1 = {}, lx2 = {}, t4 = {}, t6 = {}},
               For[i = 1, i <= 1042, i++, t1 = {}; t2 = {};
                               For[j = 4, j <= Length[l[[i]]], j++,
     If[l[[i, j]] == qd, AppendTo[t1, i];
      For[k = j + 1, k <= Length[l[[i]]], k++, AppendTo[t1, l[[i, k]]]
                                                            ];
      AppendTo[lx1, t1]
                                           ];
     If[l[[i, j]] == zd, AppendTo[t2, i];
      For[k = 4, k <= j – 1, k++, AppendTo[t2, l[[i, k]]]
                                                             ];
      AppendTo[lx2, t2]
                                            ]
                                     ]
                          ];
              For[i = 1, i <= Length[lx1], i++,
    For[j = 1, j <= Length[lx2], j++, t3 = {};
     If[Intersection[lx1[[i]], lx2[[j]]] != {} &&
       lx1[[i, 1]] != lx2[[j, 1]], AppendTo[t3, lx1[[i, 1]]];  
      AppendTo[t3, Intersection[lx1[[i]], lx2[[j]]]];
      AppendTo[t3, lx2[[j, 1]]]; AppendTo[t4, t3]
                                              ]
                                   ]
                       ];
              For[i = 1, i <= Length[t4], i++,
    For[j = 1, j <= Length[t4[[i, 2]]], j++, t5 = {};
                                 x1 = Position[l[[t4[[i, 1]]]], qd];
     x2 = Position[l[[t4[[i, 1]]]], t4[[i, 2, j]]];
      x3 = Position[l[[t4[[i, 3]]]], t4[[i, 2, j]]];
     x4 = Position[l[[t4[[i, 3]]]], zd];
     AppendTo[t5, l[[t4[[i, 1]], 1]]];  
     AppendTo[t5, t4[[i, 2, j]]];
     AppendTo[t5, l[[t4[[i, 3]], 1]]];
     AppendTo[t5,
      x2[[1, 1]] – x1[[1 , 1]] + x4[[1, 1]] – x3[[1, 1]]]   ;
     If[l[[t4[[i, 3]], 2]] == “DY”, money1 = 1,
      Which[(x2[[1, 1]] – x1[[1 , 1]]) >=
         0 && (x2[[1, 1]] – x1[[1 , 1]]) <= 20,
       money1 =
        1, (x2[[1, 1]] – x1[[1 , 1]]) >=
         21 && (x2[[1, 1]] – x1[[1 , 1]]) <= 40,
       money1 = 2, (x2[[1, 1]] – x1[[1 , 1]]) > 40, money1 = 3]];
     If[l[[t4[[i, 3]], 2]] == “DY”, money2 = 1,
      Which[(x4[[1, 1]] – x3[[1 , 1]]) >=
         0 && (x4[[1, 1]] – x3[[1 , 1]]) <= 20,
       money2 =
        1, (x4[[1, 1]] – x3[[1 , 1]]) >=
         21 && (x4[[1, 1]] – x3[[1 , 1]]) <= 40,
       money2 = 2, (x4[[1, 1]] – x3[[1 , 1]]) > 40, money2 = 3]];
                AppendTo[t5, money1 + money2];
     AppendTo[
      t5, (x2[[1, 1]] – x1[[1 , 1]] + x4[[1, 1]] – x3[[1, 1]])*3 + 5];
     AppendTo[t6, t5]
                             ]
                     ];
         Print[t6]
              ];

记下今天mathematica中用的两个新函数

第一个  Flatten

用法:这个是help brows上的   

Flatten[list]
flattens out nested lists.
Flatten[list,n]
flattens to level n.
Flatten[list,n,h]
flattens subexpressions with head h.
Flatten[list,{{Subscript[s, 11],Subscript[s, 12],[Ellipsis]},{Subscript[s, 21],Subscript[s, 22],[Ellipsis]},[Ellipsis]}]
flattens list by combining all levels Subscript[s, ij] to make each level i in the result.

这个理解有点难度呵呵,其实也就是那个意思嘎嘎

看下里面的 Basic examples

一,Flatten out lists at all levels:

      In[1]:=Flatten[{{a, b}, {c, {d}, e}, {f, {g, h}}}]

      Out[1]:= {a, b, c, d, e, f, g, h}

二,Flatten only at level 1:

       In[1]:= Flatten[{{a, b}, {c, {d}, e}, {f, {g, h}}}, 1]

      Out[1]:={a, b, c, {d}, e, f, {g, h}}

好了例子就是这个了,不过注意是basic呵呵

第二个  Intersection

Intersection[Subscript[list, 1],Subscript[list, 2],[Ellipsis]]
gives a sorted list of the elements common to all the Subscript[list, i].

Basic examples

Find elements common to all the lists given:

In[1]:=Intersection[{1, 1, 2, 3}, {3, 1, 4}, {4, 1, 3, 3}]

Out[1]:={1, 3}

好了用法就是这样的~

主要是解决公交换乘问题换乘一次的方案实施~

好吧,洗洗睡了,上次的lingo分析还没彻底搞好,真是悲了个剧,算了睡觉,我还想再活五百年~