用vue写一个杨辉三角形的代码
时间: 2024-05-26 20:10:59 浏览: 85
可以的,以下是一个用Vue写的杨辉三角形代码:
<template>
<div>
<h3>杨辉三角形:</h3>
<table>
<tbody>
<tr v-for="(row, rowIndex) in rows">
<td v-for="(value, colIndex) in row" :key="`${rowIndex}-${colIndex}`">{{ value }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
rows: [],
};
},
created() {
this.generateTriangle(10);
},
methods: {
generateTriangle(numRows) {
const rows = [];
for (let i = 0; i < numRows; i++) {
const row = [];
for (let j = 0; j <= i; j++) {
if (j === 0 || j === i) {
row.push(1);
} else {
row.push(rows[i - 1][j - 1] + rows[i - 1][j]);
}
}
rows.push(row);
}
this.rows = rows;
},
},
};
</script>
阅读全文