java输出杨辉三角
① java:杨辉三角,输入n输出它的前n行
import java.util.Scanner;
public class HelloWorld {
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
System.out.println("输入n");
int n=input.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (j < i) {
a[i][j] = 1;
if (j == 0) {
a[i][j] = 1;
} else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
} else {
a[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int k = 1; k <= n - i; k++)
System.out.printf(" ");
for (int j = 0; j <= i; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.printf("\n");
}
}
}
② Java语言杨辉三角
打印杨辉三角代码如下:
public class woo {
public static void triangle(int n) {
int[][] array = new int[n][n];//三角形数组
for(int i=0;i<array.length;i++){
for(int j=0;j<=i;j++){
if(j==0||j==i){
array[i][j]=1;
}else{
array[i][j] = array[i-1][j-1]+array[i-1][j];
}
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String args[]) {
triangle(9);
}
}
(2)java输出杨辉三角扩展阅读:
杨辉三角起源于中国,在欧洲这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年。它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的优美结合。
杨辉三角具有以下性质:
1、最外层的数字始终是1;
2、第二层是自然数列;
3、第三层是三角数列;
4、角数列相邻数字相加可得方数数列。
③ 用java编程打印输出10行杨辉三角形
// 直接复制
public class SanJiao {
public static void main(String[] args) {
int row = 10; // 三角的行数
int[][] result = new int[row][row];
for (int i = 0; i < row; i++) { // 行
result[i][0] = 1;
System.out.print(result[i][0] + "\t");
for (int j = 1; j <= i; j++) { // 列
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
System.out.print(result[i][j] + "\t");
}
System.out.println();
}
}
}
④ 用JAVA程序打印出杨辉三角
||给你来个main方法(10行结果自的)
public static void main(String args[])
{
int a=10;
int b[][];
b=new int[a][];
for(int i=1;i<=10;i++)
{
b[i-1]=new int[i];
}
for(int j=0;j<10;j++)
{
for(int k=0;k<=j;k++)
{
if(j==0||k==0||k==j)//分支
{
b[j][k]=1;//等于1的
continue;
}else
{
b[j][k]=b[j-1][k-1]+b[j-1][k];//计算值
}
}
}
for(int m=0;m<10;m++)
{
for(int n=0;n<=m;n++)
{
System.out.print(b[m][n]+" ");//循环输出
}
System.out.println(" ");
}
}
⑤ java输出杨辉三角怎么修改
就改了2个小地方
for (int k = 0; k < n - i - 1; k++) {
System.out.print(" ");
}
packageshiyan;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclass实验4杨辉三角{
publicstaticvoidmain(String[]args)throwsException{
intn=0;
System.out.println("杨辉三角");
for(;;){
retry:for(;;){
System.out.print("请输入行数n(输入exit退出):");
InputStreamReaderisr=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(isr);
Strings=br.readLine();
s=s.trim();
booleanb=s.equalsIgnoreCase("exit");
if(b){
return;
}
try{
n=Integer.parseInt(s);
}catch(Exceptione){
System.out.println("输入非法,请重新输入!");
continueretry;
}
if(n>0){
break;
}else{
System.out.println("输入非法,请重新输入!");
}
}
inta[][]=newint[n][n];
for(inti=0;i<n;i++){
for(intk=0;k<n-i-1;k++){
System.out.print("");
}
for(intj=0;j<i+1;j++){
if(j==0||j==i){
a[i][j]=1;
System.out.print("1");//
System.out.print("");
}else{
a[i][j]=a[i-1][j-1]+a[i-1][j];//
System.out.print(a[i][j]);
Stringstr=String.valueOf(a[i][j]);
str=str.trim();
intl=str.length();
if(l==1){
System.out.print("");
}
if(l==2){
System.out.print("");
}
if(l==3){
System.out.print("");
}
if(l==4){
System.out.print("");
}
}
}
System.out.println();
}
}
}
}
⑥ 用JAVA打印输出10行杨辉三角形
public class YangHui {
public static void main(String args[]) {
final int ROW = 10;
int a[][] = new int[ROW + 1][];
for (int i = 0; i <= ROW; i++) {
a[i] = new int[i + 1]; // 指定每行的列数
}
yanghui(a, ROW);
}
static void yanghui(int a[][], int ROW) {
for (int i = 0; i <= ROW; i++)
for (int j = 0; j <= a[i].length - 1; j++) {
if (i == 0 || j == 0 || j == a[i].length - 1)
a[i][j] = 1;
else
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
for (int i = 0; i <= ROW; i++) {
for (int j = 0; j <= a[i].length - 1; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
⑦ java 输出杨辉三角
import java.util.ArrayList;
import java.util.List;
public class Admin {
public static void main(String... args) {
List result = test(100);
show(result);
}
private static List test(int row) {
List result = new ArrayList();
for (int i = 0; i < row; i++) {
Node[] arr = getLastArr(result, i - 1);
Node[] nodes = new Node[i + 1];
for (int j = 0; j < nodes.length; j++) {
Node left = getNode(arr, j - 1);
Node right = getNode(arr, j);
nodes[j] = new Node(1, left, right);
}
result.add(nodes);
}
return result;
}
private static Node getNode(Node[] arr, int idx) {
if (arr == null || idx > arr.length - 1 || idx < 0) {
return null;
}
return (Node) arr[idx];
}
private static Node[] getLastArr(List data, int idx) {
if (data == null || idx > data.size() - 1 || idx < 0) {
return null;
}
return (Node[]) data.get(idx);
}
private static void show(List result) {
for (int i = 0; i < result.size(); i++) {
Node[] nodes = (Node[]) result.get(i);
for (int j = 0; j < nodes.length; j++) {
System.out.print(nodes[j] + " ");
}
System.out.println();
}
}
}
class Node {
private long num = 0;
private Node left;
private Node right;
public Node(int num, Node left, Node right) {
this.num = num;
this.left = left;
this.right = right;
}
public String toString() {
long leftNum = 0;
long rightNum = 0;
if (left != null) {
leftNum = left.num;
}
if (right != null) {
rightNum = right.num;
}
if (leftNum + rightNum == 0) {
return num + "";
}
num = leftNum + rightNum;
return num + "";
}
}
⑧ 输出杨辉三角形,Java。
publicstaticvoidmain(String[]args)
{
System.out.print("请输入整数n:");
Scannerin=newScanner(System.in);
intn=in.nextInt();
int[][]iaYong=newint[n][];//先分配n行
System.out.println("杨辉三角形:");
iaYong[0]=newint[1];//分配第一行的列
iaYong[0][0]=1;
for(inti=1;i<iaYong.length;i++)
{
iaYong[i]=newint[i+1];//分配其余行的列
iaYong[i][0]=1;
for(intj=1;j<i;j++)
iaYong[i][j]=iaYong[i-1][j-1]+iaYong[i-1][j];
iaYong[i][i]=1;
}
for(intline=0;line<n;line++)//上半部
{
for(inti=0;i<n-line-1;i++)//输出每行的前导空格
System.out.print("");
for(intcol:iaYong[line])
System.out.printf("%3d",col);//输出数据
System.out.println();
}
for(intline=n-2;line>=0;line--)//下半部
{
for(inti=0;i<n-line-1;i++)//输出每行的前导空格
System.out.print("");
for(intcol:iaYong[line])
System.out.printf("%3d",col);//输出数据
System.out.println();
}
}
⑨ 怎样用java打印杨辉三角,自己输入行
/**
* 打印杨辉三角
功能描述:使用多重循环打印6阶杨辉三角
* @author pieryon
*
*/
public class YHSJ {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入行号:");
int m = in.nextInt();
int n = 2*m-1;//列元素数;
int arr[][] = new int[m][n];
for (int i = 0; i < m; i++) { //外循环控制行
for (int j = 0; j < n; j++) { //内循环控制列
if (j<(m-i-1)||(j>=(m+i))) { //输出等腰三角形两边空格
System.out.print(" ");
}else if (j==(m-i-1)||j==(m+i-1)) { //计算输出等腰三角形两边的空格
arr[i][j] = 1;
System.out.print(arr[i][j]);
}else if ((i+j)%2==0&&m%2==0||(i+j)%2==1&&m%2==1) {
System.out.print(" ");
}else {
arr[i][j] = arr[i-1][j-1]+arr[i-1][j+1];
System.out.print(arr[i][j]);
}
}
System.out.println();
}
}
}
以上就可以轻松实现杨辉三角的打印了!