Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to output triangular numbers

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "how to use Java to output triangle numbers". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to use Java to output triangle numbers" can help you solve the problem.

Title:

Given a digital triangle as shown in the following figure, starting from the top, each node can choose to move to its lower left node or to its lower right node, all the way to the bottom, asking for a path to maximize the sum of numbers on the path.

7 3 8 8 1 0 2 7 4 44 5 2 6 5

Input format

The first line contains the integer nn, which represents the number of layers of the numeric triangle.

The next nn row contains several integers, each containing several integers, where the ii line represents the integer contained in the ii layer of the digital triangle.

Output format

Outputs an integer that represents the largest path number sum.

Data range

1 ≤ n ≤ 5001 ≤ n ≤ 500

Integer ≤ 10000 − 10000 in − 10000 ≤ Triangle Integer ≤ 10000 in ≤ Triangle

Enter a sample:

573 88 1 0 2 7 4 44 5 2 6 5

Sample output:

thirty

Answer to the question:

Compared with the two-dimensional array, each number can only go down or down to the right, and it is found that it is more convenient to go from the bottom to the top. We think backwards and go from the bottom or the lower right. If we know a certain number of devolved and lower right each time, who is the biggest?

Each time you compare who is the largest from the two adjacent numbers in the last row, add the largest number to the top of the first number, so that the first number in the first row of the array is the maximum.

For example:

Code: import java.util.Scanner; public class Main {public static void main (String [] args) {Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int nums [] [] = new int [n] []; for (int I = 0polii

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report