サイダー問題


package com.javaeye.aspnetdb;

/**
 * 
 * @author aspentdb 
 * @url	www.aspnetdb.iteye.com 
 *     :
 *  1000   ,            , 3       1   ,
 *             ,          ,           。
 * 
 */
public class Drinks {


	public static void main(String[] args) {
		new Drinks().Method1(1000, 3);		
		new Drinks().Method2(1000, 4);
	}

	/**
	 * 
	 * @param iTotalNum        
	 * @param iRoleNum           
	 * 
	 *            
	 */
	public void Method1(int iTotalNum, int iRoleNum) {

		if(iRoleNum <= 0)
		{
			System.out.println("    !");
			return;
		}

		int drinknum = 0;	//       
		int emptyNum = 0;	//      
		while(iTotalNum > 0) {
			iTotalNum --;
			drinknum ++;
			emptyNum ++;
			if(emptyNum % iRoleNum == 0)
			{
				iTotalNum ++;
				emptyNum -= 3 ;
			}
		}

		System.out.println("    " + drinknum + ";  " + emptyNum + ";    ");

	}

	/**
	 * 
	 * @param iTotalNum        
	 * @param iRoleNum           
	 * 
	 *       ,      
	 */
	public void Method2(int iTotalNum, int iRoleNum) {

		if(iRoleNum <= 0)
		{
			System.out.println("    !");
			return;
		}

		int drinknum = 0;						//       
		int emptyNum = 0;						//      
		
		while(iTotalNum > 0) {
			
			drinknum += iTotalNum;				//      
			emptyNum += iTotalNum;				//       
			
			iTotalNum = emptyNum/iRoleNum;	    //       
			emptyNum -= iTotalNum*3;		    //        
			
		}

		System.out.println("    " + drinknum + ";  " + emptyNum + ";    ");

	}

}

 問題の出所とコードの実現は《Javaプログラミングのあれらの事》を参考にします