TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(97).特异数列
程序:
- import java.util.*;
- public class Test97{
-
- static int result[]=new int[100];
- static int M=0;
- static int N=0;
- public static void main(String aargs[]){
- System.out.printf("please input a number(0< N,M< 100):
- ");
- Scanner sc=new Scanner(System.in);
- while(sc.hasNext()){
- M=sc.nextInt();
- if(M==0) break;
- N=sc.nextInt();
- zuhe(M,N);
- }
- }
- public static void zuhe(int m,int k)
- {
- int i,j;
- for(i=m;i>=1;i--)
- {
- result[k-1]=i;
- if(k>1)
- zuhe(i,k-1);
- else
- {
- int temp=0;
- for(j=0;j< N;j++)
- temp+=result[j];
- if(temp==M)//当前组合是整数N的一个拆分
- {
- for(j=0;j< N;j++)
- System.out.printf("%4d",result[j]);
- System.out.printf("
- ");
- }
- }
- }
- }
- }
- C: >java Test97
- please input a number(0< N,M< 100):
- 8
- 4
- 1 1 1 5
- 1 1 2 4
- 1 1 3 3
- 1 2 2 3
- 2 2 2 2
复制代码
源码下载:http://file.javaxxz.com/2014/12/3/000707875.zip |
|