设计二维数组,输出、处理杨辉三角形,显示10行的杨辉三角形

本文档展示了如何在C++中实现一个三角数组,通过计算组合数来填充数组,并使用Java进行对比。重点讲解了Java和C++中数组的不同声明方式,以及迭代工具如'foreach'循环的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AS
AC

/**
 * This programe demonstrates a triangular array
 * @version 1.30 2021-12-11
 */
public class LotteryArray {
	public static void main(String[] args) {
		final int NMAX = 10;
        
		//allocate triangular array 
		int[][] odds = new int[NMAX + 1][];
		for (int n = 0; n <= NMAX; n++)
			odds[n] = new int[n + 1];

		//fill triangular array
		for (int n = 0; n < odds.length; n++)
			for (int k = 0; k < odds[n].length; k++) {
				/*
				*compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
				*/
				int lotteryOdds = 1;
				for (int i = 1; i <= k; i++)
					lotteryOdds = lotteryOdds * (n - i + 1) / i;

				odds[n][k] = lotteryOdds;
			}

		//print triangular array
		for (int[] row : odds) {
			for (int odd : row)
				System.out.printf("%4d", odd);
			System.out.println();
		}
	}
}

C++ NOTE: In C++, the Java declaration

double [][] balances = new double[10][6]; //Java

is not the same as

double balences[10][6]; //C++

or even

double (*balences)[6] = new double [10][6]; //C++

Instead, an array of ten pointers is allocated:

double** balences = new double*[10]; //C++

Then, each elements in the pointer array is filled with an array of six numbers:

for(i = 0;i < 10;i++)
  balences[i] = new double[6];

Mercifully, this loop is automatic when you ask for a new double[10][6]. When you want ragged arrays, you allocate the row arrays separately.


The “for each” Loop

Java has a powerful looping construct that allows you to loop through each element in an array(or any other collection of elements)without having to fuss with index values.

The enhanced for loop

 for (variable : collection) statement

sets the given variable to each element of the collection and then executes the statement(which, of course, may be a block). The collection expression must be an array or an object of a class that implements the Iterable interface, such as ArrayList. We discuss array lists in Chapter 5 and the Iterable interface in Chapter 2 of Volume II.

For example,

    for (int element : a)
   System.out.println(element);

prints each element of the array a on a seperate line.

You should read this loop as “for each element in a”. Then designers of the Java languages considered using keyboards, such as foreach and in. But this loop was a late addition to the Java language, and in the end nobody wanted to break the old code that already contained methods or variable with these names(such as System.in).
Of course, you could achieve the same effect with a traditional for loop:

   for (int i = 0;i < a.length;i++)
      System.out.println(a[i]);

NOTE: The loop variable of the “for each” loop traverses the elements of the array, not the index values.


Additional notes

java.math.BigInteger

  • BigInteger subtract (BigInteger other)
  • BigInteger multiply (BigInteger other)
  • BigInteger divide (BigInteger other)
  • BigInteger mod (BigInteger other)

returns the difference, product, quotient, remainder of this big integer and other.

  • int compareTo (BigInteger other)

returns 0 if this big integer equals other, a negative result if this big integer is less than other, and a positive result otherwise.

  • static BigInteger valueof (long x)

reuturns a big integer whose value equals x.

java.math.BigDecimal

  • BigDecimal add (BigDecimal other)
  • BigDecimal subtract (BigDecimal other)
  • BigDecimal multiply (BigDecimal other)
  • BigDecimal divide (BigDecimal other, RoundingMode mode)

returns the sum, difference, product, or quotient of this big decimal and other.
To compute the quotient, you must supply a rounding mode. The mode RoundingMode. HALF_UP is the rounding mode that you learned in school: round down the digits 0 to 4, round up the digits 5 to 9. It is appropriate fore routine calculations.
See the API documentation for other rounding modes.

  • static BigDecimal valueOf (long x, int scale)

returns a big decimal whose value equals x or x/10^scale.


                                                                                  END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值