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.); 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); } }
新聞熱點
疑難解答