首页 > 技术文章 > 数塔问题

mowanji 2016-05-12 16:32 原文

c++语言

#include <iostream>
#include <time.h>
#include <string>
#include <stdlib.h>
#include <cmath>
using namespace std;
int main() {
 int num[20][20] = {NULL};
 //该函数共有三个参数,两个有默认值
 //第一个为一个保存数据的二维数组
 //第二个为整个三角距左边屏幕的距离(根据三角形的层数可自定义,默认为30)
 //第三个三角形的层数,可自定义(默认为7)
 void triangle(int[20][20], int = 30, int = 7);
 int bestTrace(int[20][20], int);
 triangle(num, 30, 20);
 int trace[10000] = {NULL};
 int best = bestTrace(num, 20);
 cout << "最佳路径为 : " << best << endl;
 
}
//三角形结构函数
void triangle(int num[20][20], int sp = 30, int level = 7) {
 int space = sp;
 srand(time(0));
 for (int i = 1; i <= level; i++) {
  for (int j = 0; j < space; j++) {
   cout << " ";
  }
  for (int k = 0; k < i; k++) {
   int r = rand()%10;
   num[i - 1][k] = r;
   cout << r << " ";
  }
  cout << endl;
  space --;
 }
}

//最佳路径计算函数
int bestTrace(int num[20][20], int level) {
 int best = 0, path[20] = {NULL};
 for (int i = 0; i < level; i++) {
  if (i == 0) {
   best = num[i][0];
  } else {
   for (int k = 0; k < i+1; k++) {
    path[k] = best + num[i][k];
   }
   int max = path[0];
   for (int s = 0; s < i+1; s++) {
    if (path[s] > max) {
     max = path[s];
    }
   }
   best = max;
  }
 }
 return best;
}

结果:

                                  

java版

package com.student;

public class Test {
 
 public int[][] triangle() {
  int space = 20;
  int[][] trace = new int[7][];
  for (int i = 0; i < 7; i++) {
   trace[i] = new int[i+1];
   for (int j = 0; j < space; j++) {
    System.out.print(" ");
   }
   for (int k = 0; k <= i; k++) {
    int r = (int)Math.ceil((Math.random()*9));
    trace[i][k] = r;
    System.out.print(r + " ");
   }
   System.out.println();
   space--;
  }
  return trace;
 }

 public static void main(String[] args) {
  Test t = new Test();
  int[][] trace = t.triangle();
  int max = trace[0][0];
  for (int i = 1; i < trace.length; i++) {
   int temp = max;
   max += trace[i][0];
   for (int j = 1; j < trace[i].length; j++) {
    if (trace[i][j] + temp > max) {
     max = trace[i][j] + temp;
    }
   }
  }
  System.out.println("最佳路径为: " + max);
 }

}

 

 

javascript版

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>三角巨阵</title>
</head>
<body>
    <div>
        <pre></pre>
    </div>
    <script>
        var test = new Object();
        test.triangle = function(space = 30, level = 7) {
            var p = document.getElementsByTagName('pre')[0];
            var arr = new Array();
            for (var i = 0; i < level; i++) {
                for (var j = 0; j < space; j++) {
                    p.innerHTML += ' ';
                }
                for (var k = 0; k <= i; k++) {
                    var r = Math.ceil(Math.random()*9);
                    arr[i][k] = r;
                    p.innerHTML += r + ' ';
                }
                space--;
            }
            return arr;
        };
        test.getBestWay = function(arr) {
            var max = arr[0][0];
            for (var i = 1; i < arr.length; i++) {
                var temp = max;
                max += arr[i][0];
                for (var j = 0; j <= arr[i].length; j++) {
                    if (arr[i][j] + temp > max) {
                        max = arr[i][j] + temp;
                    }
                }
            }
            return max;
        };
        var arr = test.triangle();
        var way = test.getBestWay(arr);
        var obj = document.getElementsByTagName('pre')[0];
        obj.innerHTML += "<br>最佳路径为:" + way;
    </script>
</body>
<ml>

PHP版

 

<?php
class test {
    private $array = array();
    private function triangle($space, $level) {
        for ($i = 0; $i < $level; $i++) {
            for ($j = 0; $j < $space; $j++) {
                echo " ";
            }
            for ($k = 0; $k <= $i; $k++) {
                $rand = mt_rand(1,9);
                $arr[$i][] = $rand;
                echo $rand.' ';
            }
            echo '<br>';
            $space--;
        }
        return $arr;
    }

 

    private function getBestTrace($arr, $level) {
        for ($i = 1; $i < $level; $i++) {
            for ($j = 0; $j < $i; $j++) {
                foreach ($this->array[0][$j] as $k=>$v) {
                    $this->array[1][$j][] = $arr[$i][$j] + $v;
                    $this->array[1][$j+1][] = $arr[$i][$j+1] + $v;
                }
            }
            $this->array = array_slice($this->array, 1);
            foreach ($this->array[0] as &$val) {
                $n = count($val);
                $num = array();
                if ($n > 1) {
                    sort($val);
                    foreach ($val as $v) {
                        if ($v > $val[$n-1] - 9) {
                            $num[] = $v;
                        }
                    }
                    $val = $num;
                }
            }
        }
    }

 

    private function getBestWay() {
        foreach ($this->array as $value) {
            foreach ($value as $val) {
                foreach ($val as $v) {
                    $array[] = $v;
                }
            }
        }
        $max = $array[0];
        foreach ($array as $va) {
            if ($va > $max) {
                $max = $va;
            }
        }
        return $max;
    }

 

    public function showTriangle($space = 20, $level = 7) {
        $arr = $this->triangle($space, $level);
        $this->array[0][0][0] = $arr[0][0];
        $this->getBestTrace($arr, $level);
        echo '最佳路径为 : '.$this->getBestWay();
    }
}
echo '<pre>';
$obj = new test();
$obj->showTriangle(60, 40);
echo '</pre>';

 

推荐阅读