案例:
输入 2
输出 1 1
输入 4
输出 2
输入 8
输出 2 2
输入 15
输出 3 2 1 1
- public class MyTest {
-
- public static void main(String[] args) {
- MyTest myTest = new MyTest() ;
- myTest.test(15);
- }
-
- public void test(int num){
- int n = num ;
- while (num > 0){
- double s = Math.sqrt(n) ;
- if (isInteger( s + "")){
- System.out.print((int)s + " ");
- num -= n ;
- n = num ;
- continue;
- }
- n -- ;
- }
- }
- //判断一个数是否为整数(3.0是整数)
- public boolean isInteger(String num){
- Pattern pattern = Pattern.compile("\\d+(\\.[0]){0,1}") ;
- return pattern.matcher(num).matches();
- }
- }
-