http://yangjianzhouctgu.iteye.com/blog/1884313
- package com.zhonghaiwangluokeji.interview;
- /**
- * 0-9这十个数,组合成两个三位数和一个四位数
- * 求出满足条件:两个三位数相加等于那个四位数
- * 的所有这些数
- * @author yangjianzhou
- *
- */
- public class Problem3 {
- public static void main(String[] args) {
- printNumbers();
- }
- public static void printNumbers(){
- int result = 0;
- for(int i=123;i<=987;i++){
- for(int j=123;j<=987;j++){
- result = i+j;
- if((result>1000)&&isTrue(result,i,j)){
- System.out.print(result+" ");
- System.out.print(i+" ");
- System.out.println(j+" ");
- }
- }
- }
- }
- public static boolean isTrue(int result,int i,int j){
- String str = "0123456789";
- String s = result+""+i+""+j;
- int index = 0;
- String str1 = "";
- for(int k =0;k<str.length();k++){
- index = s.indexOf(str.charAt(k));
- if(index>=0){
- str1 = s.replaceFirst(str.charAt(k)+"", "a");
- s = str1;
- }
- }
- String s1 = s.replaceAll("a", "");
- if(s1.length()==0){
- return true;
- }
- return false;
- }
- }
package com.zhonghaiwangluokeji.interview;
/**
* 0-9这十个数,组合成两个三位数和一个四位数
* 求出满足条件:两个三位数相加等于那个四位数
* 的所有这些数
* @author yangjianzhou
*
*/
public class Problem3 {
public static void main(String[] args) {
printNumbers();
}
public static void printNumbers(){
int result = 0;
for(int i=123;i<=987;i++){
for(int j=123;j<=987;j++){
result = i+j;
if((result>1000)&&isTrue(result,i,j)){
System.out.print(result+" ");
System.out.print(i+" ");
System.out.println(j+" ");
}
}
}
}
public static boolean isTrue(int result,int i,int j){
String str = "0123456789";
String s = result+""+i+""+j;
int index = 0;
String str1 = "";
for(int k =0;k<str.length();k++){
index = s.indexOf(str.charAt(k));
if(index>=0){
str1 = s.replaceFirst(str.charAt(k)+"", "a");
s = str1;
}
}
String s1 = s.replaceAll("a", "");
if(s1.length()==0){
return true;
}
return false;
}
}
运行结果:
- 1035 246 789
- 1035 249 786
- 1053 264 789
- 1053 269 784
- 1053 284 769
- 1035 286 749
- 1035 289 746
- 1053 289 764
- 1089 324 765
- 1089 325 764
- 1098 342 756
- 1098 346 752
- 1206 347 859
- 1206 349 857
- 1098 352 746
- 1098 356 742
- 1206 357 849
- 1206 359 847
- 1089 364 725
- 1089 365 724
- 1098 423 675
- 1098 425 673
- 1305 426 879
- 1305 429 876
- 1089 432 657
- 1026 437 589
- 1089 437 652
- 1026 439 587
- 1089 452 637
- 1089 457 632
- 1062 473 589
- 1098 473 625
- 1098 475 623
- 1305 476 829
- 1062 479 583
- 1305 479 826
- 1062 483 579
- 1026 487 539
- 1026 489 537
- 1062 489 573
- 1026 537 489
- 1026 539 487
- 1062 573 489
- 1062 579 483
- 1062 583 479
- 1026 587 439
- 1026 589 437
- 1062 589 473
- 1098 623 475
- 1503 624 879
- 1098 625 473
- 1503 629 874
- 1089 632 457
- 1089 637 452
- 1089 652 437
- 1089 657 432
- 1098 673 425
- 1503 674 829
- 1098 675 423
- 1503 679 824
- 1089 724 365
- 1089 725 364
- 1098 742 356
- 1602 743 859
- 1035 746 289
- 1098 746 352
- 1035 749 286
- 1602 749 853
- 1098 752 346
- 1602 753 849
- 1098 756 342
- 1602 759 843
- 1053 764 289
- 1089 764 325
- 1089 765 324
- 1053 769 284
- 1053 784 269
- 1035 786 249
- 1035 789 246
- 1053 789 264
- 1503 824 679
- 1305 826 479
- 1305 829 476
- 1503 829 674
- 1602 843 759
- 1206 847 359
- 1206 849 357
- 1602 849 753
- 1602 853 749
- 1206 857 349
- 1206 859 347
- 1602 859 743
- 1503 874 629
- 1305 876 429
- 1305 879 426
- 1503 879 624