- 论坛徽章:
- 3
|
本帖最后由 cjaizss 于 2011-01-09 11:59 编辑
还是这个取最大值,举个含有多个栈,每个栈入出并不完全一致的例子,这里的test6函数
- #!/bin/awk -f
- func test1(a,start,len)
- {
- if(len<=1)
- return a[start];
- x = test1(a,start,int(len/2));
- y = test1(a,start+int(len/2),len-int(len/2));
- return (x>y)?x:y;
- }
- func test2(a,start,len)
- {
- if(len<=1)
- return a[start];
- testlen++;
- testa[testlen] = test2(a,start,int(len/2));
- testlen++;
- testa[testlen] = test2(a,start+int(len/2),len-int(len/2));
- testlen-=2;
- return (testa[testlen+1]>testa[testlen+2])?testa[testlen+1]:testa[testlen+2];
- }
- func test3(a,start,len)
- {
- if(len<=1) {
- return a[start];
- }
- V = V";"test3(a,start,int(len/2));
- V = V";"test3(a,start+int(len/2),len-int(len/2));
- xx = V;
- sub(/.*;/,"",xx);
- sub(/;[^;]+$/,"",V);
- yy = V;
- sub(/.*;/,"",yy);
- sub(/;[^;]+$/,"",V);
- return int(xx)>int(yy)?int(xx):int(yy);
- }
- func test4(a,start,len)
- {
- if(len<=1)
- return a[start];
- testlenx++;
- testleny++;
- testax[testlenx] = test4(a,start,int(len/2));
- testay[testleny] = test4(a,start+int(len/2),len-int(len/2));
- testlenx-=1;
- testleny-=1;
- return (testax[testlenx+1]>testay[testleny+1])?testax[testlenx+1]:testay[testleny+1];
- }
- func test5(a,start,len)
- {
- if(len<=1) {
- return a[start];
- }
- V1 = V1";"test5(a,start,int(len/2));
- V2 = V2";"test5(a,start+int(len/2),len-int(len/2));
- xx = V1;
- sub(/.*;/,"",xx);
- sub(/;[^;]+$/,"",V1);
- yy = V2;
- sub(/.*;/,"",yy);
- sub(/;[^;]+$/,"",V2);
- return int(xx)>int(yy)?int(xx):int(yy);
- }
- func test6(a,start,len)
- {
- if(len <= 1) {
- return a[start];
- } else if(len == 2) {
- return (a[start]>a[start+1])?a[start]:a[start+1];
- } else if(len == 3) {
- var1 = (a[start]>a[start+1])?a[start]:a[start+1];
- return (var1>a[start+2])?var1:a[start+2];
- }
- var2 = int(rand()*10000)%3+2;
- if(var2 == 2) {
- testlenx++;
- testleny++;
- testax[testlenx] = test6(a,start,int(len/2));
- testay[testleny] = test6(a,start+int(len/2),len-int(len/2));
- testlenx-=1;
- testleny-=1;
- return (testax[testlenx+1]>testay[testleny+1])?testax[testlenx+1]:testay[testleny+1];
- } else if(var2 == 3) {
- testlenx++;
- testleny++;
- testlenz++;
- testax[testlenx] = test6(a,start,int(len/3));
- testay[testleny] = test6(a,start+int(len/3),int(len/3));
- testaz[testlenz] = test6(a,start+2*int(len/3),len-2*int(len/3));
- testlenx-=1;
- testleny-=1;
- testlenz-=1;
- var1 = (testax[testlenx+1]>testay[testleny+1])?testax[testlenx+1]:testay[testleny+1];
- return ((var1>testaz[testlenz+1])?var1:testaz[testlenz+1]);
- } else if(var2 == 4) {
- testlenx++;
- testleny++;
- testlenz++;
- testlenA++;
- testax[testlenx] = test6(a,start,int(len/4));
- testay[testleny] = test6(a,start+int(len/4),int(len/4));
- testaz[testlenz] = test6(a,start+2*int(len/4),int(len/4));
- testaA[testlenA] = test6(a,start+3*int(len/4),len-3*int(len/4));
- testlenx-=1;
- testleny-=1;
- testlenz-=1;
- testlenA-=1;
- var1 = (testax[testlenx+1]>testay[testleny+1])?testax[testlenx+1]:testay[testleny+1];
- var4 = (testaz[testlenz+1]>testaA[testlenA+1])?testaz[testlenz+1]:testaA[testlenA+1];
- return ((var1>var4)?var1:var4);
- }
- }
- BEGIN {
- srand(systime());
- }
- NR==1{
- way=$1;
- print $1
- }
- NR==2{
- max=$1;
- for(i=2;i<=NF;i++)
- if($i > max)
- max = $i;
- print max;
- for(i=1;i<=NF;i++)
- a[i] = $i;
- if(way == 2)
- print test2(a,1,NF);
- else if(way == 3)
- print test3(a,1,NF);
- else if(way == 4)
- print test4(a,1,NF);
- else if(way == 5)
- print test5(a,1,NF);
- else if(way == 6)
- print test6(a,1,NF);
- else
- print test1(a,1,NF);
- exit(0);
- }
复制代码 |
|