zoukankan      html  css  js  c++  java
  • 28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?

    package com.study.day07;
    
    import java.util.Scanner;
    
    /**
     * 28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,
    * 那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路)
    * 答:28人需要买19瓶,50人需要买34瓶。
     * @author denny
     *思路:1.先买一瓶
     *         2.用瓶盖数/3计算可以换多个瓶
     *         3.循环条件28瓶
     */
    public class Test {
    
        public static void main(String[] args) {
            
        
            Scanner input=new Scanner(System.in);
            //多买多少个人喝的
            System.out.println("请输入要买多少瓶");
            int count=input.nextInt();
        
            getCount(count);
            
        }
        
        public static void getCount(int count){
            int temp=0; //瓶盖数
            int buyPing=0;//当前瓶数
            int sendPing=0;
            while(sendPing+buyPing!=count){
                
                buyPing++;//多买一瓶
                temp++; //瓶盖数+1
                if(temp/3==1){
                    sendPing++;//赠送的瓶+1
                    temp=temp-2; //瓶盖重新计算 换了一瓶又多了一个瓶盖temp-3+1,所以是一2 也可以直接temp=1赋值
                }
            }
            System.out.println("买了: "+buyPing+"   送了: "+sendPing);
        }
        
    }
  • 相关阅读:
    zbb20180930 Postman 使用方法详解
    Cookie、Session、jsp、EL、JSTL
    Http协议、Tomcat、servlet
    xml、网络编程、 反射
    JDBC、DBUtils
    Java IO流对象、多线程
    mySql
    Java基础
    VueJs
    GIT
  • 原文地址:https://www.cnblogs.com/liunanjava/p/4789234.html
Copyright © 2011-2022 走看看