java算法----0至9这十个数组成两个三位数和一个四位数

http://yangjianzhouctgu.iteye.com/blog/1884313

Java代码 复制代码  收藏代码
  1. package com.zhonghaiwangluokeji.interview;   
  2.   
  3. /**  
  4.  * 0-9这十个数,组合成两个三位数和一个四位数  
  5.  * 求出满足条件:两个三位数相加等于那个四位数  
  6.  * 的所有这些数  
  7.  * @author yangjianzhou  
  8.  *  
  9.  */  
  10. public class Problem3 {   
  11.   
  12.     public static void main(String[] args) {   
  13.         printNumbers();   
  14.     }   
  15.     public static void printNumbers(){   
  16.   
  17.         int result = 0;   
  18.         for(int i=123;i<=987;i++){   
  19.             for(int j=123;j<=987;j++){   
  20.                 result = i+j;   
  21.                 if((result>1000)&&isTrue(result,i,j)){   
  22.                     System.out.print(result+" ");   
  23.                     System.out.print(i+" ");   
  24.                     System.out.println(j+" ");   
  25.                 }   
  26.             }   
  27.         }   
  28.     }   
  29.   
  30.     public static boolean isTrue(int result,int i,int j){   
  31.         String str = "0123456789";   
  32.         String s = result+""+i+""+j;   
  33.         int index = 0;   
  34.         String str1 = "";   
  35.         for(int k =0;k<str.length();k++){   
  36.             index = s.indexOf(str.charAt(k));   
  37.             if(index>=0){   
  38.                 str1 = s.replaceFirst(str.charAt(k)+"""a");   
  39.                 s = str1;   
  40.             }   
  41.         }   
  42.         String s1 = s.replaceAll("a""");   
  43.         if(s1.length()==0){   
  44.             return true;   
  45.         }   
  46.         return false;   
  47.     }   
  48. }  
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;
	}
}



运行结果:

Java代码 复制代码  收藏代码
  1. 1035 246 789    
  2. 1035 249 786    
  3. 1053 264 789    
  4. 1053 269 784    
  5. 1053 284 769    
  6. 1035 286 749    
  7. 1035 289 746    
  8. 1053 289 764    
  9. 1089 324 765    
  10. 1089 325 764    
  11. 1098 342 756    
  12. 1098 346 752    
  13. 1206 347 859    
  14. 1206 349 857    
  15. 1098 352 746    
  16. 1098 356 742    
  17. 1206 357 849    
  18. 1206 359 847    
  19. 1089 364 725    
  20. 1089 365 724    
  21. 1098 423 675    
  22. 1098 425 673    
  23. 1305 426 879    
  24. 1305 429 876    
  25. 1089 432 657    
  26. 1026 437 589    
  27. 1089 437 652    
  28. 1026 439 587    
  29. 1089 452 637    
  30. 1089 457 632    
  31. 1062 473 589    
  32. 1098 473 625    
  33. 1098 475 623    
  34. 1305 476 829    
  35. 1062 479 583    
  36. 1305 479 826    
  37. 1062 483 579    
  38. 1026 487 539    
  39. 1026 489 537    
  40. 1062 489 573    
  41. 1026 537 489    
  42. 1026 539 487    
  43. 1062 573 489    
  44. 1062 579 483    
  45. 1062 583 479    
  46. 1026 587 439    
  47. 1026 589 437    
  48. 1062 589 473    
  49. 1098 623 475    
  50. 1503 624 879    
  51. 1098 625 473    
  52. 1503 629 874    
  53. 1089 632 457    
  54. 1089 637 452    
  55. 1089 652 437    
  56. 1089 657 432    
  57. 1098 673 425    
  58. 1503 674 829    
  59. 1098 675 423    
  60. 1503 679 824    
  61. 1089 724 365    
  62. 1089 725 364    
  63. 1098 742 356    
  64. 1602 743 859    
  65. 1035 746 289    
  66. 1098 746 352    
  67. 1035 749 286    
  68. 1602 749 853    
  69. 1098 752 346    
  70. 1602 753 849    
  71. 1098 756 342    
  72. 1602 759 843    
  73. 1053 764 289    
  74. 1089 764 325    
  75. 1089 765 324    
  76. 1053 769 284    
  77. 1053 784 269    
  78. 1035 786 249    
  79. 1035 789 246    
  80. 1053 789 264    
  81. 1503 824 679    
  82. 1305 826 479    
  83. 1305 829 476    
  84. 1503 829 674    
  85. 1602 843 759    
  86. 1206 847 359    
  87. 1206 849 357    
  88. 1602 849 753    
  89. 1602 853 749    
  90. 1206 857 349    
  91. 1206 859 347    
  92. 1602 859 743    
  93. 1503 874 629    
  94. 1305 876 429    
  95. 1305 879 426    
  96. 1503 879 624   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值