题目举例:1元钱一瓶汽水,喝完后两个空瓶换一瓶汽水,问:你有20元钱,最多可以 喝到多少瓶汽水?
import java.io.*;
import java.lang.Double;
import java.math.BigDecimal;
public class countTest
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("总钱数:") ;
double money = Double.parseDouble(reader.readLine());
System.out.println("单价:") ;
double price = Double.parseDouble(reader.readLine());
System.out.println("兑换一听需要的空瓶数:") ;
int n = Integer.parseInt(reader.readLine());
System.out.println("***********************") ;
int i = countTest.count(money, price, n) ;
}
public static int count(double money, double price, int n)
{
//i为进行的轮数 ;
int iCount = 0 ;
//喝到的总瓶数 ;
int sum = 0 ;
//空瓶数量 ;
int iEmpty = 0 ;
//money不够 ;
if(money < price)
{
System.out.println("钱数不够,无法购买!") ;
return sum ;
}
//money充足 ;
else
{
double tmp ;
//第一轮比较特殊,用钱买 ;
if(iCount == 0)
{
iCount = iCount + 1 ;
tmp = money/price ;
Double d = new Double(tmp) ;
sum = d.intValue() ;
iEmpty = sum ;
System.out.println("第"+iCount+"次") ;
System.out.println("已经喝到"+sum+"瓶") ;
System.out.println("本次喝到"+sum+"瓶") ;
System.out.println("空瓶还有"+iEmpty+"个") ;
System.out.println("----------------") ;
}
//从第二轮开始,用瓶子换 ;
//空瓶足够换一瓶 ;
while(iEmpty >= n)
{
//tmp1表示本次喝到的瓶数 ;
int tmp1 ;
iCount = iCount + 1 ;
tmp1 = iEmpty/n ;
sum = sum + tmp1 ;
iEmpty = iEmpty/n + iEmpty%n ;
System.out.println("第"+iCount+"次") ;
System.out.println("已经喝到"+sum+"瓶") ;
System.out.println("本次喝到"+tmp1+"瓶") ;
System.out.println("空瓶还有"+iEmpty+"个") ;
System.out.println("----------------") ;
}
int iExchange = iCount - 1 ;
System.out.println("总共进行了1轮购买和"+iExchange+"轮兑换") ;
System.out.println("总共喝到"+sum+"瓶") ;
System.out.println("还剩余"+iEmpty+"个空瓶") ;
return sum ;
}
}
}