First is the topic description:
Describe
650) this.width=650; "src=" http://media.openjudge.cn/images/2747_1.jpg "style=" Border:0px;vertical-align:middle; " alt= "2747_1.jpg"/>
For example, there are 3 squares with an integer a1,a2,a3 inside each square. Known 0<=a1,a2,a3<=n, and A1+A2 is a multiple of 2, A2+A3 is a multiple of 3, A1+A2+A3 is a multiple of 5. Your task is to find a set of a1,a2,a3 that makes A1+A2+A3 the largest.
Input
A row that contains an integer n (0<=n<=100).
Output
An integer that is the maximum value of the A1+A2+A3.
Sample input
Sample output
And here's my solution:
When I first approached the problem, I thought of the poor lifting method, the code is as follows:
Import Java.util.scanner;public class Numblock {public static void main (string[] args) {int sum=0; Boolean Flag=false; Scanner sc = new Scanner (system.in), int n = sc.nextint (), for (int a1=0;a1<=n;a1++) {for (int a2=0;a2<=n;a2++) { T a3=0;a3<=n;a3++) {if ((A1+A2)%2==0&& (A2+A3)%3==0&& (A1+A2+A3)%5==0) {flag=true;} else {flag=false;} if (flag==true) {int sum1=a1+a2+a3;if (sum1>sum) {sum=sum1;}}}} SYSTEM.OUT.PRINTLN (sum);}}
The whole process can basically be divided into two steps:
(1) Determine whether the value of A1, A2, A3 three integers satisfies the requirements
I have defined a Boolean variable that can be followed only if its value is true;
(2) Calculates and outputs the maximum value of three integers
First, the last output of the integer variable sum is assigned a value of 0, after judging the current A1, A2, A3 to meet the requirements, calculate the sum of three integers at a time, and compared with the existing sum, so as to ensure that the final output is the maximum value.
The disadvantage of this code is that it is too nested for the For loop, and then I try to optimize it.
This article is from the "11962685" blog, please be sure to keep this source http://11972685.blog.51cto.com/11962685/1888734
Openjudge 4146: Digital squares java Poor lifting method