把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包 含数字 2 和 4,一共有多少种不同的分解方法?
注意交换 3 个整数的顺序被视为同一种方法,例如 1000+1001+18 和 1001+1000+18 被视为同一种。注意读题 1,正整数 2,三个数各不相同—for(j=i+1) 3,不包含2,4—求数位和的方法判断 4boolean h函数写法 加一个count判断 5boolean拼写~~
代码 答案40785
public class 分解2019 { public static void main(String[] args) { int count=0; for(int i=1;i<=2019;i++) { for(int j=i+1;j<=2019;j++) { for(int k=j+1;k<=2019;k++) { if(i+j+k==2019&&h(i)&&h(k)&&h(j)) { count+=1; } } } } System.out.println(count); } public static boolean h(int a) { int count=0; while (a!=0) { if (a%10==2||a%10==4) { count=1; } a=a/10; } if(count==0) return true; else return false; } }