Write one to nine summation algorithm using JAVA

Write one to nine summation algorithm using JAVA

Find the 123456789 string 1..9 without changing the number position random +- requires the expression result to sum to 100, write such an expression


package demo;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;

/**
* @author yeqv
* @program A2
* @Classname a3
* @Date 2022/1/13 17:17
* @Email w16638771062@163.com
*/
public class a3 {
public static void main (String[] args) {
//define a collection
Set set = new HashSet();
int index = 0 ;
while ( true ) {
//Define an array of character type
String[] s1 = { "" , "+" , "-" };
//Instantiate a random generator object
Random rand = new Random();
//Instantiate a constructor object of a string type with the first string "1"
StringBuilder sbu = new StringBuilder( "1" );
//Define a loop, the maximum number is 9
for ( int i = 2 ; i <= 9 ; i++) {
//Insert a random string from the array s1 into the string constructor "sbu"
sbu.append(s1[rand.nextInt(s1.length)]);
//Insert the number i of the current loop into the constructor
sbu.append(i);
}
//Use regular expressions to formulate a format, each paragraph starts with +- or empty, followed by numbers between 0-9, for example
// 12+345-6+78-9
var p3 = Pattern.compile( "[+-]*[0-9]+" );
//match the string inside the string constructor with the format
var m3 = p3.matcher(sbu);
//Set a variable of type int as the sum of the formula
int sum = 0 ;
//Check whether the strings in the constructor match the format in order
while (m3.find()) {
//Convert subsequence string to int type assignment and num
int num = Integer.parseInt(m3.group());
//add the converted expressions
sum += num;
}
//Determine whether the sum of the expression is 100 and the string sbu cannot be included in the set
if (sum == 100 && !set.contains(sbu.toString())) {
// output sequence and formula
System.out.printf( "%d, %s = 100%n" , ++index, sbu);
//Add the string to the collection set
set.add(sbu.toString());
}
//Judging that the number of strings in the collection is enough to end with 10
if (set.size() == 10 ) {
break ;
}
}

}

}

Copyright statement: The content of this article is contributed by Alibaba Cloud real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00