您的位置:首页 > 其它

递归与非递归解决组合问题

2006-10-08 11:23 363 查看
 递归算法:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NonRecursion {
 
 public static void main(String[] args){
   
   int n=0;
   int m=0;
   boolean finish=false;
   BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        
        
   while(!finish){
    try{
    //get n 
    System.out.println("Please enter n:");
    n = Integer.parseInt(input.readLine());
    if(n<0){
     System.err.println("n can't be less than zero, please enter again.");
     continue;
    } 
    System.out.println("n is:"+n);
    
    //get m
    System.out.println("Please enter m:");
    m = Integer.parseInt(input.readLine());
    if(m<0 || m >n){
     System.err.println("m can't be less than zero or more than n , please enter again.");
     continue;
    } 
    System.out.println("m is:"+m);
    finish = true;
    }catch(IOException e){
     System.err.println("There is an ioexception happening.");
    }
   }
 }
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息